Skip to content

Commit 23b82c3

Browse files
committed
resolve: Move macro resolution traces from Modules to Resolver
Traces already contain module info without that. It's easy to forget to call `finalize_*` on a module. In particular, macros enum and trait modules weren't finalized. By happy accident macros weren't placed into those modules until now.
1 parent 73dee25 commit 23b82c3

12 files changed

+90
-95
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,9 +1267,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
12671267

12681268
fn visit_attribute(&mut self, attr: &'b ast::Attribute) {
12691269
if !attr.is_sugared_doc && is_builtin_attr(attr) {
1270-
self.parent_scope.module.builtin_attrs.borrow_mut().push((
1271-
attr.path.segments[0].ident, self.parent_scope.clone()
1272-
));
1270+
self.r.builtin_attrs.push((attr.path.segments[0].ident, self.parent_scope.clone()));
12731271
}
12741272
visit::walk_attribute(self, attr);
12751273
}

src/librustc_resolve/late.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,6 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
574574
self.ribs[ValueNS].push(Rib::new(ModuleRibKind(module)));
575575
self.ribs[TypeNS].push(Rib::new(ModuleRibKind(module)));
576576

577-
self.r.finalize_current_module_macro_resolutions(module);
578577
let ret = f(self);
579578

580579
self.parent_scope.module = orig_module;
@@ -1227,7 +1226,6 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
12271226
self.ribs[ValueNS].push(Rib::new(ModuleRibKind(anonymous_module)));
12281227
self.ribs[TypeNS].push(Rib::new(ModuleRibKind(anonymous_module)));
12291228
self.parent_scope.module = anonymous_module;
1230-
self.r.finalize_current_module_macro_resolutions(anonymous_module);
12311229
} else {
12321230
self.ribs[ValueNS].push(Rib::new(NormalRibKind));
12331231
}
@@ -1984,7 +1982,6 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
19841982

