Skip to content

Commit dd186c0

Browse files
Attempt at defining attr target lists
Signed-off-by: Jonathan Brouwer <[email protected]>
1 parent 9c2c275 commit dd186c0

23 files changed

+151
-8
lines changed

compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::iter;
22

33
use rustc_feature::{AttributeTemplate, template};
44
use rustc_hir::attrs::AttributeKind;
5+
use rustc_hir::{MethodKind, Target};
56
use rustc_span::{Span, Symbol, sym};
67

78
use super::{CombineAttributeParser, ConvertFn};
@@ -15,6 +16,7 @@ impl<S: Stage> CombineAttributeParser<S> for AllowInternalUnstableParser {
1516
type Item = (Symbol, Span);
1617
const CONVERT: ConvertFn<Self::Item> =
1718
|items, span| AttributeKind::AllowInternalUnstable(items, span);
19+
const ALLOWED_TARGETS: &'static [Target] = &[Target::MacroDef];
1820
const TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
1921

2022
fn extend<'c>(
@@ -32,6 +34,7 @@ impl<S: Stage> CombineAttributeParser<S> for UnstableFeatureBoundParser {
3234
const PATH: &'static [rustc_span::Symbol] = &[sym::unstable_feature_bound];
3335
type Item = (Symbol, Span);
3436
const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::UnstableFeatureBound(items);
37+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn];
3538
const TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
3639

3740
fn extend<'c>(
@@ -53,6 +56,12 @@ impl<S: Stage> CombineAttributeParser<S> for AllowConstFnUnstableParser {
5356
type Item = Symbol;
5457
const CONVERT: ConvertFn<Self::Item> =
5558
|items, first_span| AttributeKind::AllowConstFnUnstable(items, first_span);
59+
const ALLOWED_TARGETS: &'static [Target] = &[
60+
Target::Fn,
61+
Target::Method(MethodKind::Inherent),
62+
Target::Method(MethodKind::Trait { body: false }),
63+
Target::Method(MethodKind::Trait { body: true }),
64+
];
5665
const TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
5766

5867
fn extend<'c>(

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_feature::{AttributeTemplate, template};
22
use rustc_hir::attrs::{AttributeKind, CoverageAttrKind, OptimizeAttr, UsedBy};
3+
use rustc_hir::{MethodKind, Target};
34
use rustc_session::parse::feature_err;
45
use rustc_span::{Span, Symbol, sym};
56

@@ -17,6 +18,12 @@ impl<S: Stage> SingleAttributeParser<S> for OptimizeParser {
1718
const PATH: &[Symbol] = &[sym::optimize];
1819
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
1920
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
21+
const ALLOWED_TARGETS: &'static [Target] = &[
22+
Target::Fn,
23+
Target::Closure,
24+
Target::Method(MethodKind::Trait { body: true }),
25+
Target::Method(MethodKind::Inherent),
26+
];
2027
const TEMPLATE: AttributeTemplate = template!(List: "size|speed|none");
2128

2229
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
@@ -49,6 +56,7 @@ pub(crate) struct ColdParser;
4956
impl<S: Stage> NoArgsAttributeParser<S> for ColdParser {
5057
const PATH: &[Symbol] = &[sym::cold];
5158
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
59+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn];
5260
const CREATE: fn(Span) -> AttributeKind = AttributeKind::Cold;
5361
}
5462

@@ -58,6 +66,15 @@ impl<S: Stage> SingleAttributeParser<S> for CoverageParser {
5866
const PATH: &[Symbol] = &[sym::coverage];
5967
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
6068
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
69+
const ALLOWED_TARGETS: &'static [Target] = &[
70+
Target::Fn,
71+
Target::Closure,
72+
Target::Method(MethodKind::Trait { body: true }),
73+
Target::Method(MethodKind::Inherent),
74+
Target::Impl { of_trait: true },
75+
Target::Impl { of_trait: false },
76+
Target::Mod,
77+
];
6178
const TEMPLATE: AttributeTemplate = template!(OneOf: &[sym::off, sym::on]);
6279

6380
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
@@ -97,6 +114,8 @@ impl<S: Stage> SingleAttributeParser<S> for ExportNameParser {
97114
const PATH: &[rustc_span::Symbol] = &[sym::export_name];
98115
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
99116
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
117+
const ALLOWED_TARGETS: &'static [Target] =
118+
&[Target::Static, Target::Fn, Target::Method(MethodKind::Inherent)];
100119
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "name");
101120

