Skip to content

Commit fc57cd9

Browse files
committed
transpile: some minor cleanups from #1452
1 parent 584b50b commit fc57cd9

File tree

4 files changed

+44
-46
lines changed

4 files changed

+44
-46
lines changed

c2rust-transpile/src/rust_ast/item_store.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use indexmap::{IndexMap, IndexSet};
33
use syn::{ForeignItem, Ident, Item};
44

55
use std::borrow::Cow;
6+
use std::collections::HashSet;
67
use std::mem::swap;
78

89
#[derive(Debug)]
@@ -79,19 +80,14 @@ impl PathedMultiImports {
7980
.collect()
8081
}
8182

82-
/// Remove all imports covered by the other `PathedMultiImports`.
83+
/// Remove all imports covered by the other [`PathedMultiImports`].
8384
pub fn remove(&mut self, other: &PathedMultiImports) {
8485
for (k, v) in &mut self.0 {
8586
// We don't consider attributes, just subtract leaf sets.
8687
let other_items = other
8788
.0
8889
.get(k)
89-
.map(|imports| {
90-
imports
91-
.leaves
92-
.iter()
93-
.collect::<std::collections::HashSet<_>>()
94-
})
90+
.map(|imports| imports.leaves.iter().collect::<HashSet<_>>())
9591
.unwrap_or_default();
9692
v.leaves.retain(|leaf| !other_items.contains(leaf));
9793
}

c2rust-transpile/src/translator/mod.rs

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ pub fn translate(
664664
if t.tcfg.reorganize_definitions
665665
&& decl_file_id.map_or(false, |id| id != t.main_file)
666666
{
667-
let name: Option<&String> = t
667+
let name = t
668668
.ast_context
669669
.get_decl(&decl_id)
670670
.and_then(|x| x.kind.get_name());
@@ -941,12 +941,13 @@ impl<'a> IdentsOrGlob<'a> {
941941

942942
/// Extract the set of names made visible by a `use`.
943943
fn use_idents<'a>(i: &'a UseTree) -> IdentsOrGlob<'a> {
944+
use UseTree::*;
944945
match i {
945-
UseTree::Path(up) => use_idents(&up.tree),
946-
UseTree::Name(un) => IdentsOrGlob::Idents(vec![&un.ident]),
947-
UseTree::Rename(ur) => IdentsOrGlob::Idents(vec![&ur.rename]),
948-
UseTree::Glob(_ugl) => IdentsOrGlob::Glob,
949-
UseTree::Group(ugr) => ugr
946+
Path(up) => use_idents(&up.tree),
947+
Name(un) => IdentsOrGlob::Idents(vec![&un.ident]),
948+
Rename(ur) => IdentsOrGlob::Idents(vec![&ur.rename]),
949+
Glob(_ugl) => IdentsOrGlob::Glob,
950+
Group(ugr) => ugr
950951
.items
951952
.iter()
952953
.map(|tree| use_idents(tree))
@@ -1078,37 +1079,38 @@ fn make_submodule(
10781079

10791080
// Consumers will `use` reexported items at their exported locations.
10801081
for item in uses.into_items() {
1081-
if let Item::Use(ItemUse {
1082-
vis: Visibility::Public(_),
1083-
tree,
1084-
..
1085-
}) = &*item
1086-
{
1087-
match use_idents(tree) {
1088-
IdentsOrGlob::Idents(idents) => {
1089-
for ident in idents {
1090-
fn is_simd_type_name(name: &str) -> bool {
1091-
const SIMD_TYPE_NAMES: &[&str] = &[
1092-
"__m128i", "__m128", "__m128d", "__m64", "__m256", "__m256d",
1093-
"__m256i",
1094-
];
1095-
SIMD_TYPE_NAMES.contains(&name) || name.starts_with("_mm_")
1096-
}
1097-
let name = &*ident.to_string();
1098-
if is_simd_type_name(name) {
1099-
// Import vector type/operation names from the stdlib, as we also generate
1100-
// other uses for them from that location and can't easily reason about
1101-
// the ultimate target of reexported names when avoiding duplicate imports
1102-
// (which are verboten).
1103-
simd::add_arch_use(use_item_store, "x86", name);
1104-
simd::add_arch_use(use_item_store, "x86_64", name);
1105-
} else {
1106-
// Add a `use` for `self::this_module::exported_name`.
1107-
use_item_store.add_use(false, use_path(), name);
1108-
}
1109-
}
1110-
}
1111-
IdentsOrGlob::Glob => {}
1082+
let tree = match &*item {
1083+
Item::Use(ItemUse {
1084+
vis: Visibility::Public(_),
1085+
tree,
1086+
..
1087+
}) => tree,
1088+
_ => continue,
1089+
};
1090+
let idents = match use_idents(tree) {
1091+
IdentsOrGlob::Idents(idents) => idents,
1092+
IdentsOrGlob::Glob => continue,
1093+
};
1094+
1095+
fn is_simd_type_name(name: &str) -> bool {
1096+
const SIMD_TYPE_NAMES: &[&str] = &[
1097+
"__m128i", "__m128", "__m128d", "__m64", "__m256", "__m256d", "__m256i",
1098+
];
1099+
SIMD_TYPE_NAMES.contains(&name) || name.starts_with("_mm_")
1100+
}
1101+
1102+
for ident in idents {
1103+
let name = &*ident.to_string();
1104+
if is_simd_type_name(name) {
1105+
// Import vector type/operation names from the stdlib, as we also generate
1106+
// other uses for them from that location and can't easily reason about
1107+
// the ultimate target of reexported names when avoiding duplicate imports
1108+
// (which are verboten).
1109+
simd::add_arch_use(use_item_store, "x86", name);
1110+
simd::add_arch_use(use_item_store, "x86_64", name);
1111+
} else {
1112+
// Add a `use` for `self::this_module::exported_name`.
1113+
use_item_store.add_use(false, use_path(), name);
11121114
}
11131115
}
11141116
items.push(item);

tests/items/src/test_fn_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub fn test_fn_attrs() {
88

99
// Remove the c2rust::src_loc annotation, which is only produced if
1010
// --reorganize-definitions is enabled.
11-
let mut lines: Vec<&str> = src.lines().collect();
11+
let mut lines = src.lines().collect::<Vec<_>>();
1212
lines.retain(|x| !x.contains("#[c2rust::src_loc"));
1313
let src = lines.join("\n");
1414

tests/statics/src/test_sections.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn test_sectioned_used_static() {
3636
// directly at the source file
3737
let src = include_str!("attributes.rs");
3838

39-
let mut lines: Vec<&str> = src.lines().collect();
39+
let mut lines = src.lines().collect::<Vec<_>>();
4040

4141
// Remove the c2rust::src_loc annotation, which is only produced if
4242
// --reorganize-definitions is enabled.

0 commit comments

Comments
 (0)