Skip to content

Commit 774e1be

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 0ba23b6 commit 774e1be

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

c2rust-refactor/src/transform/reorganize_definitions.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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,19 @@ 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 name = format!("{0}_{idx}", ident.name.as_str());
562+
item.ident_mut().name = Symbol::intern(&name);
563+
}
564+
558565
let dest_module_id = self.find_destination_id(&item);
559566

567+
let ident = item.ident();
560568
let dest_module_info = self.modules.get_mut(&dest_module_id).unwrap();
561569
dest_module_info.items[item.namespace].insert(ident);
562570
let mut path_segments = dest_module_info.path.clone();
@@ -1100,6 +1108,22 @@ impl MovedDecl {
11001108
},
11011109
}
11021110
}
1111+
1112+
fn ident_mut(&mut self) -> &mut Ident {
1113+
match &mut self.kind {
1114+
DeclKind::ForeignItem(item, _) => &mut item.ident,
1115+
DeclKind::Item(item) => {
1116+
// Reborrow the item inside the P<Item> so we can
1117+
// sub-borrow different fields from it
1118+
let item = &mut **item;
1119+
if let ItemKind::Use(UseTree { kind: UseTreeKind::Simple(Some(rename), _, _), .. }) = &mut item.kind {
1120+
rename
1121+
} else {
1122+
&mut item.ident
1123+
}
1124+
}
1125+
}
1126+
}
11031127
}
11041128

11051129
impl ToString for MovedDecl {
@@ -1408,8 +1432,8 @@ impl<'a, 'tcx> HeaderDeclarations<'a, 'tcx> {
14081432
.type_ns
14091433
.into_iter()
14101434
.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()))
1435+
.chain(idents.type_ns.into_values().flatten())
1436+
.chain(idents.value_ns.into_values().flatten())
14131437
.collect::<Vec<_>>();
14141438

14151439
all_items.sort_by(|a, b| {

0 commit comments

Comments
 (0)