Skip to content

Commit 3d73ec7

Browse files
Wip
Signed-off-by: Jonathan Brouwer <[email protected]>
1 parent 1cffc48 commit 3d73ec7

24 files changed

+208
-212
lines changed

compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ impl<S: Stage> CombineAttributeParser<S> for AllowInternalUnstableParser {
1616
type Item = (Symbol, Span);
1717
const CONVERT: ConvertFn<Self::Item> =
1818
|items, span| AttributeKind::AllowInternalUnstable(items, span);
19-
const ALLOWED_TARGETS: &'static [Target] = &[
20-
Target::MacroDef,
19+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
20+
Allow(Target::MacroDef),
2121
Target::Fn, //TODO if proc macro
22-
];
22+
]);
2323
const TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
2424

2525
fn extend<'c>(
@@ -37,7 +37,7 @@ impl<S: Stage> CombineAttributeParser<S> for UnstableFeatureBoundParser {
3737
const PATH: &'static [rustc_span::Symbol] = &[sym::unstable_feature_bound];
3838
type Item = (Symbol, Span);
3939
const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::UnstableFeatureBound(items);
40-
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn];
40+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn), Target::Impl {of_trait: true}]);
4141
const TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
4242

4343
fn extend<'c>(
@@ -59,12 +59,12 @@ impl<S: Stage> CombineAttributeParser<S> for AllowConstFnUnstableParser {
5959
type Item = Symbol;
6060
const CONVERT: ConvertFn<Self::Item> =
6161
|items, first_span| AttributeKind::AllowConstFnUnstable(items, first_span);
62-
const ALLOWED_TARGETS: &'static [Target] = &[
63-
Target::Fn,
64-
Target::Method(MethodKind::Inherent),
65-
Target::Method(MethodKind::Trait { body: false }),
66-
Target::Method(MethodKind::Trait { body: true }),
67-
];
62+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
63+
Allow(Target::Fn),
64+
Allow(Target::Method(MethodKind::Inherent)),
65+
Allow(Target::Method(MethodKind::Trait { body: false })),
66+
Allow(Target::Method(MethodKind::Trait { body: true })),
67+
]);
6868
const TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
6969

7070
fn extend<'c>(

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,32 @@ use rustc_hir::attrs::{AttributeKind, CoverageAttrKind, OptimizeAttr, UsedBy};
33
use rustc_hir::{MethodKind, Target};
44
use rustc_session::parse::feature_err;
55
use rustc_span::{Span, Symbol, sym};
6-
6+
use crate::context::AllowedTargets;
7+
use crate::context::MaybeWarn::Allow;
78
use super::{
89
AcceptMapping, AttributeOrder, AttributeParser, CombineAttributeParser, ConvertFn,
910
NoArgsAttributeParser, OnDuplicate, SingleAttributeParser,
1011
};
1112
use crate::context::{AcceptContext, FinalizeContext, Stage};
13+
use crate::context::AllowedTargets;
14+
use crate::context::MaybeWarn::Allow;
1215
use crate::parser::ArgParser;
1316
use crate::session_diagnostics::{NakedFunctionIncompatibleAttribute, NullOnExport};
17+
use crate::context::AllowedTargets;
18+
use crate::context::MaybeWarn::Allow;
1419

1520
pub(crate) struct OptimizeParser;
1621

1722
impl<S: Stage> SingleAttributeParser<S> for OptimizeParser {
1823
const PATH: &[Symbol] = &[sym::optimize];
1924
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
2025
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-
];
26+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
27+
Allow(Target::Fn),
28+
Allow(Target::Closure),
29+
Allow(Target::Method(MethodKind::Trait { body: true })),
30+
Allow(Target::Method(MethodKind::Inherent)),
31+
]);
2732
const TEMPLATE: AttributeTemplate = template!(List: "size|speed|none");
2833

2934
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
@@ -56,14 +61,14 @@ pub(crate) struct ColdParser;
5661
impl<S: Stage> NoArgsAttributeParser<S> for ColdParser {
5762
const PATH: &[Symbol] = &[sym::cold];
5863
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
59-
const ALLOWED_TARGETS: &'static [Target] = &[
60-
Target::Fn,
61-
Target::Method(MethodKind::Trait { body: true }),
62-
Target::Method(MethodKind::Trait { body: false }),
63-
Target::Method(MethodKind::Inherent),
64-
Target::ForeignFn,
65-
Target::Closure,
66-
];
64+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
65+
Allow(Target::Fn),
66+
Allow(Target::Method(MethodKind::Trait { body: true })),
67+
Allow(Target::Method(MethodKind::Trait { body: false })),
68+
Allow(Target::Method(MethodKind::Inherent)),
69+
Allow(Target::ForeignFn),
70+
Allow(Target::Closure),
71+
]);
6772
const CREATE: fn(Span) -> AttributeKind = AttributeKind::Cold;
6873
}
6974

