Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ddaf123

Browse files
committed
Auto merge of #146420 - jdonszelmann:crate-level-into-allowed-targets, r=scrabsha
Crate level into allowed targets r? `@scrabsha` `@rustbot` blocked on #146389
2 parents 60a5372 + f56eb06 commit ddaf123

File tree

7 files changed

+23
-62
lines changed

7 files changed

+23
-62
lines changed

‎compiler/rustc_attr_parsing/src/attributes/crate_level.rs‎

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ impl<S: Stage> SingleAttributeParser<S> for CrateNameParser {
4343
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
4444
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
4545
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "name");
46-
const TYPE: AttributeType = AttributeType::CrateLevel;
47-
48-
// because it's a crate-level attribute, we already warn about it.
49-
// Putting target limitations here would give duplicate warnings
50-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
46+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
5147

5248
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
5349
let ArgParser::NameValue(n) = args else {
@@ -76,11 +72,7 @@ impl<S: Stage> SingleAttributeParser<S> for RecursionLimitParser {
7672
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
7773
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
7874
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "N", "https://doc.rust-lang.org/reference/attributes/limits.html#the-recursion_limit-attribute");
79-
const TYPE: AttributeType = AttributeType::CrateLevel;
80-
81-
// because it's a crate-level attribute, we already warn about it.
82-
// Putting target limitations here would give duplicate warnings
83-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
75+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
8476

8577
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
8678
let ArgParser::NameValue(nv) = args else {
@@ -103,11 +95,7 @@ impl<S: Stage> SingleAttributeParser<S> for MoveSizeLimitParser {
10395
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
10496
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
10597
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "N");
106-
const TYPE: AttributeType = AttributeType::CrateLevel;
107-
108-
// because it's a crate-level attribute, we already warn about it.
109-
// Putting target limitations here would give duplicate warnings
110-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
98+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
11199

112100
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
113101
let ArgParser::NameValue(nv) = args else {
@@ -130,11 +118,7 @@ impl<S: Stage> SingleAttributeParser<S> for TypeLengthLimitParser {
130118
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
131119
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
132120
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "N");
133-
const TYPE: AttributeType = AttributeType::CrateLevel;
134-
135-
// because it's a crate-level attribute, we already warn about it.
136-
// Putting target limitations here would give duplicate warnings
137-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
121+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
138122

139123
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
140124
let ArgParser::NameValue(nv) = args else {
@@ -157,11 +141,7 @@ impl<S: Stage> SingleAttributeParser<S> for PatternComplexityLimitParser {
157141
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
158142
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
159143
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "N");
160-
const TYPE: AttributeType = AttributeType::CrateLevel;
161-
162-
// because it's a crate-level attribute, we already warn about it.
163-
// Putting target limitations here would give duplicate warnings
164-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
144+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
165145

166146
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
167147
let ArgParser::NameValue(nv) = args else {
@@ -182,21 +162,15 @@ pub(crate) struct NoCoreParser;
182162
impl<S: Stage> NoArgsAttributeParser<S> for NoCoreParser {
183163
const PATH: &[Symbol] = &[sym::no_core];
184164
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
185-
// because it's a crate-level attribute, we already warn about it.
186-
// Putting target limitations here would give duplicate warnings
187-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
165+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
188166
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoCore;
189-
const TYPE: AttributeType = AttributeType::CrateLevel;
190167
}
191168

192169
pub(crate) struct NoStdParser;
193170

194171
impl<S: Stage> NoArgsAttributeParser<S> for NoStdParser {
195172
const PATH: &[Symbol] = &[sym::no_std];
196173
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
197-
// because it's a crate-level attribute, we already warn about it.
198-
// Putting target limitations here would give duplicate warnings
199-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
174+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
200175
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoStd;
201-
const TYPE: AttributeType = AttributeType::CrateLevel;
202176
}

‎compiler/rustc_attr_parsing/src/attributes/mod.rs‎

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@
1212
//! - [`CombineAttributeParser`](crate::attributes::CombineAttributeParser): makes it easy to implement an attribute which should combine the
1313
//! contents of attributes, if an attribute appear multiple times in a list
1414
//!
15-
//! By default, attributes are allowed anywhere. When adding an attribute that should only be used
16-
//! at the crate root, consider setting the `TYPE` in the parser trait to
17-
//! [`AttributeType::CrateLevel`](rustc_feature::AttributeType::CrateLevel).
18-
//!
1915
//! Attributes should be added to `crate::context::ATTRIBUTE_PARSERS` to be parsed.
2016
2117
use std::marker::PhantomData;
2218

23-
use rustc_feature::{AttributeTemplate, AttributeType,template};
19+
use rustc_feature::{AttributeTemplate, template};
2420
use rustc_hir::attrs::AttributeKind;
2521
use rustc_span::{Span, Symbol};
2622
use thin_vec::ThinVec;
@@ -89,11 +85,8 @@ pub(crate) trait AttributeParser<S: Stage>: Default + 'static {
8985
///
9086
/// If an attribute has this symbol, the `accept` function will be called on it.
9187
const ATTRIBUTES: AcceptMapping<Self, S>;
92-
9388
const ALLOWED_TARGETS: AllowedTargets;
9489

95-
const TYPE: AttributeType = AttributeType::Normal;
96-
9790
/// The parser has gotten a chance to accept the attributes on an item,
9891
/// here it can produce an attribute.
9992
///
@@ -135,8 +128,6 @@ pub(crate) trait SingleAttributeParser<S: Stage>: 'static {
135128
/// The template this attribute parser should implement. Used for diagnostics.
136129
const TEMPLATE: AttributeTemplate;
137130

138-
const TYPE: AttributeType = AttributeType::Normal;
139-
140131
/// Converts a single syntactical attribute to a single semantic attribute, or [`AttributeKind`]
141132
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind>;
142133
}
@@ -183,8 +174,6 @@ impl<T: SingleAttributeParser<S>, S: Stage> AttributeParser<S> for Single<T, S>
183174
)];
184175
const ALLOWED_TARGETS: AllowedTargets = T::ALLOWED_TARGETS;
185176

186-
const TYPE: AttributeType = T::TYPE;
187-
188177
fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
189178
Some(self.1?.0)
190179
}
@@ -269,7 +258,6 @@ pub(crate) trait NoArgsAttributeParser<S: Stage>: 'static {
269258
const PATH: &[Symbol];
270259
const ON_DUPLICATE: OnDuplicate<S>;
271260
const ALLOWED_TARGETS: AllowedTargets;
272-
const TYPE: AttributeType = AttributeType::Normal;
273261

274262
/// Create the [`AttributeKind`] given attribute's [`Span`].
275263
const CREATE: fn(Span) -> AttributeKind;
@@ -289,7 +277,6 @@ impl<T: NoArgsAttributeParser<S>, S: Stage> SingleAttributeParser<S> for Without
289277
const ON_DUPLICATE: OnDuplicate<S> = T::ON_DUPLICATE;
290278
const ALLOWED_TARGETS: AllowedTargets = T::ALLOWED_TARGETS;
291279
const TEMPLATE: AttributeTemplate = template!(Word);
292-
const TYPE: AttributeType = T::TYPE;
293280

294281
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
295282
if let Err(span) = args.no_args() {
@@ -323,8 +310,6 @@ pub(crate) trait CombineAttributeParser<S: Stage>: 'static {
323310
/// The template this attribute parser should implement. Used for diagnostics.
324311
const TEMPLATE: AttributeTemplate;
325312

326-
const TYPE: AttributeType = AttributeType::Normal;
327-
328313
/// Converts a single syntactical attribute to a number of elements of the semantic attribute, or [`AttributeKind`]
329314
fn extend<'c>(
330315
cx: &'c mut AcceptContext<'_, '_, S>,
@@ -360,7 +345,6 @@ impl<T: CombineAttributeParser<S>, S: Stage> AttributeParser<S> for Combine<T, S
360345
group.items.extend(T::extend(cx, args))
361346
})];
362347
const ALLOWED_TARGETS: AllowedTargets = T::ALLOWED_TARGETS;
363-
const TYPE: AttributeType = T::TYPE;
364348

