Skip to content

Commit 8d9c9b0

Browse files
committed
Limit how deep we visit items to find cfg'd out names
1 parent 0b99f4a commit 8d9c9b0

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

compiler/rustc_expand/src/expand.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -1274,9 +1274,10 @@ impl InvocationCollectorNode for P<ast::Item> {
12741274
res
12751275
}
12761276
fn declared_names(&self) -> Vec<Ident> {
1277-
struct ItemNameVisitor(Vec<Ident>);
1277+
struct ItemNameVisitor(Vec<Ident>, u8);
12781278
impl Visitor<'_> for ItemNameVisitor {
12791279
fn visit_item(&mut self, i: &ast::Item) {
1280+
self.1 += 1;
12801281
if let ItemKind::Use(ut) = &i.kind {
12811282
fn collect_use_tree_leaves(ut: &ast::UseTree, idents: &mut Vec<Ident>) {
12821283
match &ut.kind {
@@ -1294,11 +1295,19 @@ impl InvocationCollectorNode for P<ast::Item> {
12941295
} else {
12951296
self.0.push(i.ident);
12961297
}
1297-
visit::walk_item(self, i);
1298+
if self.1 < 4 {
1299+
// We only visit up to 3 levels of nesting from this item, like if we were
1300+
// looking at `mod a`, we'd find item `a::b::c`. We have this limit to guard
1301+
// against deeply nested modules behind `cfg` flags, where we could spend
1302+
// significant time collecting this information purely for a potential
1303+
// diagnostic improvement.
1304+
visit::walk_item(self, i);
1305+
}
1306+
self.1 -= 1;
12981307
}
12991308
}
13001309

1301-
let mut v = ItemNameVisitor(vec![]);
1310+
let mut v = ItemNameVisitor(vec![], 0);
13021311
v.visit_item(self);
13031312
v.0
13041313
}

0 commit comments

Comments
 (0)