102121
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
@@ -138,6 +157,7 @@ impl<S: Stage> AttributeParser<S> for NakedParser {
138157
this.span = Some(cx.attr_span);
139158
}
140159
})];
160+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn];
141161

142162
fn finalize(self, cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
143163
// FIXME(jdonszelmann): upgrade this list to *parsed* attributes
@@ -230,13 +250,15 @@ pub(crate) struct TrackCallerParser;
230250
impl<S: Stage> NoArgsAttributeParser<S> for TrackCallerParser {
231251
const PATH: &[Symbol] = &[sym::track_caller];
232252
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
253+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn, Target::Method(MethodKind::Inherent)];
233254
const CREATE: fn(Span) -> AttributeKind = AttributeKind::TrackCaller;
234255
}
235256

236257
pub(crate) struct NoMangleParser;
237258
impl<S: Stage> NoArgsAttributeParser<S> for NoMangleParser {
238259
const PATH: &[Symbol] = &[sym::no_mangle];
239260
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
261+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn];
240262
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoMangle;
241263
}
242264

@@ -310,6 +332,7 @@ impl<S: Stage> AttributeParser<S> for UsedParser {
310332
}
311333
},
312334
)];
335+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Static];
313336

314337
fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
315338
// Ratcheting behaviour, if both `linker` and `compiler` are specified, use `linker`
@@ -373,4 +396,6 @@ impl<S: Stage> CombineAttributeParser<S> for TargetFeatureParser {
373396
}
374397
features
375398
}
399+
400+
const ALLOWED_TARGETS: &'static [Target] = &[];
376401
}

compiler/rustc_attr_parsing/src/attributes/confusables.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_feature::template;
22
use rustc_hir::attrs::AttributeKind;
3+
use rustc_hir::{MethodKind, Target};
34
use rustc_span::{Span, Symbol, sym};
45
use thin_vec::ThinVec;
56

@@ -41,6 +42,11 @@ impl<S: Stage> AttributeParser<S> for ConfusablesParser {
4142
this.first_span.get_or_insert(cx.attr_span);
4243
},
4344
)];
45+
const ALLOWED_TARGETS: &'static [Target] = &[
46+
Target::Method(MethodKind::Inherent),
47+
Target::Method(MethodKind::Trait { body: false }),
48+
Target::Method(MethodKind::Trait { body: true }),
49+
];
4450

