From d17ce1d6d546dc98d0f47edb920b224184693b85 Mon Sep 17 00:00:00 2001 From: Jonathan Brouwer Date: Fri, 29 May 2026 19:38:02 +0200 Subject: [PATCH 1/2] Don't pass `target` into `check_target` --- compiler/rustc_attr_parsing/src/interface.rs | 2 +- .../rustc_attr_parsing/src/target_checking.rs | 23 +++++++++++-------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_attr_parsing/src/interface.rs b/compiler/rustc_attr_parsing/src/interface.rs index 4a0ad63c0c053..2bde4d080312a 100644 --- a/compiler/rustc_attr_parsing/src/interface.rs +++ b/compiler/rustc_attr_parsing/src/interface.rs @@ -413,7 +413,7 @@ impl<'sess> AttributeParser<'sess> { finalizers.push(accept.finalizer); if !matches!(cx.should_emit, ShouldEmit::Nothing) { - Self::check_target(&accept.allowed_targets, target, &mut cx); + Self::check_target(&accept.allowed_targets, &mut cx); } } else { let attr = AttrItem { diff --git a/compiler/rustc_attr_parsing/src/target_checking.rs b/compiler/rustc_attr_parsing/src/target_checking.rs index fe4d72b83282b..c20faa918b78a 100644 --- a/compiler/rustc_attr_parsing/src/target_checking.rs +++ b/compiler/rustc_attr_parsing/src/target_checking.rs @@ -90,17 +90,16 @@ pub(crate) enum Policy { impl<'sess> AttributeParser<'sess> { pub(crate) fn check_target( allowed_targets: &AllowedTargets, - target: Target, cx: &mut AcceptContext<'_, 'sess>, ) { // For crate-level attributes we emit a specific set of lints to warn // people about accidentally not using them on the crate. if let &AllowedTargets::AllowList(&[Allow(Target::Crate)]) = allowed_targets { - Self::check_crate_level(target, cx); + Self::check_crate_level(cx); return; } - if matches!(cx.attr_path.segments.as_ref(), [sym::repr]) && target == Target::Crate { + if matches!(cx.attr_path.segments.as_ref(), [sym::repr]) && cx.target == Target::Crate { // The allowed targets of `repr` depend on its arguments. They can't be checked using // the `AttributeParser` code. let span = cx.attr_span; @@ -119,11 +118,12 @@ impl<'sess> AttributeParser<'sess> { .emit(); } - match allowed_targets.is_allowed(target) { + match allowed_targets.is_allowed(cx.target) { AllowedResult::Allowed => {} AllowedResult::Warn => { let allowed_targets = allowed_targets.allowed_targets(); - let (applied, only) = allowed_targets_applied(allowed_targets, target, cx.features); + let (applied, only) = + allowed_targets_applied(allowed_targets, cx.target, cx.features); let name = cx.attr_path.clone(); let lint = if name.segments[0] == sym::deprecated @@ -134,7 +134,7 @@ impl<'sess> AttributeParser<'sess> { Target::Arm, Target::MacroCall, ] - .contains(&target) + .contains(&cx.target) { rustc_session::lint::builtin::USELESS_DEPRECATED } else { @@ -142,6 +142,7 @@ impl<'sess> AttributeParser<'sess> { }; let attr_span = cx.attr_span; + let target = cx.target; cx.emit_lint_with_sess( lint, move |dcx, level, _| { @@ -161,12 +162,13 @@ impl<'sess> AttributeParser<'sess> { } AllowedResult::Error => { let allowed_targets = allowed_targets.allowed_targets(); - let (applied, only) = allowed_targets_applied(allowed_targets, target, cx.features); + let (applied, only) = + allowed_targets_applied(allowed_targets, cx.target, cx.features); let name = cx.attr_path.clone(); cx.dcx().emit_err(InvalidTarget { span: cx.attr_span.clone(), name, - target: target.plural_name(), + target: cx.target.plural_name(), only: if only { "only " } else { "" }, applied: DiagArgValue::StrListSepByAnd( applied.into_iter().map(Cow::Owned).collect(), @@ -176,8 +178,8 @@ impl<'sess> AttributeParser<'sess> { } } - pub(crate) fn check_crate_level(target: Target, cx: &mut AcceptContext<'_, 'sess>) { - if target == Target::Crate { + pub(crate) fn check_crate_level(cx: &mut AcceptContext<'_, 'sess>) { + if cx.target == Target::Crate { return; } @@ -200,6 +202,7 @@ impl<'sess> AttributeParser<'sess> { }) .unwrap_or_default(); + let target = cx.target; cx.emit_lint( rustc_session::lint::builtin::UNUSED_ATTRIBUTES, crate::errors::InvalidAttrStyle { From 4f2faec60f844c6bb199606e8416a20dec55a224 Mon Sep 17 00:00:00 2001 From: Jonathan Brouwer Date: Fri, 29 May 2026 19:51:58 +0200 Subject: [PATCH 2/2] Move `should_emit` check --- compiler/rustc_attr_parsing/src/interface.rs | 4 +--- compiler/rustc_attr_parsing/src/target_checking.rs | 6 +++++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_attr_parsing/src/interface.rs b/compiler/rustc_attr_parsing/src/interface.rs index 2bde4d080312a..eb49f108b0414 100644 --- a/compiler/rustc_attr_parsing/src/interface.rs +++ b/compiler/rustc_attr_parsing/src/interface.rs @@ -412,9 +412,7 @@ impl<'sess> AttributeParser<'sess> { (accept.accept_fn)(&mut cx, &args); finalizers.push(accept.finalizer); - if !matches!(cx.should_emit, ShouldEmit::Nothing) { - Self::check_target(&accept.allowed_targets, &mut cx); - } + Self::check_target(&accept.allowed_targets, &mut cx); } else { let attr = AttrItem { path: attr_path.clone(), diff --git a/compiler/rustc_attr_parsing/src/target_checking.rs b/compiler/rustc_attr_parsing/src/target_checking.rs index c20faa918b78a..db8650461769f 100644 --- a/compiler/rustc_attr_parsing/src/target_checking.rs +++ b/compiler/rustc_attr_parsing/src/target_checking.rs @@ -7,7 +7,6 @@ use rustc_hir::attrs::AttributeKind; use rustc_hir::{AttrItem, Attribute, MethodKind, Target}; use rustc_span::{BytePos, FileName, RemapPathScopeComponents, Span, Symbol, sym}; -use crate::AttributeParser; use crate::context::AcceptContext; use crate::errors::{ InvalidAttrAtCrateLevel, InvalidTargetLint, ItemFollowingInnerAttr, @@ -15,6 +14,7 @@ use crate::errors::{ }; use crate::session_diagnostics::InvalidTarget; use crate::target_checking::Policy::Allow; +use crate::{AttributeParser, ShouldEmit}; #[derive(Debug)] pub(crate) enum AllowedTargets { @@ -92,6 +92,10 @@ impl<'sess> AttributeParser<'sess> { allowed_targets: &AllowedTargets, cx: &mut AcceptContext<'_, 'sess>, ) { + if matches!(cx.should_emit, ShouldEmit::Nothing) { + return; + } + // For crate-level attributes we emit a specific set of lints to warn // people about accidentally not using them on the crate. if let &AllowedTargets::AllowList(&[Allow(Target::Crate)]) = allowed_targets {