Skip to content

Commit d81c194

Browse files
committed
resolve/expand: Improve attribute expansion on macro definitions and calls
1 parent 46c35c7 commit d81c194

8 files changed

+63
-40
lines changed

compiler/rustc_expand/src/expand.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -1543,13 +1543,8 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
15431543
}
15441544

15451545
fn visit_item_kind(&mut self, item: &mut ast::ItemKind) {
1546-
match item {
1547-
ast::ItemKind::MacroDef(..) => {}
1548-
_ => {
1549-
self.cfg.configure_item_kind(item);
1550-
noop_visit_item_kind(item, self);
1551-
}
1552-
}
1546+
self.cfg.configure_item_kind(item);
1547+
noop_visit_item_kind(item, self);
15531548
}
15541549

15551550
fn flat_map_generic_param(

compiler/rustc_expand/src/placeholders.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,9 @@ impl<'a, 'b> MutVisitor for PlaceholderExpander<'a, 'b> {
258258

259259
fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
260260
match item.kind {
261-
ast::ItemKind::MacCall(_) => return self.remove(item.id).make_items(),
262-
ast::ItemKind::MacroDef(_) => return smallvec![item],
263-
_ => {}
261+
ast::ItemKind::MacCall(_) => self.remove(item.id).make_items(),
262+
_ => noop_flat_map_item(item, self),
264263
}
265-
266-
noop_flat_map_item(item, self)
267264
}
268265

269266
fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {

compiler/rustc_resolve/src/build_reduced_graph.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -1298,26 +1298,31 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
12981298
method!(visit_ty: ast::Ty, ast::TyKind::MacCall, walk_ty);
12991299

13001300
fn visit_item(&mut self, item: &'b Item) {
1301-
let macro_use = match item.kind {
1301+
let orig_module_scope = self.parent_scope.module;
1302+
self.parent_scope.macro_rules = match item.kind {
13021303
ItemKind::MacroDef(..) => {
1303-
self.parent_scope.macro_rules = self.define_macro(item);
1304-
return;
1304+
let macro_rules_scope = self.define_macro(item);
1305+
visit::walk_item(self, item);
1306+
macro_rules_scope
13051307
}
13061308
ItemKind::MacCall(..) => {
1307-
self.parent_scope.macro_rules = self.visit_invoc_in_module(item.id);
1308-
return;
1309+
let macro_rules_scope = self.visit_invoc_in_module(item.id);
1310+
visit::walk_item(self, item);
1311+
macro_rules_scope
1312+
}
1313+
_ => {
1314+
let orig_macro_rules_scope = self.parent_scope.macro_rules;
1315+
self.build_reduced_graph_for_item(item);
1316+
visit::walk_item(self, item);
1317+
match item.kind {
1318+
ItemKind::Mod(..) if self.contains_macro_use(&item.attrs) => {
1319+
self.parent_scope.macro_rules
1320+
}
1321+
_ => orig_macro_rules_scope,
1322+
}
13091323
}
1310-
ItemKind::Mod(..) => self.contains_macro_use(&item.attrs),
1311-
_ => false,
13121324
};
1313-
let orig_current_module = self.parent_scope.module;
1314-
let orig_current_macro_rules_scope = self.parent_scope.macro_rules;
1315-
self.build_reduced_graph_for_item(item);
1316-
visit::walk_item(self, item);
1317-
self.parent_scope.module = orig_current_module;
1318-
if !macro_use {
1319-
self.parent_scope.macro_rules = orig_current_macro_rules_scope;
1320-
}
1325+
self.parent_scope.module = orig_module_scope;
13211326
}
13221327

13231328
fn visit_stmt(&mut self, stmt: &'b ast::Stmt) {

compiler/rustc_resolve/src/def_collector.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> {
9191
DefPathData::ValueNs(i.ident.name)
9292
}
9393
ItemKind::MacroDef(..) => DefPathData::MacroNs(i.ident.name),
94-
ItemKind::MacCall(..) => return self.visit_macro_invoc(i.id),
94+
ItemKind::MacCall(..) => {
95+
visit::walk_item(self, i);
96+
return self.visit_macro_invoc(i.id);
97+
}
9598
ItemKind::GlobalAsm(..) => DefPathData::Misc,
9699
ItemKind::Use(..) => {
97100
return visit::walk_item(self, i);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(extended_key_value_attributes)]
2+
#![feature(rustc_attrs)]
3+
4+
#[rustc_dummy = stringify!(a)] // OK
5+
macro_rules! bar {
6+
() => {};
7+
}
8+
9+
// FIXME?: `bar` here expands before `stringify` has a chance to expand.
10+
// `#[rustc_dummy = ...]` is validated and dropped during expansion of `bar`,
11+
// the "unexpected token" errors comes from the validation.
12+
#[rustc_dummy = stringify!(b)] //~ ERROR unexpected token: `stringify!(b)`
13+
bar!();
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unexpected token: `stringify!(b)`
2+
--> $DIR/key-value-expansion-on-mac.rs:12:17
3+
|
4+
LL | #[rustc_dummy = stringify!(b)]
5+
| ^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+

src/test/ui/feature-gates/issue-43106-gating-of-builtin-attrs.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,6 @@ warning: unknown lint: `x5100`
172172
LL | #[deny(x5100)] impl S { }
173173
| ^^^^^
174174

175-
warning: `#[macro_escape]` is a deprecated synonym for `#[macro_use]`
176-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:438:1
177-
|
178-
LL | #[macro_escape]
179-
| ^^^^^^^^^^^^^^^
180-
181175
warning: `#[macro_escape]` is a deprecated synonym for `#[macro_use]`
182176
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:441:17
183177
|
@@ -186,6 +180,12 @@ LL | mod inner { #![macro_escape] }
186180
|
187181
= help: try an outer attribute: `#[macro_use]`
188182

183+
warning: `#[macro_escape]` is a deprecated synonym for `#[macro_use]`
184+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:438:1
185+
|
186+
LL | #[macro_escape]
187+
| ^^^^^^^^^^^^^^^
188+
189189
warning: use of deprecated attribute `plugin_registrar`: compiler plugins are deprecated. See https://github.com/rust-lang/rust/pull/64675
190190
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:228:17
191191
|

src/test/ui/feature-gates/issue-43106-gating-of-macro_use.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: arguments to `macro_use` are not allowed here
2-
--> $DIR/issue-43106-gating-of-macro_use.rs:6:1
2+
--> $DIR/issue-43106-gating-of-macro_use.rs:12:17
33
|
4-
LL | #![macro_use(my_macro)]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^
4+
LL | mod inner { #![macro_use(my_macro)] }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^
66

77
error: arguments to `macro_use` are not allowed here
88
--> $DIR/issue-43106-gating-of-macro_use.rs:9:1
@@ -11,10 +11,10 @@ LL | #[macro_use(my_macro)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^
1212

1313
error: arguments to `macro_use` are not allowed here
14-
--> $DIR/issue-43106-gating-of-macro_use.rs:12:17
14+
--> $DIR/issue-43106-gating-of-macro_use.rs:6:1
1515
|
16-
LL | mod inner { #![macro_use(my_macro)] }
17-
| ^^^^^^^^^^^^^^^^^^^^^^^
16+
LL | #![macro_use(my_macro)]
17+
| ^^^^^^^^^^^^^^^^^^^^^^^
1818

1919
error: malformed `macro_use` attribute input
2020
--> $DIR/issue-43106-gating-of-macro_use.rs:15:5

0 commit comments

Comments
 (0)