From 5210c501bc7320d7cc5e84f77d2c7ca5fca9a2e0 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Wed, 28 Aug 2024 23:34:59 +0000 Subject: Limit how deep we visit items to find cfg'd out names --- compiler/rustc_expand/src/expand.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index 6174984e3be..bae39a65cd9 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -1399,9 +1399,10 @@ impl InvocationCollectorNode for P { } fn declared_idents(&self) -> Vec { - struct ItemNameVisitor(Vec); + struct ItemNameVisitor(Vec, u8); impl Visitor<'_> for ItemNameVisitor { fn visit_item(&mut self, i: &ast::Item) { + self.1 += 1; if let ItemKind::Use(ut) = &i.kind { fn collect_use_tree_leaves(ut: &ast::UseTree, idents: &mut Vec) { match &ut.kind { @@ -1421,11 +1422,19 @@ impl InvocationCollectorNode for P { self.0.push(ident); } } - visit::walk_item(self, i); + if self.1 < 4 { + // We only visit up to 3 levels of nesting from this item, like if we were + // looking at `mod a`, we'd find item `a::b::c`. We have this limit to guard + // against deeply nested modules behind `cfg` flags, where we could spend + // significant time collecting this information purely for a potential + // diagnostic improvement. + visit::walk_item(self, i); + } + self.1 -= 1; } } - let mut v = ItemNameVisitor(vec![]); + let mut v = ItemNameVisitor(vec![], 0); v.visit_item(self); v.0 } -- cgit 1.4.1-3-g733a5