Skip to content

Commit 3e0b541

Browse files
committed
WIP
1 parent a1b7009 commit 3e0b541

File tree

5 files changed

+70
-58
lines changed

5 files changed

+70
-58
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
}

compiler/rustc_resolve/src/macros.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use rustc_errors::{Applicability, StashKey};
1818
use rustc_expand::base::{Annotatable, DeriveResolution, Indeterminate, ResolverExpand};
1919
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
2020
use rustc_expand::compile_declarative_macro;
21-
use rustc_expand::expand::{AstFragment, Invocation, InvocationKind, SupportsMacroExpansion};
21+
use rustc_expand::expand::{
22+
AstFragment, AstFragmentKind, Invocation, InvocationKind, SupportsMacroExpansion,
23+
};
2224
use rustc_hir::def::{self, DefKind, Namespace, NonMacroAttrKind};
2325
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
2426
use rustc_middle::middle::stability;
@@ -247,6 +249,24 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
247249
force: bool,
248250
) -> Result<Lrc<SyntaxExtension>, Indeterminate> {
249251
let invoc_id = invoc.expansion_data.id;
252+
253+
if let Some(&(parent_def_id, _)) = self.invocation_parents.get(&invoc_id) {
254+
if invoc.fragment_kind == AstFragmentKind::Expr
255+
&& self.tcx.def_kind(parent_def_id) == DefKind::Mod
256+
{
257+
self.dcx().span_err(invoc.span(), "tralala");
258+
}
259+
} else if let Some(&(parent_def_id, _)) = self.invocation_parents.get(&eager_expansion_root)
260+
{
261+
if invoc.fragment_kind == AstFragmentKind::Expr
262+
&& self.tcx.def_kind(parent_def_id) == DefKind::Mod
263+
{
264+
self.dcx().span_err(invoc.span(), "trulala");
265+
}
266+
} else {
267+
// self.dcx().span_err(invoc.span(), "what?!");
268+
}
269+
250270
let parent_scope = match self.invocation_parent_scopes.get(&invoc_id) {
251271
Some(parent_scope) => *parent_scope,
252272
None => {

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

tests/ui/proc-macro/ambiguous-builtin-attrs.stderr

+15-15
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,6 @@ error[E0425]: cannot find value `NonExistent` in this scope
44
LL | NonExistent;
55
| ^^^^^^^^^^^ not found in this scope
66

7-
error[E0659]: `feature` is ambiguous
8-
--> $DIR/ambiguous-builtin-attrs.rs:3:4
9-
|
10-
LL | #![feature(decl_macro)]
11-
| ^^^^^^^ ambiguous name
12-
|
13-
= note: ambiguous because of a name conflict with a builtin attribute
14-
= note: `feature` could refer to a built-in attribute
15-
note: `feature` could also refer to the attribute macro imported here
16-
--> $DIR/ambiguous-builtin-attrs.rs:6:5
17-
|
18-
LL | use builtin_attrs::*;
19-
| ^^^^^^^^^^^^^^^^
20-
= help: use `crate::feature` to refer to this attribute macro unambiguously
21-
227
error[E0659]: `repr` is ambiguous
238
--> $DIR/ambiguous-builtin-attrs.rs:9:3
249
|
@@ -94,6 +79,21 @@ LL | use deny as allow;
9479
| ^^^^^^^^^^^^^
9580
= help: use `crate::allow` to refer to this built-in attribute unambiguously
9681

82+
error[E0659]: `feature` is ambiguous
83+
--> $DIR/ambiguous-builtin-attrs.rs:3:4
84+
|
85+
LL | #![feature(decl_macro)]
86+
| ^^^^^^^ ambiguous name
87+
|
88+
= note: ambiguous because of a name conflict with a builtin attribute
89+
= note: `feature` could refer to a built-in attribute
90+
note: `feature` could also refer to the attribute macro imported here
91+
--> $DIR/ambiguous-builtin-attrs.rs:6:5
92+
|
93+
LL | use builtin_attrs::*;
94+
| ^^^^^^^^^^^^^^^^
95+
= help: use `crate::feature` to refer to this attribute macro unambiguously
96+
9797
error[E0517]: attribute should be applied to a struct, enum, or union
9898
--> $DIR/ambiguous-builtin-attrs.rs:20:39
9999
|

0 commit comments

Comments
 (0)