diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index 6174984e3bea..bae39a65cd91 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 }