Skip to content

Commit 69c26e1

Browse files
committed
Centralize logic for use statement stuff
1 parent f95cd4b commit 69c26e1

3 files changed

Lines changed: 56 additions & 22 deletions

File tree

cli/src/imports.rs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,14 @@ fn classify_use(tree: &UseTree, manifest: &Manifest) -> UseShape {
199199
let mut full = prefix.clone();
200200
full.push(name.clone());
201201
let path = full.join("::");
202-
let Some(feature) = manifest.paths.get(&path) else {
202+
let Some((full_path, feature)) = manifest.resolve_path(&path) else {
203203
return UseShape::Other;
204204
};
205205
return UseShape::Single {
206206
leaf: EngageLeaf {
207-
full_path: path,
207+
full_path: full_path.to_string(),
208208
local_name: name,
209-
feature: feature.clone(),
209+
feature: feature.to_string(),
210210
},
211211
};
212212
}
@@ -216,14 +216,14 @@ fn classify_use(tree: &UseTree, manifest: &Manifest) -> UseShape {
216216
let mut full = prefix.clone();
217217
full.push(name.clone());
218218
let path = full.join("::");
219-
let Some(feature) = manifest.paths.get(&path) else {
219+
let Some((full_path, feature)) = manifest.resolve_path(&path) else {
220220
return UseShape::Other;
221221
};
222222
return UseShape::Single {
223223
leaf: EngageLeaf {
224-
full_path: path,
224+
full_path: full_path.to_string(),
225225
local_name: local,
226-
feature: feature.clone(),
226+
feature: feature.to_string(),
227227
},
228228
};
229229
}
@@ -236,12 +236,12 @@ fn classify_use(tree: &UseTree, manifest: &Manifest) -> UseShape {
236236
let mut full = prefix.clone();
237237
full.push(name.clone());
238238
let path = full.join("::");
239-
let kind = if let Some(feature) = manifest.paths.get(&path) {
239+
let kind = if let Some((full_path, feature)) = manifest.resolve_path(&path) {
240240
MemberKind::Engage {
241241
leaf: EngageLeaf {
242-
full_path: path,
242+
full_path: full_path.to_string(),
243243
local_name: name.clone(),
244-
feature: feature.clone(),
244+
feature: feature.to_string(),
245245
},
246246
}
247247
} else {
@@ -258,12 +258,12 @@ fn classify_use(tree: &UseTree, manifest: &Manifest) -> UseShape {
258258
let mut full = prefix.clone();
259259
full.push(name.clone());
260260
let path = full.join("::");
261-
let kind = if let Some(feature) = manifest.paths.get(&path) {
261+
let kind = if let Some((full_path, feature)) = manifest.resolve_path(&path) {
262262
MemberKind::Engage {
263263
leaf: EngageLeaf {
264-
full_path: path,
264+
full_path: full_path.to_string(),
265265
local_name: local.clone(),
266-
feature: feature.clone(),
266+
feature: feature.to_string(),
267267
},
268268
}
269269
} else {
@@ -336,6 +336,27 @@ impl<'ast> Visit<'ast> for BodyRefVisitor {
336336
fn visit_ident(&mut self, ident: &'ast proc_macro2::Ident) {
337337
self.out.insert(strip_raw(&ident.to_string()));
338338
}
339+
340+
// syn keeps a macro's contents as raw tokens and never parses them, so a type
341+
// used only inside something like `vec![Fade::black_out(...)]` is invisible to
342+
// the normal walk. Pull every identifier out of the macro tokens so we don't
343+
// call a still-used import dead and delete it.
344+
fn visit_macro(&mut self, mac: &'ast syn::Macro) {
345+
collect_idents_from_tokens(mac.tokens.clone(), &mut self.out);
346+
syn::visit::visit_macro(self, mac);
347+
}
348+
}
349+
350+
fn collect_idents_from_tokens(tokens: proc_macro2::TokenStream, out: &mut BTreeSet<String>) {
351+
for tt in tokens {
352+
match tt {
353+
proc_macro2::TokenTree::Ident(id) => {
354+
out.insert(strip_raw(&id.to_string()));
355+
}
356+
proc_macro2::TokenTree::Group(g) => collect_idents_from_tokens(g.stream(), out),
357+
_ => {}
358+
}
359+
}
339360
}
340361

341362
pub fn apply_use_edits(file: &Path, edits: &[UseEdit]) -> Result<()> {

cli/src/manifest.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ impl Manifest {
5757
self.paths.iter().filter(|(path, _)| path.split("::").last() == Some(leaf)).collect()
5858
}
5959

60+
/// Resolve a path as written in code to its (canonical path, gating feature).
61+
/// Accepts the canonical path itself (engage_il2cpp::app::fade::Fade) or the
62+
/// short re-export form code usually writes (engage_il2cpp::app::Fade), and
63+
/// always hands back the canonical one so callers agree on a single key.
64+
/// Returns None if the path names no known engage type. This is the one place
65+
/// that knows about re-exports, so both the scanner and the prune import check
66+
/// go through it instead of each reimplementing the lookup.
67+
pub fn resolve_path(&self, path: &str) -> Option<(&str, &str)> {
68+
if let Some((canonical, feature)) = self.paths.get_key_value(path) {
69+
return Some((canonical.as_str(), feature.as_str()));
70+
}
71+
72+
let canonical = self.reexports.get(path)?;
73+
let feature = self.paths.get(canonical)?;
74+
Some((canonical.as_str(), feature.as_str()))
75+
}
76+
6077
pub fn closure(&self, feature: &str) -> BTreeSet<String> {
6178
let mut out = BTreeSet::new();
6279
let mut stack = vec![feature.to_string()];

cli/src/scan.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,12 @@ impl<'a> PathVisitor<'a> {
110110
for n in (2..=segments.len()).rev() {
111111
let candidate: String = segments[..n].join("::");
112112

113-
if self.manifest.paths.contains_key(&candidate) {
114-
self.out.insert(candidate);
115-
return;
116-
}
117-
118-
// Code usually imports the short re-export path (engage_il2cpp::app::Fade)
119-
// rather than the canonical one (engage_il2cpp::app::fade::Fade). Record the
120-
// canonical so it maps to a feature downstream.
121-
if let Some(canonical) = self.manifest.reexports.get(&candidate) {
122-
self.out.insert(canonical.clone());
113+
// Matches either the canonical path or the short re-export form code
114+
// usually writes (engage_il2cpp::app::Fade). Record the canonical so it
115+
// maps to a feature downstream.
116+
if let Some((canonical, _)) = self.manifest.resolve_path(&candidate) {
117+
let canonical = canonical.to_string();
118+
self.out.insert(canonical);
123119
return;
124120
}
125121

0 commit comments

Comments
 (0)