Skip to content

Commit 82471b6

Browse files
Rollup merge of rust-lang#157120 - JonathanBrouwer:repr-target-checking, r=mejrs
Cleanups around attribute target checking This is in preparation for a new approach I'm trying for rust-lang#156569 So r? @mejrs
2 parents d1cd77e + 4f2faec commit 82471b6

2 files changed

Lines changed: 19 additions & 14 deletions

File tree

compiler/rustc_attr_parsing/src/interface.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,7 @@ impl<'sess> AttributeParser<'sess> {
412412
(accept.accept_fn)(&mut cx, &args);
413413
finalizers.push(accept.finalizer);
414414

415-
if !matches!(cx.should_emit, ShouldEmit::Nothing) {
416-
Self::check_target(&accept.allowed_targets, target, &mut cx);
417-
}
415+
Self::check_target(&accept.allowed_targets, &mut cx);
418416
} else {
419417
let attr = AttrItem {
420418
path: attr_path.clone(),

compiler/rustc_attr_parsing/src/target_checking.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ use rustc_hir::attrs::AttributeKind;
77
use rustc_hir::{AttrItem, Attribute, MethodKind, Target};
88
use rustc_span::{BytePos, FileName, RemapPathScopeComponents, Span, Symbol, sym};
99

10-
use crate::AttributeParser;
1110
use crate::context::AcceptContext;
1211
use crate::errors::{
1312
InvalidAttrAtCrateLevel, InvalidTargetLint, ItemFollowingInnerAttr,
1413
UnsupportedAttributesInWhere,
1514
};
1615
use crate::session_diagnostics::InvalidTarget;
1716
use crate::target_checking::Policy::Allow;
17+
use crate::{AttributeParser, ShouldEmit};
1818

1919
#[derive(Debug)]
2020
pub(crate) enum AllowedTargets {
@@ -90,17 +90,20 @@ pub(crate) enum Policy {
9090
impl<'sess> AttributeParser<'sess> {
9191
pub(crate) fn check_target(
9292
allowed_targets: &AllowedTargets,
93-
target: Target,
9493
cx: &mut AcceptContext<'_, 'sess>,
9594
) {
95+
if matches!(cx.should_emit, ShouldEmit::Nothing) {
96+
return;
97+
}
98+
9699
// For crate-level attributes we emit a specific set of lints to warn
97100
// people about accidentally not using them on the crate.
98101
if let &AllowedTargets::AllowList(&[Allow(Target::Crate)]) = allowed_targets {
99-
Self::check_crate_level(target, cx);
102+
Self::check_crate_level(cx);
100103
return;
101104
}
102105

103-
if matches!(cx.attr_path.segments.as_ref(), [sym::repr]) && target == Target::Crate {
106+
if matches!(cx.attr_path.segments.as_ref(), [sym::repr]) && cx.target == Target::Crate {
104107
// The allowed targets of `repr` depend on its arguments. They can't be checked using
105108
// the `AttributeParser` code.
106109
let span = cx.attr_span;
@@ -119,11 +122,12 @@ impl<'sess> AttributeParser<'sess> {
119122
.emit();
120123
}
121124

122-
match allowed_targets.is_allowed(target) {
125+
match allowed_targets.is_allowed(cx.target) {
123126
AllowedResult::Allowed => {}
124127
AllowedResult::Warn => {
125128
let allowed_targets = allowed_targets.allowed_targets();
126-
let (applied, only) = allowed_targets_applied(allowed_targets, target, cx.features);
129+
let (applied, only) =
130+
allowed_targets_applied(allowed_targets, cx.target, cx.features);
127131
let name = cx.attr_path.clone();
128132

129133
let lint = if name.segments[0] == sym::deprecated
@@ -134,14 +138,15 @@ impl<'sess> AttributeParser<'sess> {
134138
Target::Arm,
135139
Target::MacroCall,
136140
]
137-
.contains(&target)
141+
.contains(&cx.target)
138142
{
139143
rustc_session::lint::builtin::USELESS_DEPRECATED
140144
} else {
141145
rustc_session::lint::builtin::UNUSED_ATTRIBUTES
142146
};
143147

144148
let attr_span = cx.attr_span;
149+
let target = cx.target;
145150
cx.emit_lint_with_sess(
146151
lint,
147152
move |dcx, level, _| {
@@ -161,12 +166,13 @@ impl<'sess> AttributeParser<'sess> {
161166
}
162167
AllowedResult::Error => {
163168
let allowed_targets = allowed_targets.allowed_targets();
164-
let (applied, only) = allowed_targets_applied(allowed_targets, target, cx.features);
169+
let (applied, only) =
170+
allowed_targets_applied(allowed_targets, cx.target, cx.features);
165171
let name = cx.attr_path.clone();
166172
cx.dcx().emit_err(InvalidTarget {
167173
span: cx.attr_span.clone(),
168174
name,
169-
target: target.plural_name(),
175+
target: cx.target.plural_name(),
170176
only: if only { "only " } else { "" },
171177
applied: DiagArgValue::StrListSepByAnd(
172178
applied.into_iter().map(Cow::Owned).collect(),
@@ -176,8 +182,8 @@ impl<'sess> AttributeParser<'sess> {
176182
}
177183
}
178184

179-
pub(crate) fn check_crate_level(target: Target, cx: &mut AcceptContext<'_, 'sess>) {
180-
if target == Target::Crate {
185+
pub(crate) fn check_crate_level(cx: &mut AcceptContext<'_, 'sess>) {
186+
if cx.target == Target::Crate {
181187
return;
182188
}
183189

@@ -200,6 +206,7 @@ impl<'sess> AttributeParser<'sess> {
200206
})
201207
.unwrap_or_default();
202208

209+
let target = cx.target;
203210
cx.emit_lint(
204211
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
205212
crate::errors::InvalidAttrStyle {

0 commit comments

Comments
 (0)