Skip to content

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

7 files changed

Lines changed: 23 additions & 62 deletions

File tree

‎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
constATTRIBUTE_ORDER:AttributeOrder = AttributeOrder::KeepOutermost;
4444
constON_DUPLICATE:OnDuplicate<S> = OnDuplicate::WarnButFutureError;
4545
constTEMPLATE:AttributeTemplate = template!(NameValueStr:"name");
46-
constTYPE: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-
constALLOWED_TARGETS:AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
46+
constALLOWED_TARGETS:AllowedTargets = AllowedTargets::CrateLevel;
5147

5248
fnconvert(cx:&mutAcceptContext<'_,'_,S>,args:&ArgParser<'_>) -> Option<AttributeKind>{
5349
letArgParser::NameValue(n) = args else{
@@ -76,11 +72,7 @@ impl<S: Stage> SingleAttributeParser<S> for RecursionLimitParser {
7672
constATTRIBUTE_ORDER:AttributeOrder = AttributeOrder::KeepOutermost;
7773
constON_DUPLICATE:OnDuplicate<S> = OnDuplicate::WarnButFutureError;
7874
constTEMPLATE:AttributeTemplate = template!(NameValueStr:"N","https://doc.rust-lang.org/reference/attributes/limits.html#the-recursion_limit-attribute");
79-
constTYPE: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-
constALLOWED_TARGETS:AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
75+
constALLOWED_TARGETS:AllowedTargets = AllowedTargets::CrateLevel;
8476

8577
fnconvert(cx:&mutAcceptContext<'_,'_,S>,args:&ArgParser<'_>) -> Option<AttributeKind>{
8678
letArgParser::NameValue(nv) = args else{
@@ -103,11 +95,7 @@ impl<S: Stage> SingleAttributeParser<S> for MoveSizeLimitParser {
10395
constATTRIBUTE_ORDER:AttributeOrder = AttributeOrder::KeepOutermost;
10496
constON_DUPLICATE:OnDuplicate<S> = OnDuplicate::Error;
10597
constTEMPLATE:AttributeTemplate = template!(NameValueStr:"N");
106-
constTYPE: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-
constALLOWED_TARGETS:AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
98+
constALLOWED_TARGETS:AllowedTargets = AllowedTargets::CrateLevel;
11199

112100
fnconvert(cx:&mutAcceptContext<'_,'_,S>,args:&ArgParser<'_>) -> Option<AttributeKind>{
113101
letArgParser::NameValue(nv) = args else{
@@ -130,11 +118,7 @@ impl<S: Stage> SingleAttributeParser<S> for TypeLengthLimitParser {
130118
constATTRIBUTE_ORDER:AttributeOrder = AttributeOrder::KeepOutermost;
131119
constON_DUPLICATE:OnDuplicate<S> = OnDuplicate::WarnButFutureError;
132120
constTEMPLATE:AttributeTemplate = template!(NameValueStr:"N");
133-
constTYPE: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-
constALLOWED_TARGETS:AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
121+
constALLOWED_TARGETS:AllowedTargets = AllowedTargets::CrateLevel;
138122

139123
fnconvert(cx:&mutAcceptContext<'_,'_,S>,args:&ArgParser<'_>) -> Option<AttributeKind>{
140124
letArgParser::NameValue(nv) = args else{
@@ -157,11 +141,7 @@ impl<S: Stage> SingleAttributeParser<S> for PatternComplexityLimitParser {
157141
constATTRIBUTE_ORDER:AttributeOrder = AttributeOrder::KeepOutermost;
158142
constON_DUPLICATE:OnDuplicate<S> = OnDuplicate::Error;
159143
constTEMPLATE:AttributeTemplate = template!(NameValueStr:"N");
160-
constTYPE: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-
constALLOWED_TARGETS:AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
144+
constALLOWED_TARGETS:AllowedTargets = AllowedTargets::CrateLevel;
165145

166146
fnconvert(cx:&mutAcceptContext<'_,'_,S>,args:&ArgParser<'_>) -> Option<AttributeKind>{
167147
letArgParser::NameValue(nv) = args else{
@@ -182,21 +162,15 @@ pub(crate) struct NoCoreParser;
182162
impl<S:Stage>NoArgsAttributeParser<S>forNoCoreParser{
183163
constPATH:&[Symbol] = &[sym::no_core];
184164
constON_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-
constALLOWED_TARGETS:AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
165+
constALLOWED_TARGETS:AllowedTargets = AllowedTargets::CrateLevel;
188166
constCREATE:fn(Span) -> AttributeKind = AttributeKind::NoCore;
189-
constTYPE:AttributeType = AttributeType::CrateLevel;
190167
}
191168

192169
pub(crate)structNoStdParser;
193170

194171
impl<S:Stage>NoArgsAttributeParser<S>forNoStdParser{
195172
constPATH:&[Symbol] = &[sym::no_std];
196173
constON_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-
constALLOWED_TARGETS:AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
174+
constALLOWED_TARGETS:AllowedTargets = AllowedTargets::CrateLevel;
200175
constCREATE:fn(Span) -> AttributeKind = AttributeKind::NoStd;
201-
constTYPE: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
constATTRIBUTES:AcceptMapping<Self,S>;
92-
9388
constALLOWED_TARGETS:AllowedTargets;
9489

95-
constTYPE: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
constTEMPLATE:AttributeTemplate;
137130

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

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

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

294281
fnconvert(cx:&mutAcceptContext<'_,'_,S>,args:&ArgParser<'_>) -> Option<AttributeKind>{
295282
ifletErr(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
constTEMPLATE:AttributeTemplate;
325312

326-
constTYPE:AttributeType = AttributeType::Normal;
327-
328313
/// Converts a single syntactical attribute to a number of elements of the semantic attribute, or [`AttributeKind`]
329314
fnextend<'c>(
330315
cx:&'cmutAcceptContext<'_,'_,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
constALLOWED_TARGETS:AllowedTargets = T::ALLOWED_TARGETS;
363-
constTYPE:AttributeType = T::TYPE;
364348

365349
fnfinalize(self,_cx:&FinalizeContext<'_,'_,S>) -> Option<AttributeKind>{
366350
ifletSome(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
usesuper::prelude::*;
64
usecrate::attributes::{
75
AttributeOrder,NoArgsAttributeParser,OnDuplicate,SingleAttributeParser,
@@ -155,8 +153,7 @@ pub(crate) struct CoherenceIsCoreParser;
155153
impl<S:Stage>NoArgsAttributeParser<S>forCoherenceIsCoreParser{
156154
constPATH:&[Symbol] = &[sym::rustc_coherence_is_core];
157155
constON_DUPLICATE:OnDuplicate<S> = OnDuplicate::Error;
158-
constALLOWED_TARGETS:AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
159-
constTYPE:AttributeType = AttributeType::CrateLevel;
156+
constALLOWED_TARGETS:AllowedTargets = AllowedTargets::CrateLevel;
160157
constCREATE: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
typeAcceptFn<S> =
@@ -133,7 +132,6 @@ macro_rules! attribute_parsers {
133132
})
134133
}),
135134
allowed_targets: <$names ascrate::attributes::AttributeParser<$stage>>::ALLOWED_TARGETS,
136-
attribute_type: <$names ascrate::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)enumAllowedTargets{
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)enumAllowedResult{
@@ -43,13 +48,15 @@ impl AllowedTargets {
4348
AllowedResult::Warn
4449
}
4550
}
51+
AllowedTargets::CrateLevel => AllowedResult::Allowed,
4652
}
4753
}
4854

4955
pub(crate)fnallowed_targets(&self) -> Vec<Target>{
5056
matchself{
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:&mutAcceptContext<'_,'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)fncheck_type(
112-
attribute_type:AttributeType,
121+
crate_level:bool,
113122
target:Target,
114123
cx:&mutAcceptContext<'_,'sess,S>,
115124
){
@@ -119,7 +128,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
119128
return;
120129
}
121130

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

0 commit comments

Comments
 (0)