about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2024-08-28 23:34:59 +0000
committerEsteban Küber <esteban@kuber.com.ar>2025-08-01 21:51:05 +0000
commit5210c501bc7320d7cc5e84f77d2c7ca5fca9a2e0 (patch)
treef8e519db067fb569bafd5c1ca97ea12cda8d1b07
parentadcda6ca9a6d27c04399e3efe1c67fc6ff04d997 (diff)
downloadrust-5210c501bc7320d7cc5e84f77d2c7ca5fca9a2e0.tar.gz
rust-5210c501bc7320d7cc5e84f77d2c7ca5fca9a2e0.zip
Limit how deep we visit items to find cfg'd out names
-rw-r--r--compiler/rustc_expand/src/expand.rs15
1 files 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<ast::Item> {
     }
 
     fn declared_idents(&self) -> Vec<Ident> {
-        struct ItemNameVisitor(Vec<Ident>);
+        struct ItemNameVisitor(Vec<Ident>, 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<Ident>) {
                         match &ut.kind {
@@ -1421,11 +1422,19 @@ impl InvocationCollectorNode for P<ast::Item> {
                         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
     }