11use derive_more:: From ;
22use indexmap:: IndexMap ;
3- use log:: { debug, trace} ;
3+ use log:: { debug, trace, warn } ;
44use smallvec:: SmallVec ;
55use std:: collections:: { HashMap , HashSet , hash_map:: Entry } ;
66use std:: mem;
@@ -18,7 +18,7 @@ use rustc_ast::ptr::P;
1818use rustc_ast_pretty:: pprust:: { self , PrintState , item_to_string} ;
1919use rustc_span:: symbol:: { Ident , kw} ;
2020use rustc_data_structures:: map_in_place:: MapInPlace ;
21- use rustc_span:: { BytePos , DUMMY_SP } ;
21+ use rustc_span:: { BytePos , DUMMY_SP , Symbol } ;
2222use smallvec:: smallvec;
2323
2424use crate :: ast_manip:: util:: { is_relative_path, join_visibility, namespace, split_uses, is_exported, is_c2rust_attr} ;
@@ -28,7 +28,6 @@ use crate::driver::Phase;
2828use crate :: { expect, match_or} ;
2929use crate :: path_edit:: fold_resolved_paths_with_id;
3030use crate :: RefactorCtxt ;
31- use crate :: util:: Lone ;
3231use crate :: ast_builder:: mk;
3332
3433use super :: externs;
@@ -553,10 +552,21 @@ impl<'a, 'tcx> Reorganizer<'a, 'tcx> {
553552 let mut module_items: IndexMap < NodeId , Vec < MovedDecl > > = IndexMap :: new ( ) ;
554553 // Move named items into module_items
555554 idents. map ( |idents| {
556- for ( ident, items) in idents. into_iter ( ) {
557- for item in items {
555+ for items in idents. into_values ( ) {
556+ for ( idx, mut item) in items. into_iter ( ) . enumerate ( ) {
557+ if idx > 0 {
558+ let ident = item. ident ( ) ;
559+ // Append a number suffix to this item if
560+ // there are multiple items with the same name
561+ let old_name = ident. name . as_str ( ) ;
562+ let new_name = format ! ( "{old_name}_{idx}" ) ;
563+ warn ! ( "Renaming identifier {old_name} to {new_name} due to collision" ) ;
564+ item. ident_mut ( ) . name = Symbol :: intern ( & new_name) ;
565+ }
566+
558567 let dest_module_id = self . find_destination_id ( & item) ;
559568
569+ let ident = item. ident ( ) ;
560570 let dest_module_info = self . modules . get_mut ( & dest_module_id) . unwrap ( ) ;
561571 dest_module_info. items [ item. namespace ] . insert ( ident) ;
562572 let mut path_segments = dest_module_info. path . clone ( ) ;
@@ -1100,6 +1110,22 @@ impl MovedDecl {
11001110 } ,
11011111 }
11021112 }
1113+
1114+ fn ident_mut ( & mut self ) -> & mut Ident {
1115+ match & mut self . kind {
1116+ DeclKind :: ForeignItem ( item, _) => & mut item. ident ,
1117+ DeclKind :: Item ( item) => {
1118+ // Reborrow the item inside the P<Item> so we can
1119+ // sub-borrow different fields from it
1120+ let item = & mut * * item;
1121+ if let ItemKind :: Use ( UseTree { kind : UseTreeKind :: Simple ( Some ( rename) , _, _) , .. } ) = & mut item. kind {
1122+ rename
1123+ } else {
1124+ & mut item. ident
1125+ }
1126+ }
1127+ }
1128+ }
11031129}
11041130
11051131impl ToString for MovedDecl {
@@ -1408,8 +1434,8 @@ impl<'a, 'tcx> HeaderDeclarations<'a, 'tcx> {
14081434 . type_ns
14091435 . into_iter ( )
14101436 . chain ( unnamed_items. value_ns . into_iter ( ) )
1411- . chain ( idents. type_ns . into_iter ( ) . map ( | ( _ , v ) | v . lone ( ) ) )
1412- . chain ( idents. value_ns . into_iter ( ) . map ( | ( _ , v ) | v . lone ( ) ) )
1437+ . chain ( idents. type_ns . into_values ( ) . flatten ( ) )
1438+ . chain ( idents. value_ns . into_values ( ) . flatten ( ) )
14131439 . collect :: < Vec < _ > > ( ) ;
14141440
14151441 all_items. sort_by ( |a, b| {
0 commit comments