@@ -73,15 +78,15 @@ impl<S: Stage> SingleAttributeParser<S> for CoverageParser {
7378
const PATH: &[Symbol] = &[sym::coverage];
7479
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
7580
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
76-
const ALLOWED_TARGETS: &'static [Target] = &[
77-
Target::Fn,
78-
Target::Closure,
79-
Target::Method(MethodKind::Trait { body: true }),
80-
Target::Method(MethodKind::Inherent),
81+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
82+
Allow(Target::Fn),
83+
Allow(Target::Closure),
84+
Allow(Target::Method(MethodKind::Trait { body: true })),
85+
Allow(Target::Method(MethodKind::Inherent)),
8186
Target::Impl { of_trait: true },
8287
Target::Impl { of_trait: false },
8388
Target::Mod,
84-
];
89+
]);
8590
const TEMPLATE: AttributeTemplate = template!(OneOf: &[sym::off, sym::on]);
8691

8792
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
@@ -121,8 +126,8 @@ impl<S: Stage> SingleAttributeParser<S> for ExportNameParser {
121126
const PATH: &[rustc_span::Symbol] = &[sym::export_name];
122127
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
123128
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
124-
const ALLOWED_TARGETS: &'static [Target] =
125-
&[Target::Static, Target::Fn, Target::Method(MethodKind::Inherent)];
129+
const ALLOWED_TARGETS: AllowedTargets =
130+
&[Target::Static, Target::Fn, Target::Method(MethodKind::Inherent), Target::Method(MethodKind::Trait { body: true })];
126131
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "name");
127132

128133
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
@@ -164,11 +169,11 @@ impl<S: Stage> AttributeParser<S> for NakedParser {
164169
this.span = Some(cx.attr_span);
165170
}
166171
})];
167-
const ALLOWED_TARGETS: &'static [Target] = &[
172+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
168173
Target::Fn,
169174
Target::Method(MethodKind::Inherent),
170175
Target::Method(MethodKind::Trait { body: true }),
171-
];
176+
]);
172177

173178
fn finalize(self, cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
174179
// FIXME(jdonszelmann): upgrade this list to *parsed* attributes
@@ -261,22 +266,22 @@ pub(crate) struct TrackCallerParser;
261266
impl<S: Stage> NoArgsAttributeParser<S> for TrackCallerParser {
262267
const PATH: &[Symbol] = &[sym::track_caller];
263268
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
264-
const ALLOWED_TARGETS: &'static [Target] = &[
269+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
265270
Target::Fn,
266271
Target::Method(MethodKind::Inherent),
267272
Target::Method(MethodKind::Trait { body: true }),
268273
Target::Method(MethodKind::Trait { body: false }),
269274
Target::ForeignFn,
270275
Target::Closure,
271-
];
276+
]);
272277
const CREATE: fn(Span) -> AttributeKind = AttributeKind::TrackCaller;
273278
}
274279

275280
pub(crate) struct NoMangleParser;
276281
impl<S: Stage> NoArgsAttributeParser<S> for NoMangleParser {
277282
const PATH: &[Symbol] = &[sym::no_mangle];
278283
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
279-
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn, Target::Static];
284+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Target::Fn, Target::Static, Target::Method(MethodKind::Inherent), Target::Method(MethodKind::Trait {body: true})]);
280285
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoMangle;
281286
}
282287

@@ -350,7 +355,7 @@ impl<S: Stage> AttributeParser<S> for UsedParser {
350355
}
351356
},
352357
)];
353-
const ALLOWED_TARGETS: &'static [Target] = &[Target::Static];
358+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Target::Static]);
354359

355360
fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
356361
// Ratcheting behaviour, if both `linker` and `compiler` are specified, use `linker`
@@ -415,5 +420,5 @@ impl<S: Stage> CombineAttributeParser<S> for TargetFeatureParser {
415420
features
416421
}
417422

418-
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn, Target::Method(MethodKind::Inherent)];
423+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Target::Fn, Target::Method(MethodKind::Inherent)]);
419424
}

compiler/rustc_attr_parsing/src/attributes/confusables.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use thin_vec::ThinVec;
77
use super::{AcceptMapping, AttributeParser};
88
use crate::context::{FinalizeContext, Stage};
99
use crate::session_diagnostics;
10-
10+
use crate::context::AllowedTargets;
11+
use crate::context::MaybeWarn::Allow;
1112
#[derive(Default)]
1213
pub(crate) struct ConfusablesParser {
1314
confusables: ThinVec<Symbol>,
@@ -42,7 +43,7 @@ impl<S: Stage> AttributeParser<S> for ConfusablesParser {
4243
this.first_span.get_or_insert(cx.attr_span);
4344
},
4445
)];
45-
const ALLOWED_TARGETS: &'static [Target] = &[Target::Method(MethodKind::Inherent)];
46+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Target::Method(MethodKind::Inherent)]);
4647

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

compiler/rustc_attr_parsing/src/attributes/deprecation.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use super::{AttributeOrder, OnDuplicate, SingleAttributeParser};
88
use crate::context::{AcceptContext, Stage};
99
use crate::parser::ArgParser;
1010
use crate::session_diagnostics;
11-
11+
use crate::context::AllowedTargets;
12+
use crate::context::MaybeWarn::Allow;
1213
pub(crate) struct DeprecationParser;
1314

