Skip to content

Commit cb297a1

Browse files
committed
WIP
1 parent 4546947 commit cb297a1

File tree

3 files changed

+34
-42
lines changed

3 files changed

+34
-42
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{Determinacy, ExternPreludeEntry, Finalize, Module, ModuleKind, Modul
1414
use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, ResolutionError};
1515
use crate::{Resolver, ResolverArenas, Segment, ToNameBinding, Used, VisResolutionError};
1616

17-
use rustc_ast::visit::{self, AssocCtxt, Visitor};
17+
use rustc_ast::visit::{self, AssocCtxt, Visitor, WalkItemKind};
1818
use rustc_ast::{self as ast, AssocItem, AssocItemKind, MetaItemKind, StmtKind};
1919
use rustc_ast::{Block, ForeignItem, ForeignItemKind, Impl, Item, ItemKind, NodeId};
2020
use rustc_attr as attr;
@@ -1312,7 +1312,17 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
13121312
_ => {
13131313
let orig_macro_rules_scope = self.parent_scope.macro_rules;
13141314
self.build_reduced_graph_for_item(item);
1315-
visit::walk_item(self, item);
1315+
match item.kind {
1316+
ItemKind::Mod(..) => {
1317+
// Visit attributes after items for backward compatibility.
1318+
// This way they can use `macro_rules` defined later.
1319+
self.visit_vis(&item.vis);
1320+
self.visit_ident(item.ident);
1321+
item.kind.walk(item, AssocCtxt::Trait, self);
1322+
visit::walk_list!(self, visit_attribute, &item.attrs);
1323+
}
1324+
_ => visit::walk_item(self, item),
1325+
}
13161326
match item.kind {
13171327
ItemKind::Mod(..) if self.contains_macro_use(&item.attrs) => {
13181328
self.parent_scope.macro_rules
@@ -1502,7 +1512,10 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
15021512
if krate.is_placeholder {
15031513
self.visit_invoc_in_module(krate.id);
15041514
} else {
1505-
visit::walk_crate(self, krate);
1515+
// Visit attributes after items for backward compatibility.
1516+
// This way they can use `macro_rules` defined later.
1517+
visit::walk_list!(self, visit_item, &krate.items);
1518+
visit::walk_list!(self, visit_attribute, &krate.attrs);
15061519
self.contains_macro_use(&krate.attrs);
15071520
}
15081521
}

tests/ui/attributes/key-value-expansion-scope.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#![doc = in_root!()] //~ ERROR cannot find macro `in_root` in this scope
1+
#![doc = in_root!()] // ERROR cannot find macro `in_root` in this scope
22
#![doc = in_mod!()] //~ ERROR cannot find macro `in_mod` in this scope
3-
#![doc = in_mod_escape!()] //~ ERROR cannot find macro `in_mod_escape` in this scope
3+
#![doc = in_mod_escape!()] // ERROR cannot find macro `in_mod_escape` in this scope
44
#![doc = in_block!()] //~ ERROR cannot find macro `in_block` in this scope
55

66
#[doc = in_root!()] //~ ERROR cannot find macro `in_root` in this scope
@@ -16,8 +16,9 @@ fn before() {
1616

1717
macro_rules! in_root { () => { "" } }
1818

19+
#[doc = in_mod!()]
1920
mod macros_stay {
20-
#![doc = in_mod!()] //~ ERROR cannot find macro `in_mod` in this scope
21+
#![doc = in_mod!()] // ERROR cannot find macro `in_mod` in this scope
2122

2223
macro_rules! in_mod { () => { "" } }
2324

@@ -28,8 +29,9 @@ mod macros_stay {
2829
}
2930

3031
#[macro_use]
32+
#[doc = in_mod_escape!()]
3133
mod macros_escape {
32-
#![doc = in_mod_escape!()] //~ ERROR cannot find macro `in_mod_escape` in this scope
34+
#![doc = in_mod_escape!()] // ERROR cannot find macro `in_mod_escape` in this scope
3335

3436
macro_rules! in_mod_escape { () => { "" } }
3537

@@ -39,8 +41,9 @@ mod macros_escape {
3941
}
4042
}
4143

44+
#[doc = in_block!()] //~ ERROR cannot find macro `in_block` in this scope
4245
fn block() {
43-
#![doc = in_block!()] //~ ERROR cannot find macro `in_block` in this scope
46+
#![doc = in_block!()] //~ ERROR cannot find macro `in_block` in this scope
4447

4548
macro_rules! in_block { () => { "" } }
4649

tests/ui/attributes/key-value-expansion-scope.stderr

+10-34
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
error: cannot find macro `in_root` in this scope
2-
--> $DIR/key-value-expansion-scope.rs:1:10
3-
|
4-
LL | #![doc = in_root!()]
5-
| ^^^^^^^
6-
|
7-
= help: have you added the `#[macro_use]` on the module/import?
8-
91
error: cannot find macro `in_mod` in this scope
102
--> $DIR/key-value-expansion-scope.rs:2:10
113
|
@@ -14,14 +6,6 @@ LL | #![doc = in_mod!()]
146
|
157
= help: have you added the `#[macro_use]` on the module/import?
168

17-
error: cannot find macro `in_mod_escape` in this scope
18-
--> $DIR/key-value-expansion-scope.rs:3:10
19-
|
20-
LL | #![doc = in_mod_escape!()]
21-
| ^^^^^^^^^^^^^
22-
|
23-
= help: have you added the `#[macro_use]` on the module/import?
24-
259
error: cannot find macro `in_block` in this scope
2610
--> $DIR/key-value-expansion-scope.rs:4:10
2711
|
@@ -94,61 +78,53 @@ LL | #![doc = in_block!()]
9478
|
9579
= help: have you added the `#[macro_use]` on the module/import?
9680

97-
error: cannot find macro `in_mod` in this scope
98-
--> $DIR/key-value-expansion-scope.rs:20:14
99-
|
100-
LL | #![doc = in_mod!()]
101-
| ^^^^^^
102-
|
103-
= help: have you added the `#[macro_use]` on the module/import?
104-
105-
error: cannot find macro `in_mod_escape` in this scope
106-
--> $DIR/key-value-expansion-scope.rs:32:14
81+
error: cannot find macro `in_block` in this scope
82+
--> $DIR/key-value-expansion-scope.rs:44:9
10783
|
108-
LL | #![doc = in_mod_escape!()]
109-
| ^^^^^^^^^^^^^
84+
LL | #[doc = in_block!()]
85+
| ^^^^^^^^
11086
|
11187
= help: have you added the `#[macro_use]` on the module/import?
11288

11389
error: cannot find macro `in_block` in this scope
114-
--> $DIR/key-value-expansion-scope.rs:43:14
90+
--> $DIR/key-value-expansion-scope.rs:46:14
11591
|
11692
LL | #![doc = in_block!()]
11793
| ^^^^^^^^
11894
|
11995
= help: have you added the `#[macro_use]` on the module/import?
12096

12197
error: cannot find macro `in_mod` in this scope
122-
--> $DIR/key-value-expansion-scope.rs:54:9
98+
--> $DIR/key-value-expansion-scope.rs:57:9
12399
|
124100
LL | #[doc = in_mod!()]
125101
| ^^^^^^
126102
|
127103
= help: have you added the `#[macro_use]` on the module/import?
128104

129105
error: cannot find macro `in_block` in this scope
130-
--> $DIR/key-value-expansion-scope.rs:56:9
106+
--> $DIR/key-value-expansion-scope.rs:59:9
131107
|
132108
LL | #[doc = in_block!()]
133109
| ^^^^^^^^
134110
|
135111
= help: have you added the `#[macro_use]` on the module/import?
136112

137113
error: cannot find macro `in_mod` in this scope
138-
--> $DIR/key-value-expansion-scope.rs:59:14
114+
--> $DIR/key-value-expansion-scope.rs:62:14
139115
|
140116
LL | #![doc = in_mod!()]
141117
| ^^^^^^
142118
|
143119
= help: have you added the `#[macro_use]` on the module/import?
144120

145121
error: cannot find macro `in_block` in this scope
146-
--> $DIR/key-value-expansion-scope.rs:61:14
122+
--> $DIR/key-value-expansion-scope.rs:64:14
147123
|
148124
LL | #![doc = in_block!()]
149125
| ^^^^^^^^
150126
|
151127
= help: have you added the `#[macro_use]` on the module/import?
152128

153-
error: aborting due to 19 previous errors
129+
error: aborting due to 16 previous errors
154130

0 commit comments

Comments
 (0)