Skip to content

Commit f543419

Browse files
Specify the list of allowed targets per attribute
1 parent 15b9255 commit f543419

23 files changed

+347
-44
lines changed

compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ 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};
8-
use crate::context::{AcceptContext, Stage};
9+
use crate::context::MaybeWarn::{Allow, Warn};
10+
use crate::context::{AcceptContext, AllowedTargets, Stage};
911
use crate::parser::ArgParser;
1012
use crate::session_diagnostics;
1113

@@ -15,6 +17,12 @@ impl<S: Stage> CombineAttributeParser<S> for AllowInternalUnstableParser {
1517
type Item = (Symbol, Span);
1618
const CONVERT: ConvertFn<Self::Item> =
1719
|items, span| AttributeKind::AllowInternalUnstable(items, span);
20+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
21+
Allow(Target::MacroDef),
22+
Allow(Target::Fn),
23+
Warn(Target::Field),
24+
Warn(Target::Arm),
25+
]);
1826
const TEMPLATE: AttributeTemplate = template!(Word, List: &["feat1, feat2, ..."]);
1927

2028
fn extend<'c>(
@@ -32,6 +40,11 @@ impl<S: Stage> CombineAttributeParser<S> for UnstableFeatureBoundParser {
3240
const PATH: &'static [rustc_span::Symbol] = &[sym::unstable_feature_bound];
3341
type Item = (Symbol, Span);
3442
const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::UnstableFeatureBound(items);
43+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
44+
Allow(Target::Fn),
45+
Allow(Target::Impl { of_trait: true }),
46+
Allow(Target::Trait),
47+
]);
3548
const TEMPLATE: AttributeTemplate = template!(Word, List: &["feat1, feat2, ..."]);
3649

3750
fn extend<'c>(
@@ -53,6 +66,13 @@ impl<S: Stage> CombineAttributeParser<S> for AllowConstFnUnstableParser {
5366
type Item = Symbol;
5467
const CONVERT: ConvertFn<Self::Item> =
5568
|items, first_span| AttributeKind::AllowConstFnUnstable(items, first_span);
69+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
70+
Allow(Target::Fn),
71+
Allow(Target::Method(MethodKind::Inherent)),
72+
Allow(Target::Method(MethodKind::Trait { body: false })),
73+
Allow(Target::Method(MethodKind::Trait { body: true })),
74+
Allow(Target::Method(MethodKind::TraitImpl)),
75+
]);
5676
const TEMPLATE: AttributeTemplate = template!(Word, List: &["feat1, feat2, ..."]);
5777

5878
fn extend<'c>(
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
//! Attributes that can be found in function body.
22
3+
use rustc_hir::Target;
34
use rustc_hir::attrs::AttributeKind;
45
use rustc_span::{Symbol, sym};
56

67
use super::{NoArgsAttributeParser, OnDuplicate};
7-
use crate::context::Stage;
8+
use crate::context::MaybeWarn::Allow;
9+
use crate::context::{AllowedTargets, Stage};
810

911
pub(crate) struct CoroutineParser;
1012

1113
impl<S: Stage> NoArgsAttributeParser<S> for CoroutineParser {
1214
const PATH: &[Symbol] = &[sym::coroutine];
1315
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
16+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Closure)]);
1417
const CREATE: fn(rustc_span::Span) -> AttributeKind = |span| AttributeKind::Coroutine(span);
1518
}

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
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

67
use super::{
78
AcceptMapping, AttributeOrder, AttributeParser, CombineAttributeParser, ConvertFn,
89
NoArgsAttributeParser, OnDuplicate, SingleAttributeParser,
910
};
10-
use crate::context::{AcceptContext, FinalizeContext, Stage};
11+
use crate::context::MaybeWarn::{Allow, Warn};
12+
use crate::context::{AcceptContext, AllowedTargets, FinalizeContext, Stage};
1113
use crate::parser::ArgParser;
1214
use crate::session_diagnostics::{NakedFunctionIncompatibleAttribute, NullOnExport};
1315

@@ -17,6 +19,13 @@ impl<S: Stage> SingleAttributeParser<S> for OptimizeParser {
1719
const PATH: &[Symbol] = &[sym::optimize];
1820
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
1921
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
22+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
23+
Allow(Target::Fn),
24+
Allow(Target::Closure),
25+
Allow(Target::Method(MethodKind::Trait { body: true })),
26+
Allow(Target::Method(MethodKind::TraitImpl)),
27+
Allow(Target::Method(MethodKind::Inherent)),
28+
]);
2029
const TEMPLATE: AttributeTemplate = template!(List: &["size", "speed", "none"]);
2130

2231
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
@@ -49,6 +58,15 @@ pub(crate) struct ColdParser;
4958
impl<S: Stage> NoArgsAttributeParser<S> for ColdParser {
5059
const PATH: &[Symbol] = &[sym::cold];
5160
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
61+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowListWarnRest(&[
62+
Allow(Target::Fn),
63+
Allow(Target::Method(MethodKind::Trait { body: true })),
64+
Allow(Target::Method(MethodKind::TraitImpl)),
65+
Allow(Target::Method(MethodKind::Trait { body: false })),
66+
Allow(Target::Method(MethodKind::Inherent)),
67+
Allow(Target::ForeignFn),
68+
Allow(Target::Closure),
69+
]);
5270
const CREATE: fn(Span) -> AttributeKind = AttributeKind::Cold;
5371
}
5472