365349
fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
366350
if let Some(first_span) = self.first_span {

‎compiler/rustc_attr_parsing/src/attributes/prelude.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// data structures
22
#[doc(hidden)]
3-
pub(super) use rustc_feature::{AttributeTemplate, AttributeType,template};
3+
pub(super) use rustc_feature::{AttributeTemplate, template};
44
#[doc(hidden)]
55
pub(super) use rustc_hir::attrs::AttributeKind;
66
#[doc(hidden)]

‎compiler/rustc_attr_parsing/src/attributes/traits.rs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::mem;
22

3-
use rustc_feature::AttributeType;
4-
53
use super::prelude::*;
64
use crate::attributes::{
75
AttributeOrder, NoArgsAttributeParser, OnDuplicate, SingleAttributeParser,
@@ -155,8 +153,7 @@ pub(crate) struct CoherenceIsCoreParser;
155153
impl<S: Stage> NoArgsAttributeParser<S> for CoherenceIsCoreParser {
156154
const PATH: &[Symbol] = &[sym::rustc_coherence_is_core];
157155
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
158-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
159-
const TYPE: AttributeType = AttributeType::CrateLevel;
156+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
160157
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::CoherenceIsCore;
161158
}
162159

‎compiler/rustc_attr_parsing/src/context.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::sync::LazyLock;
66
use private::Sealed;
77
use rustc_ast::{AttrStyle, CRATE_NODE_ID, MetaItemLit, NodeId};
88
use rustc_errors::{Diag, Diagnostic, Level};
9-
use rustc_feature::{AttributeTemplate,AttributeType};
9+
use rustc_feature::AttributeTemplate;
1010
use rustc_hir::attrs::AttributeKind;
1111
use rustc_hir::lints::{AttributeLint, AttributeLintKind};
1212
use rustc_hir::{AttrPath, CRATE_HIR_ID, HirId};
@@ -83,7 +83,6 @@ pub(super) struct GroupTypeInnerAccept<S: Stage> {
8383
pub(super) template: AttributeTemplate,
8484
pub(super) accept_fn: AcceptFn<S>,
8585
pub(super) allowed_targets: AllowedTargets,
86-
pub(super) attribute_type: AttributeType,
8786
}
8887

8988
type AcceptFn<S> =
@@ -133,7 +132,6 @@ macro_rules! attribute_parsers {
133132
})
134133
}),
135134
allowed_targets: <$names as crate::attributes::AttributeParser<$stage>>::ALLOWED_TARGETS,
136-
attribute_type: <$names as crate::attributes::AttributeParser<$stage>>::TYPE,
137135
});
138136
}
139137

