Skip to content

Commit fe60f19

Browse files
committed
Ban custom inner attributes in expressions and statements
1 parent cb473c2 commit fe60f19

File tree

5 files changed

+240
-156
lines changed

5 files changed

+240
-156
lines changed

compiler/rustc_expand/src/expand.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -206,30 +206,36 @@ ast_fragments! {
206206
}
207207
}
208208

209+
pub enum SupportsMacroExpansion {
210+
No,
211+
Yes { supports_inner_attrs: bool },
212+
}
213+
209214
impl AstFragmentKind {
210215
crate fn dummy(self, span: Span) -> AstFragment {
211216
self.make_from(DummyResult::any(span)).expect("couldn't create a dummy AST fragment")
212217
}
213218

214-
/// Fragment supports macro expansion and not just inert attributes, `cfg` and `cfg_attr`.
215-
pub fn supports_macro_expansion(self) -> bool {
219+
pub fn supports_macro_expansion(self) -> SupportsMacroExpansion {
216220
match self {
217221
AstFragmentKind::OptExpr
218222
| AstFragmentKind::Expr
219-
| AstFragmentKind::Pat
220-
| AstFragmentKind::Ty
221223
| AstFragmentKind::Stmts
222-
| AstFragmentKind::Items
224+
| AstFragmentKind::Ty
225+
| AstFragmentKind::Pat => SupportsMacroExpansion::Yes { supports_inner_attrs: false },
226+
AstFragmentKind::Items
223227
| AstFragmentKind::TraitItems
224228
| AstFragmentKind::ImplItems
225-
| AstFragmentKind::ForeignItems => true,
229+
| AstFragmentKind::ForeignItems => {
230+
SupportsMacroExpansion::Yes { supports_inner_attrs: true }
231+
}
226232
AstFragmentKind::Arms
227233
| AstFragmentKind::Fields
228234
| AstFragmentKind::FieldPats
229235
| AstFragmentKind::GenericParams
230236
| AstFragmentKind::Params
231237
| AstFragmentKind::StructFields
232-
| AstFragmentKind::Variants => false,
238+
| AstFragmentKind::Variants => SupportsMacroExpansion::No,
233239
}
234240
}
235241

compiler/rustc_resolve/src/macros.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_errors::struct_span_err;
1717
use rustc_expand::base::Annotatable;
1818
use rustc_expand::base::{Indeterminate, ResolverExpand, SyntaxExtension, SyntaxExtensionKind};
1919
use rustc_expand::compile_declarative_macro;
20-
use rustc_expand::expand::{AstFragment, Invocation, InvocationKind};
20+
use rustc_expand::expand::{AstFragment, Invocation, InvocationKind, SupportsMacroExpansion};
2121
use rustc_feature::is_builtin_attr_name;
2222
use rustc_hir::def::{self, DefKind, NonMacroAttrKind};
2323
use rustc_hir::def_id;
@@ -278,12 +278,12 @@ impl<'a> ResolverExpand for Resolver<'a> {
278278

279279
// Derives are not included when `invocations` are collected, so we have to add them here.
280280
let parent_scope = &ParentScope { derives, ..parent_scope };
281-
let require_inert = !invoc.fragment_kind.supports_macro_expansion();
281+
let supports_macro_expansion = invoc.fragment_kind.supports_macro_expansion();
282282
let node_id = self.lint_node_id(eager_expansion_root);
283283
let (ext, res) = self.smart_resolve_macro_path(
284284
path,
285285
kind,
286-
require_inert,
286+
supports_macro_expansion,
287287
inner_attr,
288288
parent_scope,
289289
node_id,
@@ -457,7 +457,7 @@ impl<'a> Resolver<'a> {
457457
&mut self,
458458
path: &ast::Path,
459459
kind: MacroKind,
460-
require_inert: bool,
460+
supports_macro_expansion: SupportsMacroExpansion,
461461
inner_attr: bool,
462462
parent_scope: &ParentScope<'a>,
463463
node_id: NodeId,
@@ -505,8 +505,17 @@ impl<'a> Resolver<'a> {
505505

506506
let unexpected_res = if ext.macro_kind() != kind {
507507
Some((kind.article(), kind.descr_expected()))
508-
} else if require_inert && matches!(res, Res::Def(..)) {
509-
Some(("a", "non-macro attribute"))
508+
} else if matches!(res, Res::Def(..)) {
509+
match supports_macro_expansion {
510+
SupportsMacroExpansion::No => Some(("a", "non-macro attribute")),
511+
SupportsMacroExpansion::Yes { supports_inner_attrs } => {
512+
if inner_attr && !supports_inner_attrs {
513+
Some(("a", "non-macro inner attribute"))
514+
} else {
515+
None
516+
}
517+
}
518+
}
510519
} else {
511520
None
512521
};

src/test/ui/proc-macro/inner-attrs.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// check-pass
21
// compile-flags: -Z span-debug --error-format human
32
// aux-build:test-macros.rs
43

54
#![feature(custom_inner_attributes)]
65
#![feature(proc_macro_hygiene)]
76
#![feature(stmt_expr_attributes)]
7+
#![feature(rustc_attrs)]
88

99
#![no_std] // Don't load unnecessary hygiene information from std
1010
extern crate std;
@@ -25,17 +25,34 @@ struct MyStruct {
2525

2626
fn bar() {
2727
(#![print_target_and_args(fifth)] 1, 2);
28+
//~^ ERROR expected non-macro inner attribute, found attribute macro
29+
30+
#[print_target_and_args(tuple_attrs)] (
31+
#![cfg_attr(FALSE, rustc_dummy)]
32+
3, 4, {
33+
#![cfg_attr(not(FALSE), rustc_dummy(innermost))]
34+
5
35+
}
36+
);
37+
38+
#[print_target_and_args(array_attrs)] [
39+
#![rustc_dummy(inner)]
40+
true; 0
41+
];
2842

2943
[#![print_target_and_args(sixth)] 1 , 2];
44+
//~^ ERROR expected non-macro inner attribute, found attribute macro
3045
[#![print_target_and_args(seventh)] true ; 5];
31-
46+
//~^ ERROR expected non-macro inner attribute, found attribute macro
3247

3348
match 0 {
3449
#![print_target_and_args(eighth)]
50+
//~^ ERROR expected non-macro inner attribute, found attribute macro
3551
_ => {}
3652
}
3753

3854
MyStruct { #![print_target_and_args(ninth)] field: true };
55+
//~^ ERROR expected non-macro inner attribute, found attribute macro
3956
}
4057

4158
extern {
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: expected non-macro inner attribute, found attribute macro `print_target_and_args`
2+
--> $DIR/inner-attrs.rs:27:9
3+
|
4+
LL | (#![print_target_and_args(fifth)] 1, 2);
5+
| ^^^^^^^^^^^^^^^^^^^^^ not a non-macro inner attribute
6+
7+
error: expected non-macro inner attribute, found attribute macro `print_target_and_args`
8+
--> $DIR/inner-attrs.rs:43:9
9+
|
10+
LL | [#![print_target_and_args(sixth)] 1 , 2];
11+
| ^^^^^^^^^^^^^^^^^^^^^ not a non-macro inner attribute
12+
13+
error: expected non-macro inner attribute, found attribute macro `print_target_and_args`
14+
--> $DIR/inner-attrs.rs:45:9
15+
|
16+
LL | [#![print_target_and_args(seventh)] true ; 5];
17+
| ^^^^^^^^^^^^^^^^^^^^^ not a non-macro inner attribute
18+
19+
error: expected non-macro inner attribute, found attribute macro `print_target_and_args`
20+
--> $DIR/inner-attrs.rs:49:12
21+
|
22+
LL | #![print_target_and_args(eighth)]
23+
| ^^^^^^^^^^^^^^^^^^^^^ not a non-macro inner attribute
24+
25+
error: expected non-macro inner attribute, found attribute macro `print_target_and_args`
26+
--> $DIR/inner-attrs.rs:54:19
27+
|
28+
LL | MyStruct { #![print_target_and_args(ninth)] field: true };
29+
| ^^^^^^^^^^^^^^^^^^^^^ not a non-macro inner attribute
30+
31+
error: aborting due to 5 previous errors
32+

0 commit comments

Comments
 (0)