@@ -58,6 +76,17 @@ impl<S: Stage> SingleAttributeParser<S> for CoverageParser {
5876
const PATH: &[Symbol] = &[sym::coverage];
5977
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
6078
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
79+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
80+
Allow(Target::Fn),
81+
Allow(Target::Closure),
82+
Allow(Target::Method(MethodKind::Trait { body: true })),
83+
Allow(Target::Method(MethodKind::TraitImpl)),
84+
Allow(Target::Method(MethodKind::Inherent)),
85+
Allow(Target::Impl { of_trait: true }),
86+
Allow(Target::Impl { of_trait: false }),
87+
Allow(Target::Mod),
88+
Allow(Target::Crate),
89+
]);
6190
const TEMPLATE: AttributeTemplate = template!(OneOf: &[sym::off, sym::on]);
6291

6392
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
@@ -97,6 +126,16 @@ impl<S: Stage> SingleAttributeParser<S> for ExportNameParser {
97126
const PATH: &[rustc_span::Symbol] = &[sym::export_name];
98127
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
99128
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
129+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
130+
Allow(Target::Static),
131+
Allow(Target::Fn),
132+
Allow(Target::Method(MethodKind::Inherent)),
133+
Allow(Target::Method(MethodKind::Trait { body: true })),
134+
Allow(Target::Method(MethodKind::TraitImpl)),
135+
Warn(Target::Field),
136+
Warn(Target::Arm),
137+
Warn(Target::MacroDef),
138+
]);
100139
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "name");
101140

102141
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
@@ -138,6 +177,12 @@ impl<S: Stage> AttributeParser<S> for NakedParser {
138177
this.span = Some(cx.attr_span);
139178
}
140179
})];
180+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
181+
Allow(Target::Fn),
182+
Allow(Target::Method(MethodKind::Inherent)),
183+
Allow(Target::Method(MethodKind::Trait { body: true })),
184+
Allow(Target::Method(MethodKind::TraitImpl)),
185+
]);
141186

142187
fn finalize(self, cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
143188
// FIXME(jdonszelmann): upgrade this list to *parsed* attributes
@@ -230,13 +275,31 @@ pub(crate) struct TrackCallerParser;
230275
impl<S: Stage> NoArgsAttributeParser<S> for TrackCallerParser {
231276
const PATH: &[Symbol] = &[sym::track_caller];
232277
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
278+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
279+
Allow(Target::Fn),
280+
Allow(Target::Method(MethodKind::Inherent)),
281+
Allow(Target::Method(MethodKind::Trait { body: true })),
282+
Allow(Target::Method(MethodKind::TraitImpl)),
283+
Allow(Target::Method(MethodKind::Trait { body: false })),
284+
Allow(Target::ForeignFn),
285+
Allow(Target::Closure),
286+
Warn(Target::MacroDef),
287+
Warn(Target::Arm),
288+
Warn(Target::Field),
289+
]);
233290
const CREATE: fn(Span) -> AttributeKind = AttributeKind::TrackCaller;
234291
}
235292

236293
pub(crate) struct NoMangleParser;
237294
impl<S: Stage> NoArgsAttributeParser<S> for NoMangleParser {
238295
const PATH: &[Symbol] = &[sym::no_mangle];
239296
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
297+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowListWarnRest(&[
298+
Allow(Target::Fn),
299+
Allow(Target::Static),
300+
Allow(Target::Method(MethodKind::Inherent)),
301+
Allow(Target::Method(MethodKind::TraitImpl)),
302+
]);
240303
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoMangle;
241304
}
242305

@@ -310,6 +373,7 @@ impl<S: Stage> AttributeParser<S> for UsedParser {
310373
}
311374
},
312375
)];
376+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Static)]);
313377

314378
fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
315379
// Ratcheting behaviour, if both `linker` and `compiler` are specified, use `linker`
@@ -373,4 +437,15 @@ impl<S: Stage> CombineAttributeParser<S> for TargetFeatureParser {
373437
}
374438
features
375439
}
440+
441+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
442+
Allow(Target::Fn),
443+
Allow(Target::Method(MethodKind::Inherent)),
444+
Allow(Target::Method(MethodKind::Trait { body: true })),
445+
Allow(Target::Method(MethodKind::TraitImpl)),
446+
Warn(Target::Statement),
447+
Warn(Target::Field),
448+
Warn(Target::Arm),
449+
Warn(Target::MacroDef),
450+
]);
376451
}

compiler/rustc_attr_parsing/src/attributes/confusables.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
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

