Skip to content

Commit 521d3a1

Browse files
Auto merge of #145788 - JonathanBrouwer:proper-fix-for-macro-call-target, r=<try>
Fix attribute target checking for macro calls try-job: x86_64-mingw-1
2 parents 41a79f1 + fe81a79 commit 521d3a1

14 files changed

+337
-59
lines changed

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ impl<S: Stage> SingleAttributeParser<S> for ExportNameParser {
127127
Warn(Target::Field),
128128
Warn(Target::Arm),
129129
Warn(Target::MacroDef),
130+
Warn(Target::MacroCall),
130131
]);
131132
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "name");
132133

@@ -174,6 +175,7 @@ impl<S: Stage> AttributeParser<S> for NakedParser {
174175
Allow(Target::Method(MethodKind::Inherent)),
175176
Allow(Target::Method(MethodKind::Trait { body: true })),
176177
Allow(Target::Method(MethodKind::TraitImpl)),
178+
Warn(Target::MacroCall),
177179
]);
178180

179181
fn finalize(self, cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
@@ -278,6 +280,7 @@ impl<S: Stage> NoArgsAttributeParser<S> for TrackCallerParser {
278280
Warn(Target::MacroDef),
279281
Warn(Target::Arm),
280282
Warn(Target::Field),
283+
Warn(Target::MacroCall),
281284
]);
282285
const CREATE: fn(Span) -> AttributeKind = AttributeKind::TrackCaller;
283286
}
@@ -365,7 +368,8 @@ impl<S: Stage> AttributeParser<S> for UsedParser {
365368
}
366369
},
367370
)];
368-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Static)]);
371+
const ALLOWED_TARGETS: AllowedTargets =
372+
AllowedTargets::AllowList(&[Allow(Target::Static), Warn(Target::MacroCall)]);
369373

370374
fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
371375
// Ratcheting behaviour, if both `linker` and `compiler` are specified, use `linker`
@@ -450,6 +454,7 @@ impl<S: Stage> CombineAttributeParser<S> for TargetFeatureParser {
450454
Warn(Target::Field),
451455
Warn(Target::Arm),
452456
Warn(Target::MacroDef),
457+
Warn(Target::MacroCall),
453458
]);
454459
}
455460

compiler/rustc_attr_parsing/src/attributes/inline.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ impl<S: Stage> SingleAttributeParser<S> for InlineParser {
2525
Warn(Target::MacroDef),
2626
Warn(Target::Arm),
2727
Warn(Target::AssocConst),
28+
Warn(Target::MacroCall),
2829
]);
2930
const TEMPLATE: AttributeTemplate = template!(
3031
Word,

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,11 @@ impl<S: Stage> SingleAttributeParser<S> for LinkOrdinalParser {
110110
const PATH: &[Symbol] = &[sym::link_ordinal];
111111
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
112112
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
113-
const ALLOWED_TARGETS: AllowedTargets =
114-
AllowedTargets::AllowList(&[Allow(Target::ForeignFn), Allow(Target::ForeignStatic)]);
113+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
114+
Allow(Target::ForeignFn),
115+
Allow(Target::ForeignStatic),
116+
Warn(Target::MacroCall),
117+
]);
115118
const TEMPLATE: AttributeTemplate = template!(
116119
List: &["ordinal"],
117120
"https://doc.rust-lang.org/reference/items/external-blocks.html#the-link_ordinal-attribute"

compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ impl<S: Stage> NoArgsAttributeParser<S> for NonExhaustiveParser {
1919
Warn(Target::Field),
2020
Warn(Target::Arm),
2121
Warn(Target::MacroDef),
22+
Warn(Target::MacroCall),
2223
]);
2324
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NonExhaustive;
2425
}

compiler/rustc_attr_parsing/src/attributes/proc_macro_attrs.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
use super::prelude::*;
22

