Skip to content

Commit 8e5a67c

Browse files
committed
transpile: deduplicate imports from main module itself vs. submodules
1 parent f585328 commit 8e5a67c

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

c2rust-transpile/src/rust_ast/item_store.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ impl PathedMultiImports {
7878
})
7979
.collect()
8080
}
81+
82+
/// Remove all imports covered by the other `PathedMultiImports`.
83+
pub fn remove(&mut self, other: &PathedMultiImports) {
84+
for (k, v) in &mut self.0 {
85+
// We don't consider attributes, just subtract leaf sets.
86+
let other_items = other
87+
.0
88+
.get(k)
89+
.map(|imports| {
90+
imports
91+
.leaves
92+
.iter()
93+
.collect::<std::collections::HashSet<_>>()
94+
})
95+
.unwrap_or_default();
96+
v.leaves.retain(|leaf| !other_items.contains(leaf));
97+
}
98+
}
8199
}
82100

83101
#[derive(Debug, Default)]

c2rust-transpile/src/translator/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,14 +879,17 @@ pub fn translate(
879879

880880
all_items.extend(mod_items);
881881

882+
// Before consuming `uses`, remove them from the uses from submodules to avoid duplicates.
883+
let (_, _, mut new_uses) = new_uses.drain();
884+
new_uses.remove(&uses);
885+
882886
// This could have been merged in with items below; however, it's more idiomatic to have
883887
// imports near the top of the file than randomly scattered about. Also, there is probably
884888
// no reason to have comments associated with imports so it doesn't need to go through
885889
// the above comment store process
886890
all_items.extend(uses.into_items());
887891

888892
// Print new uses from submodules
889-
let (_, _, new_uses) = new_uses.drain();
890893
all_items.extend(new_uses.into_items());
891894

892895
if !foreign_items.is_empty() {

0 commit comments

Comments
 (0)