4551
fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
4652
if self.confusables.is_empty() {

compiler/rustc_attr_parsing/src/attributes/deprecation.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_feature::{AttributeTemplate, template};
2+
use rustc_hir::Target;
23
use rustc_hir::attrs::{AttributeKind, DeprecatedSince, Deprecation};
34
use rustc_span::{Span, Symbol, sym};
45

@@ -38,6 +39,15 @@ impl<S: Stage> SingleAttributeParser<S> for DeprecationParser {
3839
const PATH: &[Symbol] = &[sym::deprecated];
3940
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
4041
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
42+
const ALLOWED_TARGETS: &'static [Target] = &[
43+
Target::Fn,
44+
Target::Mod,
45+
Target::Struct,
46+
Target::Enum,
47+
Target::Union,
48+
Target::Const,
49+
Target::Static,
50+
];
4151
const TEMPLATE: AttributeTemplate = template!(
4252
Word,
4353
List: r#"/*opt*/ since = "version", /*opt*/ note = "reason""#,

compiler/rustc_attr_parsing/src/attributes/dummy.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_feature::{AttributeTemplate, template};
2+
use rustc_hir::Target;
23
use rustc_hir::attrs::AttributeKind;
34
use rustc_span::{Symbol, sym};
45

@@ -11,6 +12,7 @@ impl<S: Stage> SingleAttributeParser<S> for DummyParser {
1112
const PATH: &[Symbol] = &[sym::rustc_dummy];
1213
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
1314
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Ignore;
15+
const ALLOWED_TARGETS: &'static [Target] = &[];
1416
const TEMPLATE: AttributeTemplate = template!(Word); // Anything, really
1517

1618
fn convert(_: &mut AcceptContext<'_, '_, S>, _: &ArgParser<'_>) -> Option<AttributeKind> {

compiler/rustc_attr_parsing/src/attributes/inline.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use rustc_feature::{AttributeTemplate, template};
66
use rustc_hir::attrs::{AttributeKind, InlineAttr};
77
use rustc_hir::lints::AttributeLintKind;
8+
use rustc_hir::{MethodKind, Target};
89
use rustc_span::{Symbol, sym};
910

1011
use super::{AcceptContext, AttributeOrder, OnDuplicate};
@@ -18,6 +19,12 @@ impl<S: Stage> SingleAttributeParser<S> for InlineParser {
1819
const PATH: &'static [Symbol] = &[sym::inline];
1920
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
2021
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
22+
const ALLOWED_TARGETS: &'static [Target] = &[
23+
Target::Fn,
24+
Target::Method(MethodKind::Inherent),
25+
Target::Method(MethodKind::Trait { body: false }),
26+
Target::Method(MethodKind::Trait { body: true }),
27+
];
2128
const TEMPLATE: AttributeTemplate = template!(Word, List: "always|never");
2229

2330
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
@@ -59,6 +66,7 @@ impl<S: Stage> SingleAttributeParser<S> for RustcForceInlineParser {
5966
const PATH: &'static [Symbol] = &[sym::rustc_force_inline];
6067
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
6168
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
69+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn];
6270
const TEMPLATE: AttributeTemplate = template!(Word, List: "reason", NameValueStr: "reason");
6371

6472
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_feature::{AttributeTemplate, template};
2+
use rustc_hir::Target;
23
use rustc_hir::attrs::AttributeKind;
34
use rustc_hir::attrs::AttributeKind::{LinkName, LinkOrdinal, LinkSection};
45
use rustc_span::{Span, Symbol, sym};
@@ -16,6 +17,7 @@ impl<S: Stage> SingleAttributeParser<S> for LinkNameParser {
1617
const PATH: &[Symbol] = &[sym::link_name];
1718
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
1819
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
20+
const ALLOWED_TARGETS: &'static [Target] = &[Target::ForeignFn];
1921
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "name");
2022

2123
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
@@ -38,6 +40,7 @@ impl<S: Stage> SingleAttributeParser<S> for LinkSectionParser {
3840
const PATH: &[Symbol] = &[sym::link_section];
3941
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
4042
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
43+
const ALLOWED_TARGETS: &'static [Target] = &[Target::ForeignMod];
4144
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "name");
4245

4346
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
@@ -64,27 +67,31 @@ pub(crate) struct ExportStableParser;
6467
impl<S: Stage> NoArgsAttributeParser<S> for ExportStableParser {
6568
const PATH: &[Symbol] = &[sym::export_stable];
6669
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
70+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn];
6771
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::ExportStable;
6872
}
6973

7074
pub(crate) struct FfiConstParser;
7175
impl<S: Stage> NoArgsAttributeParser<S> for FfiConstParser {
7276
const PATH: &[Symbol] = &[sym::ffi_const];
7377
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
78+
const ALLOWED_TARGETS: &'static [Target] = &[Target::ForeignFn];
7479
const CREATE: fn(Span) -> AttributeKind = AttributeKind::FfiConst;
7580
}
7681

7782
pub(crate) struct FfiPureParser;
7883
impl<S: Stage> NoArgsAttributeParser<S> for FfiPureParser {
7984
const PATH: &[Symbol] = &[sym::ffi_pure];
8085
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
86+
const ALLOWED_TARGETS: &'static [Target] = &[Target::ForeignFn];
8187
const CREATE: fn(Span) -> AttributeKind = AttributeKind::FfiPure;
8288
}
8389

8490
pub(crate) struct StdInternalSymbolParser;
8591
impl<S: Stage> NoArgsAttributeParser<S> for StdInternalSymbolParser {
8692
const PATH: &[Symbol] = &[sym::rustc_std_internal_symbol];
8793
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
94+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn];
8895
const CREATE: fn(Span) -> AttributeKind = AttributeKind::StdInternalSymbol;
8996
}
9097

@@ -94,6 +101,7 @@ impl<S: Stage> SingleAttributeParser<S> for LinkOrdinalParser {
94101
const PATH: &[Symbol] = &[sym::link_ordinal];
95102
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
96103
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
104+
const ALLOWED_TARGETS: &'static [Target] = &[Target::ForeignFn, Target::ForeignStatic];
97105
const TEMPLATE: AttributeTemplate = template!(List: "ordinal");
98106

99107
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {

compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_hir::attrs::AttributeKind;
2+
use rustc_hir::{MethodKind, Target};
23
use rustc_span::{Span, Symbol, sym};
34

45
use crate::attributes::{NoArgsAttributeParser, OnDuplicate};
@@ -8,26 +9,35 @@ pub(crate) struct AsPtrParser;
89
impl<S: Stage> NoArgsAttributeParser<S> for AsPtrParser {
910
const PATH: &[Symbol] = &[sym::rustc_as_ptr];
1011
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
12+
const ALLOWED_TARGETS: &'static [Target] = &[
13+
Target::Fn,
14+
Target::Method(MethodKind::Inherent),
15+
Target::Method(MethodKind::Trait { body: false }),
16+
Target::Method(MethodKind::Trait { body: true }),
17+
];
1118
const CREATE: fn(Span) -> AttributeKind = AttributeKind::AsPtr;
1219
}
1320

1421
pub(crate) struct PubTransparentParser;
1522
impl<S: Stage> NoArgsAttributeParser<S> for PubTransparentParser {
1623
const PATH: &[Symbol] = &[sym::rustc_pub_transparent];
1724
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
25+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Struct];
1826
const CREATE: fn(Span) -> AttributeKind = AttributeKind::PubTransparent;
1927
}
2028

2129
pub(crate) struct PassByValueParser;
2230
impl<S: Stage> NoArgsAttributeParser<S> for PassByValueParser {
2331
const PATH: &[Symbol] = &[sym::rustc_pass_by_value];
2432
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
33+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Struct];
2534
const CREATE: fn(Span) -> AttributeKind = AttributeKind::PassByValue;
2635
}
2736

2837
pub(crate) struct AutomaticallyDerivedParser;
2938
impl<S: Stage> NoArgsAttributeParser<S> for AutomaticallyDerivedParser {
3039
const PATH: &[Symbol] = &[sym::automatically_derived];
3140
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
41+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Impl { of_trait: true }];
3242
const CREATE: fn(Span) -> AttributeKind = AttributeKind::AutomaticallyDerived;
3343
}