‎compiler/rustc_attr_parsing/src/interface.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
272272

273273
(accept.accept_fn)(&mut cx, args);
274274
if !matches!(cx.stage.should_emit(), ShouldEmit::Nothing) {
275-
Self::check_type(accept.attribute_type, target, &mut cx);
276275
Self::check_target(&accept.allowed_targets, target, &mut cx);
277276
}
278277
}

‎compiler/rustc_attr_parsing/src/target_checking.rs‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::borrow::Cow;
22

33
use rustc_ast::AttrStyle;
44
use rustc_errors::DiagArgValue;
5-
use rustc_feature::{AttributeType,Features};
5+
use rustc_feature::Features;
66
use rustc_hir::lints::AttributeLintKind;
77
use rustc_hir::{MethodKind, Target};
88

@@ -14,6 +14,11 @@ use crate::session_diagnostics::InvalidTarget;
1414
pub(crate) enum AllowedTargets {
1515
AllowList(&'static [Policy]),
1616
AllowListWarnRest(&'static [Policy]),
17+
/// Special, and not the same as `AllowList(&[Allow(Target::Crate)])`.
18+
/// For crate-level attributes we emit a specific set of lints to warn
19+
/// people about accidentally not using them on the crate.
20+
/// Only use this for attributes that are *exclusively* valid at the crate level.
21+
CrateLevel,
1722
}
1823

1924
pub(crate) enum AllowedResult {
@@ -43,13 +48,15 @@ impl AllowedTargets {
4348
AllowedResult::Warn
4449
}
4550
}
51+
AllowedTargets::CrateLevel => AllowedResult::Allowed,
4652
}
4753
}
4854

4955
pub(crate) fn allowed_targets(&self) -> Vec<Target> {
5056
match self {
5157
AllowedTargets::AllowList(list) => list,
5258
AllowedTargets::AllowListWarnRest(list) => list,
59+
AllowedTargets::CrateLevel => ALL_TARGETS,
5360
}
5461
.iter()
5562
.filter_map(|target| match target {
@@ -74,6 +81,8 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
7481
target: Target,
7582
cx: &mut AcceptContext<'_, 'sess, S>,
7683
) {
84+
Self::check_type(matches!(allowed_targets, AllowedTargets::CrateLevel), target, cx);
85+
7786
match allowed_targets.is_allowed(target) {
7887
AllowedResult::Allowed => {}
7988
AllowedResult::Warn => {
@@ -109,7 +118,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
109118
}
110119

111120
pub(crate) fn check_type(
112-
attribute_type:AttributeType,
121+
crate_level:bool,
113122
target: Target,
114123
cx: &mut AcceptContext<'_, 'sess, S>,
115124
) {
@@ -119,7 +128,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
119128
return;
120129
}
121130

122-
if attribute_type != AttributeType::CrateLevel {
131+
if !crate_level {
123132
return;
124133
}
125134

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /