Skip to content

Commit 27af650

Browse files
committed
resolve: Do not put macros into module.unexpanded_invocations unless necessary
1 parent 6667c66 commit 27af650

8 files changed

+111
-108
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -1155,14 +1155,18 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
11551155
false
11561156
}
11571157

1158-
fn visit_invoc(&mut self, id: NodeId) -> MacroRulesScopeRef<'a> {
1158+
fn visit_invoc(&mut self, id: NodeId) -> ExpnId {
11591159
let invoc_id = id.placeholder_to_expn_id();
1160-
1161-
self.parent_scope.module.unexpanded_invocations.borrow_mut().insert(invoc_id);
1162-
11631160
let old_parent_scope = self.r.invocation_parent_scopes.insert(invoc_id, self.parent_scope);
11641161
assert!(old_parent_scope.is_none(), "invocation data is reset for an invocation");
1162+
invoc_id
1163+
}
11651164

1165+
/// Visit invocation in context in which it can emit a named item (possibly `macro_rules`)
1166+
/// directly into its parent scope's module.
1167+
fn visit_invoc_in_module(&mut self, id: NodeId) -> MacroRulesScopeRef<'a> {
1168+
let invoc_id = self.visit_invoc(id);
1169+
self.parent_scope.module.unexpanded_invocations.borrow_mut().insert(invoc_id);
11661170
self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Invocation(invoc_id))
11671171
}
11681172

@@ -1291,7 +1295,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
12911295
return;
12921296
}
12931297
ItemKind::MacCall(..) => {
1294-
self.parent_scope.macro_rules = self.visit_invoc(item.id);
1298+
self.parent_scope.macro_rules = self.visit_invoc_in_module(item.id);
12951299
return;
12961300
}
12971301
ItemKind::Mod(..) => self.contains_macro_use(&item.attrs),
@@ -1309,15 +1313,15 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
13091313

13101314
fn visit_stmt(&mut self, stmt: &'b ast::Stmt) {
13111315
if let ast::StmtKind::MacCall(..) = stmt.kind {
1312-
self.parent_scope.macro_rules = self.visit_invoc(stmt.id);
1316+
self.parent_scope.macro_rules = self.visit_invoc_in_module(stmt.id);
13131317
} else {
13141318
visit::walk_stmt(self, stmt);
13151319
}
13161320
}
13171321

