Skip to content

Commit df2af7f

Browse files
committed
c2rust-refactor: Allow and rename identically named identifiers
Allow multiple different definitions of the same identifier. Disambiguate them by appending a numeric _N suffix to each identifier after the 0-th one.
1 parent 0a4988a commit df2af7f

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

c2rust-refactor/src/transform/reorganize_definitions.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use derive_more::From;
22
use indexmap::IndexMap;
3-
use log::{debug, trace};
3+
use log::{debug, trace, warn};
44
use smallvec::SmallVec;
55
use std::collections::{HashMap, HashSet, hash_map::Entry};
66
use std::mem;
@@ -18,7 +18,7 @@ use rustc_ast::ptr::P;
1818
use rustc_ast_pretty::pprust::{self, PrintState, item_to_string};
1919
use rustc_span::symbol::{Ident, kw};
2020
use rustc_data_structures::map_in_place::MapInPlace;
21-
use rustc_span::{BytePos, DUMMY_SP};
21+
use rustc_span::{BytePos, DUMMY_SP, Symbol};
2222
use smallvec::smallvec;
2323

2424
use 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;
2828
use crate::{expect, match_or};
2929
use crate::path_edit::fold_resolved_paths_with_id;
3030
use crate::RefactorCtxt;
31-
use crate::util::Lone;
3231
use crate::ast_builder::mk;
3332

3433
use 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

11051131
impl 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

Comments
 (0)