3+
const PROC_MACRO_ALLOWED_TARGETS: AllowedTargets =
4+
AllowedTargets::AllowList(&[Allow(Target::Fn), Warn(Target::Crate), Warn(Target::MacroCall)]);
5+
36
pub(crate) struct ProcMacroParser;
47
impl<S: Stage> NoArgsAttributeParser<S> for ProcMacroParser {
58
const PATH: &[Symbol] = &[sym::proc_macro];
69
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
7-
const ALLOWED_TARGETS: AllowedTargets =
8-
AllowedTargets::AllowList(&[Allow(Target::Fn), Warn(Target::Crate)]);
10+
const ALLOWED_TARGETS: AllowedTargets = PROC_MACRO_ALLOWED_TARGETS;
911
const CREATE: fn(Span) -> AttributeKind = AttributeKind::ProcMacro;
1012
}
1113

1214
pub(crate) struct ProcMacroAttributeParser;
1315
impl<S: Stage> NoArgsAttributeParser<S> for ProcMacroAttributeParser {
1416
const PATH: &[Symbol] = &[sym::proc_macro_attribute];
1517
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
16-
const ALLOWED_TARGETS: AllowedTargets =
17-
AllowedTargets::AllowList(&[Allow(Target::Fn), Warn(Target::Crate)]);
18+
const ALLOWED_TARGETS: AllowedTargets = PROC_MACRO_ALLOWED_TARGETS;
1819
const CREATE: fn(Span) -> AttributeKind = AttributeKind::ProcMacroAttribute;
1920
}
2021