1415
fn get<S: Stage>(
@@ -39,22 +40,24 @@ impl<S: Stage> SingleAttributeParser<S> for DeprecationParser {
3940
const PATH: &[Symbol] = &[sym::deprecated];
4041
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
4142
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-
Target::MacroDef,
51-
Target::Method(MethodKind::Inherent),
52-
Target::TyAlias,
53-
Target::Use,
54-
Target::ForeignFn,
55-
Target::Field,
56-
Target::Trait,
57-
];
43+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
44+
Allow(Target::Fn),
45+
Allow(Target::Mod),
46+
Allow(Target::Struct),
47+
Allow(Target::Enum),
48+
Allow(Target::Union),
49+
Allow(Target::Const),
50+
Allow(Target::Static),
51+
Allow(Target::MacroDef),
52+
Allow(Target::Method(MethodKind::Inherent)),
53+
Allow(Target::TyAlias),
54+
Allow(Target::Use),
55+
Allow(Target::ForeignFn),
56+
Allow(Target::Field),
57+
Allow(Target::Trait),
58+
Allow(Target::AssocTy),
59+
Target::Variant
60+
]);
5861
const TEMPLATE: AttributeTemplate = template!(
5962
Word,
6063
List: r#"/*opt*/ since = "version", /*opt*/ note = "reason""#,

compiler/rustc_attr_parsing/src/attributes/dummy.rs

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,17 @@
11
use rustc_feature::{AttributeTemplate, template};
22
use rustc_hir::attrs::AttributeKind;
3-
use rustc_hir::{MethodKind, Target};
43
use rustc_span::{Symbol, sym};
54

65
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
76
use crate::context::{AcceptContext, Stage};
87
use crate::parser::ArgParser;
9-
8+
use crate::context::AllowedTargets;
109
pub(crate) struct DummyParser;
1110
impl<S: Stage> SingleAttributeParser<S> for DummyParser {
1211
const PATH: &[Symbol] = &[sym::rustc_dummy];
1312
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
1413
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Ignore;
15-
const ALLOWED_TARGETS: &'static [Target] = &[
16-
//TODO better solution than listing everything here?
17-
Target::ExternCrate,
18-
Target::Use,
19-
Target::Static,
20-
Target::Const,
21-
Target::Fn,
22-
Target::Closure,
23-
Target::Mod,
24-
Target::ForeignMod,
25-
Target::GlobalAsm,
26-
Target::TyAlias,
27-
Target::Enum,
28-
Target::Variant,
29-
Target::Struct,
30-
Target::Field,
31-
Target::Union,
32-
Target::Trait,
33-
Target::TraitAlias,
34-
Target::Impl { of_trait: false },
35-
Target::Impl { of_trait: true },
36-
Target::Expression,
37-
Target::Statement,
38-
Target::Arm,
39-
Target::AssocConst,
40-
Target::Method(MethodKind::Inherent),
41-
Target::Method(MethodKind::Trait { body: false }),
42-
Target::Method(MethodKind::Trait { body: true }),
43-
Target::AssocTy,
44-
Target::ForeignFn,
45-
Target::ForeignStatic,
46-
Target::ForeignTy,
47-
Target::MacroDef,
48-
Target::Param,
49-
Target::PatField,
50-
Target::ExprField,
51-
Target::WherePredicate,
52-
Target::MacroCall,
53-
Target::Crate,
54-
];
14+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowAll;
5515
const TEMPLATE: AttributeTemplate = template!(Word); // Anything, really
5616

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

compiler/rustc_attr_parsing/src/attributes/inline.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,21 @@ use super::{AcceptContext, AttributeOrder, OnDuplicate};
1212
use crate::attributes::SingleAttributeParser;
1313
use crate::context::Stage;
1414
use crate::parser::ArgParser;
15-
15+
use crate::context::AllowedTargets;
16+
use crate::context::MaybeWarn::Allow;
1617
pub(crate) struct InlineParser;
1718

1819
impl<S: Stage> SingleAttributeParser<S> for InlineParser {
1920
const PATH: &'static [Symbol] = &[sym::inline];
2021
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
2122
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-
Target::Closure,
28-
];
23+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
24+
Allow(Target::Fn),
25+
Allow(Target::Method(MethodKind::Inherent)),
26+
Allow(Target::Method(MethodKind::Trait { body: false })),
27+
Allow(Target::Method(MethodKind::Trait { body: true })),
28+
Allow(Target::Closure),
29+
]);
2930
const TEMPLATE: AttributeTemplate = template!(Word, List: "always|never");
3031

3132
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
@@ -67,7 +68,7 @@ impl<S: Stage> SingleAttributeParser<S> for RustcForceInlineParser {
6768
const PATH: &'static [Symbol] = &[sym::rustc_force_inline];
6869
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
6970
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
70-
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn];
71+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Target::Fn]);
7172
const TEMPLATE: AttributeTemplate = template!(Word, List: "reason", NameValueStr: "reason");
7273

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

0 commit comments

Comments
 (0)