compiler/rustc_attr_parsing/src/attributes/loop_match.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_hir::Target;
12
use rustc_hir::attrs::AttributeKind;
23
use rustc_span::{Span, Symbol, sym};
34

@@ -8,12 +9,14 @@ pub(crate) struct LoopMatchParser;
89
impl<S: Stage> NoArgsAttributeParser<S> for LoopMatchParser {
910
const PATH: &[Symbol] = &[sym::loop_match];
1011
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
12+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Expression];
1113
const CREATE: fn(Span) -> AttributeKind = AttributeKind::LoopMatch;
1214
}
1315

1416
pub(crate) struct ConstContinueParser;
1517
impl<S: Stage> NoArgsAttributeParser<S> for ConstContinueParser {
1618
const PATH: &[Symbol] = &[sym::const_continue];
1719
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
20+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Expression];
1821
const CREATE: fn(Span) -> AttributeKind = AttributeKind::ConstContinue;
1922
}

compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_errors::DiagArgValue;
22
use rustc_feature::{AttributeTemplate, template};
3+
use rustc_hir::Target;
34
use rustc_hir::attrs::{AttributeKind, MacroUseArgs};
45
use rustc_span::{Span, Symbol, sym};
56
use thin_vec::ThinVec;
@@ -13,6 +14,7 @@ pub(crate) struct MacroEscapeParser;
1314
impl<S: Stage> NoArgsAttributeParser<S> for MacroEscapeParser {
1415
const PATH: &[Symbol] = &[sym::macro_escape];
1516
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
17+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Mod, Target::ExternCrate];
1618
const CREATE: fn(Span) -> AttributeKind = AttributeKind::MacroEscape;
1719
}
1820

@@ -108,6 +110,7 @@ impl<S: Stage> AttributeParser<S> for MacroUseParser {
108110
}
109111
},
110112
)];
113+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Mod, Target::ExternCrate];
111114

112115
fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
113116
Some(AttributeKind::MacroUse { span: self.first_span?, arguments: self.state })

0 commit comments

Comments
 (0)