67
use super::{AcceptMapping, AttributeParser};
7-
use crate::context::{FinalizeContext, Stage};
8+
use crate::context::MaybeWarn::Allow;
9+
use crate::context::{AllowedTargets, FinalizeContext, Stage};
810
use crate::session_diagnostics;
9-
1011
#[derive(Default)]
1112
pub(crate) struct ConfusablesParser {
1213
confusables: ThinVec<Symbol>,
@@ -41,6 +42,8 @@ impl<S: Stage> AttributeParser<S> for ConfusablesParser {
4142
this.first_span.get_or_insert(cx.attr_span);
4243
},
4344
)];
45+
const ALLOWED_TARGETS: AllowedTargets =
46+
AllowedTargets::AllowList(&[Allow(Target::Method(MethodKind::Inherent))]);
4447

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

compiler/rustc_attr_parsing/src/attributes/deprecation.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use rustc_feature::{AttributeTemplate, template};
22
use rustc_hir::attrs::{AttributeKind, DeprecatedSince, Deprecation};
3+
use rustc_hir::{MethodKind, Target};
34
use rustc_span::{Span, Symbol, sym};
45

56
use super::util::parse_version;
67
use super::{AttributeOrder, OnDuplicate, SingleAttributeParser};
7-
use crate::context::{AcceptContext, Stage};
8+
use crate::context::MaybeWarn::{Allow, Error};
9+
use crate::context::{AcceptContext, AllowedTargets, Stage};
810
use crate::parser::ArgParser;
911
use crate::session_diagnostics;
10-
1112
pub(crate) struct DeprecationParser;
1213

1314
fn get<S: Stage>(
@@ -38,6 +39,30 @@ 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: AllowedTargets = AllowedTargets::AllowListWarnRest(&[
43+
Allow(Target::Fn),
44+
Allow(Target::Mod),
45+
Allow(Target::Struct),
46+
Allow(Target::Enum),
47+
Allow(Target::Union),
48+
Allow(Target::Const),
49+
Allow(Target::Static),
50+
Allow(Target::MacroDef),
51+
Allow(Target::Method(MethodKind::Inherent)),
52+
Allow(Target::Method(MethodKind::Trait { body: false })),
53+
Allow(Target::Method(MethodKind::Trait { body: true })),
54+
Allow(Target::TyAlias),
55+
Allow(Target::Use),
56+
Allow(Target::ForeignFn),
57+
Allow(Target::Field),
58+
Allow(Target::Trait),
59+
Allow(Target::AssocTy),
60+
Allow(Target::AssocConst),
61+
Allow(Target::Variant),
62+
Allow(Target::Impl { of_trait: false }), //FIXME This does not make sense
63+
Allow(Target::Crate),
64+
Error(Target::WherePredicate),
65+
]);
4166
const TEMPLATE: AttributeTemplate = template!(
4267
Word,
4368
List: &[r#"since = "version""#, r#"note = "reason""#, r#"since = "version", note = "reason""#],

compiler/rustc_attr_parsing/src/attributes/dummy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use rustc_hir::attrs::AttributeKind;
33
use rustc_span::{Symbol, sym};
44

55
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
6-
use crate::context::{AcceptContext, Stage};
6+
use crate::context::{ALL_TARGETS, AcceptContext, AllowedTargets, Stage};
77
use crate::parser::ArgParser;
8-
98
pub(crate) struct DummyParser;
109
impl<S: Stage> SingleAttributeParser<S> for DummyParser {
1110
const PATH: &[Symbol] = &[sym::rustc_dummy];
1211
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
1312
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Ignore;
13+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
1414
const TEMPLATE: AttributeTemplate = template!(Word); // Anything, really
1515

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

compiler/rustc_attr_parsing/src/attributes/inline.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,34 @@
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};
1112
use crate::attributes::SingleAttributeParser;
12-
use crate::context::Stage;
13+
use crate::context::MaybeWarn::{Allow, Warn};
14+
use crate::context::{AllowedTargets, Stage};
1315
use crate::parser::ArgParser;
14-
1516
pub(crate) struct InlineParser;
1617

1718
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: AllowedTargets = AllowedTargets::AllowList(&[
23+
Allow(Target::Fn),
24+
Allow(Target::Method(MethodKind::Inherent)),
25+
Allow(Target::Method(MethodKind::Trait { body: true })),
26+
Allow(Target::Method(MethodKind::TraitImpl)),
27+
Allow(Target::Closure),
28+
Allow(Target::Delegation { mac: false }),
29+
Warn(Target::Method(MethodKind::Trait { body: false })),
30+
Warn(Target::ForeignFn),
31+
Warn(Target::Field),
32+
Warn(Target::MacroDef),
33+
Warn(Target::Arm),
34+
Warn(Target::AssocConst),
35+
]);
2136
const TEMPLATE: AttributeTemplate = template!(
2237
Word,
2338
List: &["always", "never"],
@@ -63,6 +78,7 @@ impl<S: Stage> SingleAttributeParser<S> for RustcForceInlineParser {
6378
const PATH: &'static [Symbol] = &[sym::rustc_force_inline];
6479
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
6580
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
81+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
6682
const TEMPLATE: AttributeTemplate = template!(Word, List: &["reason"], NameValueStr: "reason");
6783

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

0 commit comments

Comments
 (0)