Skip to content

Commit 33ad3c8

Browse files
Specify the list of allowed targets per attribute
1 parent 8227abe commit 33ad3c8

23 files changed

+348
-44
lines changed

compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs

Lines changed: 18 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,8 @@ 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 =
44+
AllowedTargets::AllowList(&[Allow(Target::Fn), Allow(Target::Impl { of_trait: true })]);
3545
const TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
3646

3747
fn extend<'c>(
@@ -53,6 +63,13 @@ impl<S: Stage> CombineAttributeParser<S> for AllowConstFnUnstableParser {
5363
type Item = Symbol;
5464
const CONVERT: ConvertFn<Self::Item> =
5565
|items, first_span| AttributeKind::AllowConstFnUnstable(items, first_span);
66+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
67+
Allow(Target::Fn),
68+
Allow(Target::Method(MethodKind::Inherent)),
69+
Allow(Target::Method(MethodKind::Trait { body: false })),
70+
Allow(Target::Method(MethodKind::Trait { body: true })),
71+
Allow(Target::Method(MethodKind::TraitImpl)),
72+
]);
5673
const TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
5774

5875
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: 75 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,16 @@ 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+
]);
6189
const TEMPLATE: AttributeTemplate = template!(OneOf: &[sym::off, sym::on]);
6290

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

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

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

236292
pub(crate) struct NoMangleParser;
237293
impl<S: Stage> NoArgsAttributeParser<S> for NoMangleParser {
238294
const PATH: &[Symbol] = &[sym::no_mangle];
239295
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
296+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowListWarnRest(&[
297+
Allow(Target::Fn),
298+
Allow(Target::Static),
299+
Allow(Target::Method(MethodKind::Inherent)),
300+
Allow(Target::Method(MethodKind::TraitImpl)),
301+
]);
240302
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoMangle;
241303
}
242304

@@ -310,6 +372,7 @@ impl<S: Stage> AttributeParser<S> for UsedParser {
310372
}
311373
},
312374
)];
375+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Static)]);
313376

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

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: 26 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,29 @@ 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+
Error(Target::WherePredicate),
64+
]);
4165
const TEMPLATE: AttributeTemplate = template!(
4266
Word,
4367
List: r#"/*opt*/ since = "version", /*opt*/ 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::{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::AllowAll;
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: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,33 @@
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+
Warn(Target::Method(MethodKind::Trait { body: false })),
29+
Warn(Target::ForeignFn),
30+
Warn(Target::Field),
31+
Warn(Target::MacroDef),
32+
Warn(Target::Arm),
33+
Warn(Target::AssocConst),
34+
]);
2135
const TEMPLATE: AttributeTemplate = template!(Word, List: "always|never");
2236

2337
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
@@ -59,6 +73,7 @@ impl<S: Stage> SingleAttributeParser<S> for RustcForceInlineParser {
5973
const PATH: &'static [Symbol] = &[sym::rustc_force_inline];
6074
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
6175
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
76+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
6277
const TEMPLATE: AttributeTemplate = template!(Word, List: "reason", NameValueStr: "reason");
6378

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

0 commit comments

Comments
 (0)