Skip to content

Commit f311092

Browse files
Remove special case for AllowedTargets::CrateLevel
1 parent 1b9ae9e commit f311092

File tree

3 files changed

+19
-41
lines changed

3 files changed

+19
-41
lines changed

compiler/rustc_attr_parsing/src/attributes/crate_level.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ impl<S: Stage> SingleAttributeParser<S> for CrateNameParser {
99
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
1010
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
1111
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "name");
12-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
12+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
1313

1414
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
1515
let ArgParser::NameValue(n) = args else {
@@ -33,7 +33,7 @@ impl<S: Stage> SingleAttributeParser<S> for RecursionLimitParser {
3333
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
3434
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
3535
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "N", "https://doc.rust-lang.org/reference/attributes/limits.html#the-recursion_limit-attribute");
36-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
36+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
3737

3838
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
3939
let ArgParser::NameValue(nv) = args else {
@@ -56,7 +56,7 @@ impl<S: Stage> SingleAttributeParser<S> for MoveSizeLimitParser {
5656
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
5757
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
5858
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "N");
59-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
59+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
6060

6161
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
6262
let ArgParser::NameValue(nv) = args else {
@@ -79,7 +79,7 @@ impl<S: Stage> SingleAttributeParser<S> for TypeLengthLimitParser {
7979
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
8080
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
8181
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "N");
82-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
82+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
8383

8484
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
8585
let ArgParser::NameValue(nv) = args else {
@@ -102,7 +102,7 @@ impl<S: Stage> SingleAttributeParser<S> for PatternComplexityLimitParser {
102102
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
103103
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
104104
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "N");
105-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
105+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
106106

107107
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
108108
let ArgParser::NameValue(nv) = args else {
@@ -123,7 +123,7 @@ pub(crate) struct NoCoreParser;
123123
impl<S: Stage> NoArgsAttributeParser<S> for NoCoreParser {
124124
const PATH: &[Symbol] = &[sym::no_core];
125125
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
126-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
126+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
127127
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoCore;
128128
}
129129

@@ -132,7 +132,7 @@ pub(crate) struct NoStdParser;
132132
impl<S: Stage> NoArgsAttributeParser<S> for NoStdParser {
133133
const PATH: &[Symbol] = &[sym::no_std];
134134
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
135-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
135+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
136136
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoStd;
137137
}
138138

@@ -141,7 +141,7 @@ pub(crate) struct RustcCoherenceIsCoreParser;
141141
impl<S: Stage> NoArgsAttributeParser<S> for RustcCoherenceIsCoreParser {
142142
const PATH: &[Symbol] = &[sym::rustc_coherence_is_core];
143143
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
144-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
144+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
145145
const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcCoherenceIsCore;
146146
}
147147

@@ -151,7 +151,7 @@ impl<S: Stage> SingleAttributeParser<S> for WindowsSubsystemParser {
151151
const PATH: &[Symbol] = &[sym::windows_subsystem];
152152
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
153153
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
154-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
154+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
155155
const TEMPLATE: AttributeTemplate = template!(NameValueStr: ["windows", "console"], "https://doc.rust-lang.org/reference/runtime.html#the-windows_subsystem-attribute");
156156

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

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use std::ops::{Deref, DerefMut};
44
use std::sync::LazyLock;
55

66
use private::Sealed;
7-
use rustc_ast::{AttrStyle, CRATE_NODE_ID, MetaItemLit, NodeId};
7+
use rustc_ast::{AttrStyle, MetaItemLit, NodeId};
88
use rustc_errors::{Diag, Diagnostic, Level};
99
use rustc_feature::{AttrSuggestionStyle, AttributeTemplate};
1010
use rustc_hir::attrs::AttributeKind;
1111
use rustc_hir::lints::{AttributeLint, AttributeLintKind};
12-
use rustc_hir::{AttrPath, CRATE_HIR_ID, HirId};
12+
use rustc_hir::{AttrPath, HirId};
1313
use rustc_session::Session;
1414
use rustc_session::lint::{Lint, LintId};
1515
use rustc_span::{ErrorGuaranteed, Span, Symbol};
@@ -303,8 +303,6 @@ pub trait Stage: Sized + 'static + Sealed {
303303
) -> ErrorGuaranteed;
304304

305305
fn should_emit(&self) -> ShouldEmit;
306-
307-
fn id_is_crate_root(id: Self::Id) -> bool;
308306
}
309307

310308
// allow because it's a sealed trait
@@ -326,10 +324,6 @@ impl Stage for Early {
326324
fn should_emit(&self) -> ShouldEmit {
327325
self.emit_errors
328326
}
329-
330-
fn id_is_crate_root(id: Self::Id) -> bool {
331-
id == CRATE_NODE_ID
332-
}
333327
}
334328

335329
// allow because it's a sealed trait
@@ -351,10 +345,6 @@ impl Stage for Late {
351345
fn should_emit(&self) -> ShouldEmit {
352346
ShouldEmit::ErrorsAndLints
353347
}
354-
355-
fn id_is_crate_root(id: Self::Id) -> bool {
356-
id == CRATE_HIR_ID
357-
}
358348
}
359349

360350
/// used when parsing attributes for miscellaneous things *before* ast lowering

compiler/rustc_attr_parsing/src/target_checking.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ use crate::session_diagnostics::InvalidTarget;
1515
pub(crate) enum AllowedTargets {
1616
AllowList(&'static [Policy]),
1717
AllowListWarnRest(&'static [Policy]),
18-
/// Special, and not the same as `AllowList(&[Allow(Target::Crate)])`.
19-
/// For crate-level attributes we emit a specific set of lints to warn
20-
/// people about accidentally not using them on the crate.
21-
/// Only use this for attributes that are *exclusively* valid at the crate level.
22-
CrateLevel,
2318
}
2419

2520
pub(crate) enum AllowedResult {
@@ -53,15 +48,13 @@ impl AllowedTargets {
5348
AllowedResult::Warn
5449
}
5550
}
56-
AllowedTargets::CrateLevel => AllowedResult::Allowed,
5751
}
5852
}
5953

6054
pub(crate) fn allowed_targets(&self) -> Vec<Target> {
6155
match self {
6256
AllowedTargets::AllowList(list) => list,
6357
AllowedTargets::AllowListWarnRest(list) => list,
64-
AllowedTargets::CrateLevel => ALL_TARGETS,
6558
}
6659
.iter()
6760
.filter_map(|target| match target {
@@ -95,7 +88,10 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
9588
target: Target,
9689
cx: &mut AcceptContext<'_, 'sess, S>,
9790
) {
98-
Self::check_type(matches!(allowed_targets, AllowedTargets::CrateLevel), target, cx);
91+
if allowed_targets.allowed_targets() == vec![Target::Crate] {
92+
Self::check_crate_level(target, cx);
93+
return;
94+
}
9995

10096
match allowed_targets.is_allowed(target) {
10197
AllowedResult::Allowed => {}
@@ -149,18 +145,10 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
149145
}
150146
}
151147

152-
pub(crate) fn check_type(
153-
crate_level: bool,
154-
target: Target,
155-
cx: &mut AcceptContext<'_, 'sess, S>,
156-
) {
157-
let is_crate_root = S::id_is_crate_root(cx.target_id);
158-
159-
if is_crate_root {
160-
return;
161-
}
162-
163-
if !crate_level {
148+
pub(crate) fn check_crate_level(target: Target, cx: &mut AcceptContext<'_, 'sess, S>) {
149+
// For crate-level attributes we emit a specific set of lints to warn
150+
// people about accidentally not using them on the crate.
151+
if target == Target::Crate {
164152
return;
165153
}
166154

0 commit comments

Comments
 (0)