@@ -23,8 +24,7 @@ impl<S: Stage> SingleAttributeParser<S> for ProcMacroDeriveParser {
2324
const PATH: &[Symbol] = &[sym::proc_macro_derive];
2425
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
2526
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
26-
const ALLOWED_TARGETS: AllowedTargets =
27-
AllowedTargets::AllowList(&[Allow(Target::Fn), Warn(Target::Crate)]);
27+
const ALLOWED_TARGETS: AllowedTargets = PROC_MACRO_ALLOWED_TARGETS;
2828
const TEMPLATE: AttributeTemplate = template!(
2929
List: &["TraitName", "TraitName, attributes(name1, name2, ...)"],
3030
"https://doc.rust-lang.org/reference/procedural-macros.html#derive-macros"

compiler/rustc_attr_parsing/src/lints.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ pub fn emit_attribute_lint<L: LintEmitter>(lint: &AttributeLint<L::Id>, lint_emi
4141
.emit_node_span_lint(
4242
// This check is here because `deprecated` had its own lint group and removing this would be a breaking change
4343
if name.segments[0].name == sym::deprecated
44-
&& ![Target::Closure, Target::Expression, Target::Statement, Target::Arm]
45-
.contains(target)
44+
&& ![
45+
Target::Closure,
46+
Target::Expression,
47+
Target::Statement,
48+
Target::Arm,
49+
Target::MacroCall,
50+
]
51+
.contains(target)
4652
{
4753
rustc_session::lint::builtin::USELESS_DEPRECATED
4854
} else {

compiler/rustc_expand/src/expand.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ use rustc_ast::mut_visit::*;
77
use rustc_ast::tokenstream::TokenStream;
88
use rustc_ast::visit::{self, AssocCtxt, Visitor, VisitorResult, try_visit, walk_list};
99
use rustc_ast::{
10-
self as ast, AssocItemKind, AstNodeWrapper, AttrArgs, AttrStyle, AttrVec, CRATE_NODE_ID,
11-
DUMMY_NODE_ID, ExprKind, ForeignItemKind, HasAttrs, HasNodeId, Inline, ItemKind, MacStmtStyle,
12-
MetaItemInner, MetaItemKind, ModKind, NodeId, PatKind, StmtKind, TyKind, token,
10+
self as ast, AssocItemKind, AstNodeWrapper, AttrArgs, AttrStyle, AttrVec, DUMMY_NODE_ID,
11+
ExprKind, ForeignItemKind, HasAttrs, HasNodeId, Inline, ItemKind, MacStmtStyle, MetaItemInner,
12+
MetaItemKind, ModKind, NodeId, PatKind, StmtKind, TyKind, token,
1313
};
1414
use rustc_ast_pretty::pprust;
15-
use rustc_attr_parsing::{AttributeParser, EvalConfigResult, ShouldEmit, validate_attr};
15+
use rustc_attr_parsing::{AttributeParser, Early, EvalConfigResult, ShouldEmit, validate_attr};
1616
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
1717
use rustc_data_structures::stack::ensure_sufficient_stack;
1818
use rustc_errors::PResult;
@@ -2165,7 +2165,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
21652165
None,
21662166
Target::MacroCall,
21672167
call.span(),
2168-
CRATE_NODE_ID,
2168+
self.cx.current_expansion.lint_node_id,
21692169
Some(self.cx.ecfg.features),
21702170
ShouldEmit::ErrorsAndLints,
21712171
);
@@ -2184,7 +2184,9 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
21842184
self.cx.current_expansion.lint_node_id,
21852185
BuiltinLintDiag::UnusedDocComment(attr.span),
21862186
);
2187-
} else if rustc_attr_parsing::is_builtin_attr(attr) {
2187+
} else if rustc_attr_parsing::is_builtin_attr(attr)
2188+
&& !AttributeParser::<Early>::is_parsed_attribute(&attr.path())
2189+
{
21882190
let attr_name = attr.ident().unwrap().name;
21892191
// `#[cfg]` and `#[cfg_attr]` are special - they are
21902192
// eagerly evaluated.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//@ check-pass
2+
// Regression test for https://github.com/rust-lang/rust/issues/145779
3+
#![warn(unused_attributes)]
4+
5+
fn main() {
6+
#[export_name = "x"]
7+
//~^ WARN attribute cannot be used on macro calls
8+
//~| WARN previously accepted
9+
#[unsafe(naked)]
10+
//~^ WARN attribute cannot be used on macro calls
11+
//~| WARN previously accepted
12+
#[track_caller]
13+
//~^ WARN attribute cannot be used on macro calls
14+
//~| WARN previously accepted
15+
#[used]
16+
//~^ WARN attribute cannot be used on macro calls
17+
//~| WARN previously accepted
18+
#[target_feature(enable = "x")]
19+
//~^ WARN attribute cannot be used on macro calls
20+
//~| WARN previously accepted
21+
#[deprecated]
22+
//~^ WARN attribute cannot be used on macro calls
23+
//~| WARN previously accepted
24+
#[inline]
25+
//~^ WARN attribute cannot be used on macro calls
26+
//~| WARN previously accepted
27+
#[link_name = "x"]
28+
//~^ WARN attribute cannot be used on macro calls
29+
//~| WARN previously accepted
30+
#[link_section = "x"]
31+
//~^ WARN attribute cannot be used on macro calls
32+
//~| WARN previously accepted
33+
#[link_ordinal(42)]
34+
//~^ WARN attribute cannot be used on macro calls
35+
//~| WARN previously accepted
36+
#[non_exhaustive]
37+
//~^ WARN attribute cannot be used on macro calls
38+
//~| WARN previously accepted
39+
#[proc_macro]
40+
//~^ WARN attribute cannot be used on macro calls
41+
//~| WARN previously accepted
42+
#[cold]
43+
//~^ WARN attribute cannot be used on macro calls
44+
//~| WARN previously accepted
45+
#[no_mangle]
46+
//~^ WARN attribute cannot be used on macro calls
47+
//~| WARN previously accepted
48+
#[deprecated]
49+
//~^ WARN attribute cannot be used on macro calls
50+
//~| WARN previously accepted
51+
#[automatically_derived]
52+
//~^ WARN attribute cannot be used on macro calls
53+
//~| WARN previously accepted
54+
#[macro_use]
55+
//~^ WARN attribute cannot be used on macro calls
56+
//~| WARN previously accepted
57+
#[must_use]
58+
//~^ WARN attribute cannot be used on macro calls
59+
//~| WARN previously accepted
60+
#[no_implicit_prelude]
61+
//~^ WARN attribute cannot be used on macro calls
62+
//~| WARN previously accepted
63+
#[path = ""]
64+
//~^ WARN attribute cannot be used on macro calls
65+
//~| WARN previously accepted
66+
#[ignore]
67+
//~^ WARN attribute cannot be used on macro calls
68+
//~| WARN previously accepted
69+
#[should_panic]
70+
//~^ WARN attribute cannot be used on macro calls
71+
//~| WARN previously accepted
72+
unreachable!();
73+
}

0 commit comments

Comments
 (0)