19851983
impl<'a> Resolver<'a> {
19861984
pub(crate) fn late_resolve_crate(&mut self, krate: &Crate) {
1987-
self.finalize_current_module_macro_resolutions(self.graph_root);
19881985
let mut late_resolution_visitor = LateResolutionVisitor::new(self);
19891986
visit::walk_crate(&mut late_resolution_visitor, krate);
19901987
for (id, span) in late_resolution_visitor.unused_labels.iter() {

src/librustc_resolve/lib.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,6 @@ pub struct ModuleData<'a> {
418418
normal_ancestor_id: DefId,
419419

420420
resolutions: RefCell<FxHashMap<(Ident, Namespace), &'a RefCell<NameResolution<'a>>>>,
421-
single_segment_macro_resolutions: RefCell<Vec<(Ident, MacroKind, ParentScope<'a>,
422-
Option<&'a NameBinding<'a>>)>>,
423-
multi_segment_macro_resolutions: RefCell<Vec<(Vec<Segment>, Span, MacroKind, ParentScope<'a>,
424-
Option<Res>)>>,
425-
builtin_attrs: RefCell<Vec<(Ident, ParentScope<'a>)>>,
426421

427422
// Macro invocations that can expand into items in this module.
428423
unresolved_invocations: RefCell<FxHashSet<ExpnId>>,
@@ -459,9 +454,6 @@ impl<'a> ModuleData<'a> {
459454
kind,
460455
normal_ancestor_id,
461456
resolutions: Default::default(),
462-
single_segment_macro_resolutions: RefCell::new(Vec::new()),
463-
multi_segment_macro_resolutions: RefCell::new(Vec::new()),
464-
builtin_attrs: RefCell::new(Vec::new()),
465457
unresolved_invocations: Default::default(),
466458
no_implicit_prelude: false,
467459
glob_importers: RefCell::new(Vec::new()),
@@ -896,6 +888,12 @@ pub struct Resolver<'a> {
896888
local_macro_def_scopes: FxHashMap<NodeId, Module<'a>>,
897889
unused_macros: NodeMap<Span>,
898890
proc_macro_stubs: NodeSet,
891+
/// Traces collected during macro resolution and validated when it's complete.
892+
single_segment_macro_resolutions: Vec<(Ident, MacroKind, ParentScope<'a>,
893+
Option<&'a NameBinding<'a>>)>,
894+
multi_segment_macro_resolutions: Vec<(Vec<Segment>, Span, MacroKind, ParentScope<'a>,
895+
Option<Res>)>,
896+
builtin_attrs: Vec<(Ident, ParentScope<'a>)>,
899897
/// Some built-in derives mark items they are applied to so they are treated specially later.
900898
/// Derive macros cannot modify the item themselves and have to store the markers in the global
901899
/// context, so they attach the markers to derive container IDs using this resolver table.
@@ -1151,6 +1149,9 @@ impl<'a> Resolver<'a> {
11511149
struct_constructors: Default::default(),
11521150
unused_macros: Default::default(),
11531151
proc_macro_stubs: Default::default(),
1152+
single_segment_macro_resolutions: Default::default(),
1153+
multi_segment_macro_resolutions: Default::default(),
1154+
builtin_attrs: Default::default(),
11541155
special_derives: Default::default(),
11551156
active_features:
11561157
features.declared_lib_features.iter().map(|(feat, ..)| *feat)
@@ -1203,6 +1204,7 @@ impl<'a> Resolver<'a> {
12031204
/// Entry point to crate resolution.
12041205
pub fn resolve_crate(&mut self, krate: &Crate) {
12051206
ImportResolver { r: self }.finalize_imports();
1207+
self.finalize_macro_resolutions();
12061208

12071209
self.late_resolve_crate(krate);
12081210

src/librustc_resolve/macros.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ impl<'a> Resolver<'a> {
366366

367367
if trace {
368368
let kind = kind.expect("macro kind must be specified if tracing is enabled");
369-
parent_scope.module.multi_segment_macro_resolutions.borrow_mut()
369+
self.multi_segment_macro_resolutions
370370
.push((path, path_span, kind, parent_scope.clone(), res.ok()));
371371
}
372372

@@ -383,7 +383,7 @@ impl<'a> Resolver<'a> {
383383

384384
if trace {
385385
let kind = kind.expect("macro kind must be specified if tracing is enabled");
386-
parent_scope.module.single_segment_macro_resolutions.borrow_mut()
386+
self.single_segment_macro_resolutions
387387
.push((path[0].ident, kind, parent_scope.clone(), binding.ok()));
388388
}
389389

@@ -693,7 +693,7 @@ impl<'a> Resolver<'a> {
693693
}
694694
}
695695

696-
pub fn finalize_current_module_macro_resolutions(&mut self, module: Module<'a>) {
696+
crate fn finalize_macro_resolutions(&mut self) {
697697
let check_consistency = |this: &mut Self, path: &[Segment], span, kind: MacroKind,
698698
initial_res: Option<Res>, res: Res| {
699699
if let Some(initial_res) = initial_res {
@@ -729,8 +729,7 @@ impl<'a> Resolver<'a> {
729729
}
730730
};
731731

732-
let macro_resolutions =
733-
mem::take(&mut *module.multi_segment_macro_resolutions.borrow_mut());
732+
let macro_resolutions = mem::take(&mut self.multi_segment_macro_resolutions);
734733
for (mut path, path_span, kind, parent_scope, initial_res) in macro_resolutions {
735734
// FIXME: Path resolution will ICE if segment IDs present.
736735
for seg in &mut path { seg.id = None; }
@@ -757,8 +756,7 @@ impl<'a> Resolver<'a> {
757756
}
758757
}
759758

760-
let macro_resolutions =
761-
mem::take(&mut *module.single_segment_macro_resolutions.borrow_mut());
759+
let macro_resolutions = mem::take(&mut self.single_segment_macro_resolutions);
762760
for (ident, kind, parent_scope, initial_binding) in macro_resolutions {
763761
match self.early_resolve_ident_in_lexical_scope(ident, ScopeSet::Macro(kind),
764762
&parent_scope, true, true, ident.span) {
@@ -783,7 +781,7 @@ impl<'a> Resolver<'a> {
783781
}
784782
}
785783

786-
let builtin_attrs = mem::take(&mut *module.builtin_attrs.borrow_mut());
784+
let builtin_attrs = mem::take(&mut self.builtin_attrs);
787785
for (ident, parent_scope) in builtin_attrs {
788786
let _ = self.early_resolve_ident_in_lexical_scope(
789787
ident, ScopeSet::Macro(MacroKind::Attr), &parent_scope, true, true, ident.span

src/librustc_resolve/resolve_imports.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
848848
directive.vis.set(orig_vis);
849849
let module = match path_res {
850850
PathResult::Module(module) => {
851-
// Consistency checks, analogous to `finalize_current_module_macro_resolutions`.
851+
// Consistency checks, analogous to `finalize_macro_resolutions`.
852852
if let Some(initial_module) = directive.imported_module.get() {
853853
if !ModuleOrUniformRoot::same_def(module, initial_module) && no_ambiguity {
854854
span_bug!(directive.span, "inconsistent resolution for an import");
@@ -973,7 +973,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
973973

974974
match binding {
975975
Ok(binding) => {
976-
// Consistency checks, analogous to `finalize_current_module_macro_resolutions`.
976+
// Consistency checks, analogous to `finalize_macro_resolutions`.
977977
let initial_res = source_bindings[ns].get().map(|initial_binding| {
978978
all_ns_err = false;
979979
if let Some(target_binding) = target_bindings[ns].get() {

src/test/ui/feature-gates/feature-gate-rustc-diagnostic-macros.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ error: cannot find macro `__build_diagnostic_array!` in this scope
44
LL | __build_diagnostic_array!(DIAGNOSTICS);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error: cannot find macro `__register_diagnostic!` in this scope
8-
--> $DIR/feature-gate-rustc-diagnostic-macros.rs:4:1
9-
|
10-
LL | __register_diagnostic!(E0001);
11-
| ^^^^^^^^^^^^^^^^^^^^^
12-
137
error: cannot find macro `__diagnostic_used!` in this scope
148
--> $DIR/feature-gate-rustc-diagnostic-macros.rs:8:5
159
|
1610
LL | __diagnostic_used!(E0001);
1711
| ^^^^^^^^^^^^^^^^^
1812

13+
error: cannot find macro `__register_diagnostic!` in this scope
14+
--> $DIR/feature-gate-rustc-diagnostic-macros.rs:4:1
15+
|
16+
LL | __register_diagnostic!(E0001);
17+
| ^^^^^^^^^^^^^^^^^^^^^
18+
1919
error: aborting due to 3 previous errors
2020

src/test/ui/hygiene/no_implicit_prelude.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
error: cannot find macro `panic!` in this scope
2+
--> $DIR/no_implicit_prelude.rs:16:9
3+
|
4+
LL | assert_eq!(0, 0);
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
8+
19
error[E0433]: failed to resolve: use of undeclared type or module `Vec`
210
--> $DIR/no_implicit_prelude.rs:11:9
311
|
@@ -7,14 +15,6 @@ LL | fn f() { ::bar::m!(); }
715
LL | Vec::new();
816
| ^^^ use of undeclared type or module `Vec`
917

10-
error: cannot find macro `panic!` in this scope
11-
--> $DIR/no_implicit_prelude.rs:16:9
12-
|
13-
LL | assert_eq!(0, 0);
14-
| ^^^^^^^^^^^^^^^^^
15-
|
16-
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
17-
1818
error[E0599]: no method named `clone` found for type `()` in the current scope
1919
--> $DIR/no_implicit_prelude.rs:12:12
2020
|

src/test/ui/imports/local-modularized-tricky-fail-1.stderr

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,6 @@ LL | use inner1::*;
2121
| ^^^^^^^^^
2222
= help: consider adding an explicit import of `exported` to disambiguate
2323

24-
error[E0659]: `include` is ambiguous (macro-expanded name vs less macro-expanded name from outer scope during import/macro resolution)
25-
--> $DIR/local-modularized-tricky-fail-1.rs:46:1
26-
|
27-
LL | include!();
28-
| ^^^^^^^ ambiguous name
29-
|
30-
= note: `include` could refer to a macro from prelude
31-
note: `include` could also refer to the macro defined here
32-
--> $DIR/local-modularized-tricky-fail-1.rs:17:5
33-
|
34-
LL | / macro_rules! include {
35-
LL | | () => ()
36-
LL | | }
37-
| |_____^
38-
...
39-
LL | define_include!();
40-
| ------------------ in this macro invocation
41-
= help: use `crate::include` to refer to this macro unambiguously
42-
4324
error[E0659]: `panic` is ambiguous (macro-expanded name vs less macro-expanded name from outer scope during import/macro resolution)
4425
--> $DIR/local-modularized-tricky-fail-1.rs:35:5
4526
|
@@ -59,6 +40,25 @@ LL | define_panic!();
5940
| ---------------- in this macro invocation
6041
= help: use `crate::panic` to refer to this macro unambiguously
6142

43+
error[E0659]: `include` is ambiguous (macro-expanded name vs less macro-expanded name from outer scope during import/macro resolution)
44+
--> $DIR/local-modularized-tricky-fail-1.rs:46:1
45+
|
46+
LL | include!();
47+
| ^^^^^^^ ambiguous name
48+
|
49+
= note: `include` could refer to a macro from prelude
50+
note: `include` could also refer to the macro defined here
51+
--> $DIR/local-modularized-tricky-fail-1.rs:17:5
52+
|
53+
LL | / macro_rules! include {
54+
LL | | () => ()
55+
LL | | }
56+
| |_____^
57+
...
58+
LL | define_include!();
59+
| ------------------ in this macro invocation
60+
= help: use `crate::include` to refer to this macro unambiguously
61+
6262
error: aborting due to 3 previous errors
6363

6464
For more information about this error, try `rustc --explain E0659`.

src/test/ui/imports/shadow_builtin_macros.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,6 @@ LL | use foo::*;
1313
= help: consider adding an explicit import of `panic` to disambiguate
1414
= help: or use `self::panic` to refer to this macro unambiguously
1515

16-
error[E0659]: `panic` is ambiguous (macro-expanded name vs less macro-expanded name from outer scope during import/macro resolution)
17-
--> $DIR/shadow_builtin_macros.rs:20:14
18-
|
19-
LL | fn f() { panic!(); }
20-
| ^^^^^ ambiguous name
21-
|
22-
= note: `panic` could refer to a macro from prelude
23-
note: `panic` could also refer to the macro imported here
24-
--> $DIR/shadow_builtin_macros.rs:19:26
25-
|
26-
LL | ::two_macros::m!(use foo::panic;);
27-
| ^^^^^^^^^^
28-
= help: use `self::panic` to refer to this macro unambiguously
29-
3016
error[E0659]: `panic` is ambiguous (macro-expanded name vs less macro-expanded name from outer scope during import/macro resolution)
3117
--> $DIR/shadow_builtin_macros.rs:33:5
3218
|
@@ -62,6 +48,20 @@ note: `n` could also refer to the macro imported here
6248
LL | #[macro_use(n)]
6349
| ^
6450

51+
error[E0659]: `panic` is ambiguous (macro-expanded name vs less macro-expanded name from outer scope during import/macro resolution)
52+
--> $DIR/shadow_builtin_macros.rs:20:14
53+
|
54+
LL | fn f() { panic!(); }
55+
| ^^^^^ ambiguous name
56+
|
57+
= note: `panic` could refer to a macro from prelude
58+
note: `panic` could also refer to the macro imported here
59+
--> $DIR/shadow_builtin_macros.rs:19:26
60+
|
61+
LL | ::two_macros::m!(use foo::panic;);
62+
| ^^^^^^^^^^
63+
= help: use `self::panic` to refer to this macro unambiguously
64+
6565
error: aborting due to 4 previous errors
6666

6767
For more information about this error, try `rustc --explain E0659`.

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
error: cannot find attribute macro `marco_use` in this scope
2-
--> $DIR/issue-49074.rs:3:3
3-
|
4-
LL | #[marco_use] // typo
5-
| ^^^^^^^^^ help: a built-in attribute with a similar name exists: `macro_use`
6-
71
error: cannot find macro `bar!` in this scope
82
--> $DIR/issue-49074.rs:12:4
93
|
@@ -12,5 +6,11 @@ LL | bar!();
126
|
137
= help: have you added the `#[macro_use]` on the module/import?
148

9+
error: cannot find attribute macro `marco_use` in this scope
10+
--> $DIR/issue-49074.rs:3:3
11+
|
12+
LL | #[marco_use] // typo
13+
| ^^^^^^^^^ help: a built-in attribute with a similar name exists: `macro_use`
14+
1515
error: aborting due to 2 previous errors
1616

src/test/ui/proc-macro/macro-namespace-reserved-2.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,6 @@ error: expected derive macro, found macro `crate::my_macro`
8888
LL | #[derive(crate::my_macro)]
8989
| ^^^^^^^^^^^^^^^ not a derive macro
9090

91-
error: cannot find attribute macro `my_macro` in this scope
92-
--> $DIR/macro-namespace-reserved-2.rs:38:3
93-
|
94-
LL | #[my_macro]
95-
| ^^^^^^^^
96-
97-
error: cannot find derive macro `my_macro` in this scope
98-
--> $DIR/macro-namespace-reserved-2.rs:48:10
99-
|
100-
LL | #[derive(my_macro)]
101-
| ^^^^^^^^
102-
10391
error: cannot find macro `my_macro_attr!` in this scope
10492
--> $DIR/macro-namespace-reserved-2.rs:28:5
10593
|
@@ -112,5 +100,17 @@ error: cannot find macro `MyTrait!` in this scope
112100
LL | MyTrait!();
113101
| ^^^^^^^
114102

103+
error: cannot find attribute macro `my_macro` in this scope
104+
--> $DIR/macro-namespace-reserved-2.rs:38:3
105+
|
106+
LL | #[my_macro]
107+
| ^^^^^^^^
108+
109+
error: cannot find derive macro `my_macro` in this scope
110+
--> $DIR/macro-namespace-reserved-2.rs:48:10
111+
|
112+
LL | #[derive(my_macro)]
113+
| ^^^^^^^^
114+
115115
error: aborting due to 19 previous errors
116116

src/test/ui/reserved/reserved-attr-on-macro.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ LL | #[rustc_attribute_should_be_reserved]
77
= note: for more information, see https://github.com/rust-lang/rust/issues/29642
88
= help: add `#![feature(rustc_attrs)]` to the crate attributes to enable
99

10-
error: cannot find attribute macro `rustc_attribute_should_be_reserved` in this scope
11-
--> $DIR/reserved-attr-on-macro.rs:1:3
12-
|
13-
LL | #[rustc_attribute_should_be_reserved]
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15-
1610
error: cannot determine resolution for the macro `foo`
1711
--> $DIR/reserved-attr-on-macro.rs:10:5
1812
|
@@ -21,6 +15,12 @@ LL | foo!();
2115
|
2216
= note: import resolution is stuck, try simplifying macro imports
2317

18+
error: cannot find attribute macro `rustc_attribute_should_be_reserved` in this scope
19+
--> $DIR/reserved-attr-on-macro.rs:1:3
20+
|
21+
LL | #[rustc_attribute_should_be_reserved]
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
2424
error: aborting due to 3 previous errors
2525

2626
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)