13181322
fn visit_foreign_item(&mut self, foreign_item: &'b ForeignItem) {
13191323
if let ForeignItemKind::MacCall(_) = foreign_item.kind {
1320-
self.visit_invoc(foreign_item.id);
1324+
self.visit_invoc_in_module(foreign_item.id);
13211325
return;
13221326
}
13231327

@@ -1336,7 +1340,14 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
13361340

13371341
fn visit_assoc_item(&mut self, item: &'b AssocItem, ctxt: AssocCtxt) {
13381342
if let AssocItemKind::MacCall(_) = item.kind {
1339-
self.visit_invoc(item.id);
1343+
match ctxt {
1344+
AssocCtxt::Trait => {
1345+
self.visit_invoc_in_module(item.id);
1346+
}
1347+
AssocCtxt::Impl => {
1348+
self.visit_invoc(item.id);
1349+
}
1350+
}
13401351
return;
13411352
}
13421353

@@ -1460,7 +1471,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
14601471
// type and value namespaces.
14611472
fn visit_variant(&mut self, variant: &'b ast::Variant) {
14621473
if variant.is_placeholder {
1463-
self.visit_invoc(variant.id);
1474+
self.visit_invoc_in_module(variant.id);
14641475
return;
14651476
}
14661477

src/test/ui/conditional-compilation/cfg-generic-params.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ LL | struct WhereBad where for<#[cfg(no)] 'a, #[cfg(yes)] T> u8: Copy;
1717
| ^
1818

1919
error: cannot find attribute `unknown` in this scope
20-
--> $DIR/cfg-generic-params.rs:34:43
20+
--> $DIR/cfg-generic-params.rs:19:29
2121
|
22-
LL | struct WhereYes where for<#[cfg_attr(yes, unknown)] 'a> u8: Copy;
23-
| ^^^^^^^
22+
LL | fn f_lt_yes<#[cfg_attr(yes, unknown)] 'a>() {}
23+
| ^^^^^^^
2424

2525
error: cannot find attribute `unknown` in this scope
26-
--> $DIR/cfg-generic-params.rs:30:40
26+
--> $DIR/cfg-generic-params.rs:22:29
2727
|
28-
LL | type PolyYes = dyn for<#[cfg_attr(yes, unknown)] 'a> Copy;
29-
| ^^^^^^^
28+
LL | fn f_ty_yes<#[cfg_attr(yes, unknown)] T>() {}
29+
| ^^^^^^^
3030

3131
error: cannot find attribute `unknown` in this scope
3232
--> $DIR/cfg-generic-params.rs:26:34
@@ -35,16 +35,16 @@ LL | type FnYes = for<#[cfg_attr(yes, unknown)] 'a> fn();
3535
| ^^^^^^^
3636

3737
error: cannot find attribute `unknown` in this scope
38-
--> $DIR/cfg-generic-params.rs:22:29
38+
--> $DIR/cfg-generic-params.rs:30:40
3939
|
40-
LL | fn f_ty_yes<#[cfg_attr(yes, unknown)] T>() {}
41-
| ^^^^^^^
40+
LL | type PolyYes = dyn for<#[cfg_attr(yes, unknown)] 'a> Copy;
41+
| ^^^^^^^
4242

4343
error: cannot find attribute `unknown` in this scope
44-
--> $DIR/cfg-generic-params.rs:19:29
44+
--> $DIR/cfg-generic-params.rs:34:43
4545
|
46-
LL | fn f_lt_yes<#[cfg_attr(yes, unknown)] 'a>() {}
47-
| ^^^^^^^
46+
LL | struct WhereYes where for<#[cfg_attr(yes, unknown)] 'a> u8: Copy;
47+
| ^^^^^^^
4848

4949
error: aborting due to 8 previous errors
5050

Original file line numberDiff line numberDiff line change
@@ -1,104 +1,104 @@
1-
error: cannot find attribute `lt_hof` in this scope
2-
--> $DIR/feature-gate-custom_attribute2.rs:51:21
1+
error: cannot find attribute `lt_struct` in this scope
2+
--> $DIR/feature-gate-custom_attribute2.rs:4:15
33
|
4-
LL | where Q: for <#[lt_hof] 'i> Fn(&'i [u32]) -> &'i u32
5-
| ^^^^^^
4+
LL | struct StLt<#[lt_struct] 'a>(&'a u32);
5+
| ^^^^^^^^^
66

7-
error: cannot find attribute `ty_meth` in this scope
8-
--> $DIR/feature-gate-custom_attribute2.rs:46:15
7+
error: cannot find attribute `ty_struct` in this scope
8+
--> $DIR/feature-gate-custom_attribute2.rs:6:15
99
|
10-
LL | fn m_ty<#[ty_meth] P>(_: P) { }
11-
| ^^^^^^^
10+
LL | struct StTy<#[ty_struct] I>(I);
11+
| ^^^^^^^^^
1212

13-
error: cannot find attribute `lt_meth` in this scope
14-
--> $DIR/feature-gate-custom_attribute2.rs:44:15
13+
error: cannot find attribute `lt_enum` in this scope
14+
--> $DIR/feature-gate-custom_attribute2.rs:9:13
1515
|
16-
LL | fn m_lt<#[lt_meth] 'h>(_: &'h [u32]) -> &'h u32 { loop { } }
17-
| ^^^^^^^
16+
LL | enum EnLt<#[lt_enum] 'b> { A(&'b u32), B }
17+
| ^^^^^^^
1818

19-
error: cannot find attribute `ty_fn` in this scope
20-
--> $DIR/feature-gate-custom_attribute2.rs:40:11
19+
error: cannot find attribute `ty_enum` in this scope
20+
--> $DIR/feature-gate-custom_attribute2.rs:11:13
2121
|
22-
LL | fn f_ty<#[ty_fn] O>(_: O) { }
23-
| ^^^^^
22+
LL | enum EnTy<#[ty_enum] J> { A(J), B }
23+
| ^^^^^^^
2424

25-
error: cannot find attribute `lt_fn` in this scope
26-
--> $DIR/feature-gate-custom_attribute2.rs:38:11
25+
error: cannot find attribute `lt_trait` in this scope
26+
--> $DIR/feature-gate-custom_attribute2.rs:14:14
2727
|
28-
LL | fn f_lt<#[lt_fn] 'g>(_: &'g [u32]) -> &'g u32 { loop { } }
29-
| ^^^^^
28+
LL | trait TrLt<#[lt_trait] 'c> { fn foo(&self, _: &'c [u32]) -> &'c u32; }
29+
| ^^^^^^^^
3030

31-
error: cannot find attribute `ty_impl_for` in this scope
32-
--> $DIR/feature-gate-custom_attribute2.rs:33:8
31+
error: cannot find attribute `ty_trait` in this scope
32+
--> $DIR/feature-gate-custom_attribute2.rs:16:14
3333
|
34-
LL | impl<#[ty_impl_for] N> TrTy<N> for StTy<N> {
35-
| ^^^^^^^^^^^
34+
LL | trait TrTy<#[ty_trait] K> { fn foo(&self, _: K); }
35+
| ^^^^^^^^
3636

37-
error: cannot find attribute `lt_impl_for` in this scope
38-
--> $DIR/feature-gate-custom_attribute2.rs:29:8
37+
error: cannot find attribute `lt_type` in this scope
38+
--> $DIR/feature-gate-custom_attribute2.rs:19:13
3939
|
40-
LL | impl<#[lt_impl_for] 'f> TrLt<'f> for StLt<'f> {
41-
| ^^^^^^^^^^^
40+
LL | type TyLt<#[lt_type] 'd> = &'d u32;
41+
| ^^^^^^^
4242

43-
error: cannot find attribute `ty_inherent` in this scope
44-
--> $DIR/feature-gate-custom_attribute2.rs:26:8
43+
error: cannot find attribute `ty_type` in this scope
44+
--> $DIR/feature-gate-custom_attribute2.rs:21:13
4545
|
46-
LL | impl<#[ty_inherent] M> StTy<M> { }
47-
| ^^^^^^^^^^^
46+
LL | type TyTy<#[ty_type] L> = (L, );
47+
| ^^^^^^^
4848

4949
error: cannot find attribute `lt_inherent` in this scope
5050
--> $DIR/feature-gate-custom_attribute2.rs:24:8
5151
|
5252
LL | impl<#[lt_inherent] 'e> StLt<'e> { }
5353
| ^^^^^^^^^^^
5454

55-
error: cannot find attribute `ty_type` in this scope
56-
--> $DIR/feature-gate-custom_attribute2.rs:21:13
55+
error: cannot find attribute `ty_inherent` in this scope
56+
--> $DIR/feature-gate-custom_attribute2.rs:26:8
5757
|
58-
LL | type TyTy<#[ty_type] L> = (L, );
59-
| ^^^^^^^
58+
LL | impl<#[ty_inherent] M> StTy<M> { }
59+
| ^^^^^^^^^^^
6060

61-
error: cannot find attribute `lt_type` in this scope
62-
--> $DIR/feature-gate-custom_attribute2.rs:19:13
61+
error: cannot find attribute `lt_impl_for` in this scope
62+
--> $DIR/feature-gate-custom_attribute2.rs:29:8
6363
|
64-
LL | type TyLt<#[lt_type] 'd> = &'d u32;
65-
| ^^^^^^^
64+
LL | impl<#[lt_impl_for] 'f> TrLt<'f> for StLt<'f> {
65+
| ^^^^^^^^^^^
6666

67-
error: cannot find attribute `ty_trait` in this scope
68-
--> $DIR/feature-gate-custom_attribute2.rs:16:14
67+
error: cannot find attribute `ty_impl_for` in this scope
68+
--> $DIR/feature-gate-custom_attribute2.rs:33:8
6969
|
70-
LL | trait TrTy<#[ty_trait] K> { fn foo(&self, _: K); }
71-
| ^^^^^^^^
70+
LL | impl<#[ty_impl_for] N> TrTy<N> for StTy<N> {
71+
| ^^^^^^^^^^^
7272

73-
error: cannot find attribute `lt_trait` in this scope
74-
--> $DIR/feature-gate-custom_attribute2.rs:14:14
73+
error: cannot find attribute `lt_fn` in this scope
74+
--> $DIR/feature-gate-custom_attribute2.rs:38:11
7575
|
76-
LL | trait TrLt<#[lt_trait] 'c> { fn foo(&self, _: &'c [u32]) -> &'c u32; }
77-
| ^^^^^^^^
76+
LL | fn f_lt<#[lt_fn] 'g>(_: &'g [u32]) -> &'g u32 { loop { } }
77+
| ^^^^^
7878

79-
error: cannot find attribute `ty_enum` in this scope
80-
--> $DIR/feature-gate-custom_attribute2.rs:11:13
79+
error: cannot find attribute `ty_fn` in this scope
80+
--> $DIR/feature-gate-custom_attribute2.rs:40:11
8181
|
82-
LL | enum EnTy<#[ty_enum] J> { A(J), B }
83-
| ^^^^^^^
82+
LL | fn f_ty<#[ty_fn] O>(_: O) { }
83+
| ^^^^^
8484

85-
error: cannot find attribute `lt_enum` in this scope
86-
--> $DIR/feature-gate-custom_attribute2.rs:9:13
85+
error: cannot find attribute `lt_meth` in this scope
86+
--> $DIR/feature-gate-custom_attribute2.rs:44:15
8787
|
88-
LL | enum EnLt<#[lt_enum] 'b> { A(&'b u32), B }
89-
| ^^^^^^^
88+
LL | fn m_lt<#[lt_meth] 'h>(_: &'h [u32]) -> &'h u32 { loop { } }
89+
| ^^^^^^^
9090

91-
error: cannot find attribute `ty_struct` in this scope
92-
--> $DIR/feature-gate-custom_attribute2.rs:6:15
91+
error: cannot find attribute `ty_meth` in this scope
92+
--> $DIR/feature-gate-custom_attribute2.rs:46:15
9393
|
94-
LL | struct StTy<#[ty_struct] I>(I);
95-
| ^^^^^^^^^
94+
LL | fn m_ty<#[ty_meth] P>(_: P) { }
95+
| ^^^^^^^
9696

97-
error: cannot find attribute `lt_struct` in this scope
98-
--> $DIR/feature-gate-custom_attribute2.rs:4:15
97+
error: cannot find attribute `lt_hof` in this scope
98+
--> $DIR/feature-gate-custom_attribute2.rs:51:21
9999
|
100-
LL | struct StLt<#[lt_struct] 'a>(&'a u32);
101-
| ^^^^^^^^^
100+
LL | where Q: for <#[lt_hof] 'i> Fn(&'i [u32]) -> &'i u32
101+
| ^^^^^^
102102

103103
error: aborting due to 17 previous errors
104104

src/test/ui/issues/issue-40845.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
error: cannot find macro `m` in this scope
2-
--> $DIR/issue-40845.rs:4:10
3-
|
4-
LL | impl S { m!(); }
5-
| ^
6-
71
error: cannot find macro `m` in this scope
82
--> $DIR/issue-40845.rs:1:11
93
|
104
LL | trait T { m!(); }
115
| ^
126

7+
error: cannot find macro `m` in this scope
8+
--> $DIR/issue-40845.rs:4:10
9+
|
10+
LL | impl S { m!(); }
11+
| ^
12+
1313
error: aborting due to 2 previous errors
1414

src/test/ui/parser/default-unmatched-assoc.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ LL | }
3939
| - item list ends here
4040

4141
error: cannot find macro `default` in this scope
42-
--> $DIR/default-unmatched-assoc.rs:12:5
42+
--> $DIR/default-unmatched-assoc.rs:4:5
4343
|
4444
LL | default!();
4545
| ^^^^^^^
4646

4747
error: cannot find macro `default` in this scope
48-
--> $DIR/default-unmatched-assoc.rs:4:5
48+
--> $DIR/default-unmatched-assoc.rs:12:5
4949
|
5050
LL | default!();
5151
| ^^^^^^^

src/test/ui/parser/issue-65122-mac-invoc-in-mut-patterns.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ LL | mac2! { does_not_exist!() }
3434
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
3535

3636
error: cannot find macro `does_not_exist` in this scope
37-
--> $DIR/issue-65122-mac-invoc-in-mut-patterns.rs:20:13
37+
--> $DIR/issue-65122-mac-invoc-in-mut-patterns.rs:22:13
3838
|
39-
LL | mac1! { does_not_exist!() }
39+
LL | mac2! { does_not_exist!() }
4040
| ^^^^^^^^^^^^^^
4141

4242
error: cannot find macro `does_not_exist` in this scope
43-
--> $DIR/issue-65122-mac-invoc-in-mut-patterns.rs:22:13
43+
--> $DIR/issue-65122-mac-invoc-in-mut-patterns.rs:20:13
4444
|
45-
LL | mac2! { does_not_exist!() }
45+
LL | mac1! { does_not_exist!() }
4646
| ^^^^^^^^^^^^^^
4747

4848
error: aborting due to 5 previous errors
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
// check-pass
2+
13
use std as line;
24

3-
const C: u32 = line!(); //~ ERROR cannot determine resolution for the macro `line`
5+
const C: u32 = line!();
46

57
fn main() {}

src/test/ui/resolve/macro-determinacy-non-module.stderr

-10
This file was deleted.

0 commit comments

Comments
 (0)