From 52653ef1c6594c73e8e1f059161614cbbeb4520a Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 19 Apr 2026 01:53:25 +0200 Subject: [PATCH] Remove `AttributeLintKind::InvalidTarget` variant --- compiler/rustc_attr_parsing/src/errors.rs | 20 +++++ .../rustc_attr_parsing/src/target_checking.rs | 24 ++++-- compiler/rustc_lint/src/early/diagnostics.rs | 15 +--- compiler/rustc_lint/src/lints.rs | 22 +---- compiler/rustc_lint_defs/src/lib.rs | 85 ++++--------------- 5 files changed, 53 insertions(+), 113 deletions(-) diff --git a/compiler/rustc_attr_parsing/src/errors.rs b/compiler/rustc_attr_parsing/src/errors.rs index 12c83548b6a2..c18caedca40e 100644 --- a/compiler/rustc_attr_parsing/src/errors.rs +++ b/compiler/rustc_attr_parsing/src/errors.rs @@ -131,3 +131,23 @@ pub(crate) struct EmptyAttributeList<'a> { pub attr_path: &'a str, pub valid_without_list: bool, } + +#[derive(Diagnostic)] +#[diag("`#[{$name}]` attribute cannot be used on {$target}")] +#[warning( + "this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!" +)] +#[help("`#[{$name}]` can {$only}be applied to {$applied}")] +pub(crate) struct InvalidTargetLint { + pub name: String, + pub target: &'static str, + pub applied: DiagArgValue, + pub only: &'static str, + #[suggestion( + "remove the attribute", + code = "", + applicability = "machine-applicable", + style = "tool-only" + )] + pub attr_span: Span, +} diff --git a/compiler/rustc_attr_parsing/src/target_checking.rs b/compiler/rustc_attr_parsing/src/target_checking.rs index 253a089e49f1..81143e30c252 100644 --- a/compiler/rustc_attr_parsing/src/target_checking.rs +++ b/compiler/rustc_attr_parsing/src/target_checking.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; use rustc_ast::AttrStyle; -use rustc_errors::{DiagArgValue, MultiSpan, StashKey}; +use rustc_errors::{DiagArgValue, Diagnostic, MultiSpan, StashKey}; use rustc_feature::Features; use rustc_hir::attrs::AttributeKind; use rustc_hir::lints::AttributeLintKind; @@ -11,7 +11,8 @@ use crate::AttributeParser; use crate::context::{AcceptContext, Stage}; use crate::errors::{ - InvalidAttrAtCrateLevel, ItemFollowingInnerAttr, UnsupportedAttributesInWhere, + InvalidAttrAtCrateLevel, InvalidTargetLint, ItemFollowingInnerAttr, + UnsupportedAttributesInWhere, }; use crate::session_diagnostics::InvalidTarget; use crate::target_checking::Policy::Allow; @@ -142,14 +143,19 @@ pub(crate) fn check_target( }; let attr_span = cx.attr_span; - cx.emit_lint( + cx.emit_dyn_lint( lint, - AttributeLintKind::InvalidTarget { - name: name.to_string(), - target: target.plural_name(), - only: if only { "only " } else { "" }, - applied, - attr_span, + move |dcx, level| { + InvalidTargetLint { + name: name.to_string(), + target: target.plural_name(), + only: if only { "only " } else { "" }, + applied: DiagArgValue::StrListSepByAnd( + applied.iter().map(|i| Cow::Owned(i.to_string())).collect(), + ), + attr_span, + } + .into_diag(dcx, level) }, attr_span, ); diff --git a/compiler/rustc_lint/src/early/diagnostics.rs b/compiler/rustc_lint/src/early/diagnostics.rs index d92be8bddf6c..f617a512e414 100644 --- a/compiler/rustc_lint/src/early/diagnostics.rs +++ b/compiler/rustc_lint/src/early/diagnostics.rs @@ -1,8 +1,7 @@ use std::any::Any; -use std::borrow::Cow; use rustc_data_structures::sync::DynSend; -use rustc_errors::{Applicability, Diag, DiagArgValue, DiagCtxtHandle, Diagnostic, Level}; +use rustc_errors::{Applicability, Diag, DiagCtxtHandle, Diagnostic, Level}; use rustc_hir::lints::{AttributeLintKind, FormatWarning}; use rustc_middle::ty::TyCtxt; use rustc_session::Session; @@ -35,18 +34,6 @@ pub struct DecorateAttrLint<'a, 'sess, 'tcx> { impl<'a> Diagnostic<'a, ()> for DecorateAttrLint<'_, '_, '_> { fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> { match self.diagnostic { - AttributeLintKind::InvalidTarget { name, target, applied, only, attr_span } => { - lints::InvalidTargetLint { - name: name.clone(), - target, - applied: DiagArgValue::StrListSepByAnd( - applied.into_iter().map(|i| Cow::Owned(i.to_string())).collect(), - ), - only, - attr_span: *attr_span, - } - .into_diag(dcx, level) - } &AttributeLintKind::InvalidStyle { ref name, is_used_as_inner, diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index 8bbc6ddf72cb..2bd43588c08b 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -6,7 +6,7 @@ use rustc_errors::codes::*; use rustc_errors::formatting::DiagMessageAddArg; use rustc_errors::{ - Applicability, Diag, DiagArgValue, DiagCtxtHandle, DiagMessage, DiagStyledString, Diagnostic, + Applicability, Diag, DiagCtxtHandle, DiagMessage, DiagStyledString, Diagnostic, EmissionGuarantee, Level, Subdiagnostic, SuggestionStyle, msg, }; use rustc_hir as hir; @@ -3282,26 +3282,6 @@ fn add_to_diag(self, diag: &mut Diag<'_, G>) { } } -#[derive(Diagnostic)] -#[diag("`#[{$name}]` attribute cannot be used on {$target}")] -#[warning( - "this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!" -)] -#[help("`#[{$name}]` can {$only}be applied to {$applied}")] -pub(crate) struct InvalidTargetLint { - pub name: String, - pub target: &'static str, - pub applied: DiagArgValue, - pub only: &'static str, - #[suggestion( - "remove the attribute", - code = "", - applicability = "machine-applicable", - style = "tool-only" - )] - pub attr_span: Span, -} - #[derive(Diagnostic)] #[diag( "{$is_used_as_inner -> diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index 24b195f07f80..8b86d1326e75 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -654,88 +654,35 @@ pub enum DeprecatedSinceKind { #[derive(Debug)] pub enum AttributeLintKind { - InvalidTarget { - name: String, - target: &'static str, - applied: Vec, - only: &'static str, - attr_span: Span, - }, - InvalidStyle { - name: String, - is_used_as_inner: bool, - target: &'static str, - target_span: Span, - }, + InvalidStyle { name: String, is_used_as_inner: bool, target: &'static str, target_span: Span }, UnexpectedCfgName((Symbol, Span), Option<(Symbol, Span)>), UnexpectedCfgValue((Symbol, Span), Option<(Symbol, Span)>), - DuplicateDocAlias { - first_definition: Span, - }, + DuplicateDocAlias { first_definition: Span }, DocAutoCfgExpectsHideOrShow, - DocAutoCfgHideShowUnexpectedItem { - attr_name: Symbol, - }, - DocAutoCfgHideShowExpectsList { - attr_name: Symbol, - }, + DocAutoCfgHideShowUnexpectedItem { attr_name: Symbol }, + DocAutoCfgHideShowExpectsList { attr_name: Symbol }, DocInvalid, AmbiguousDeriveHelpers, - DocUnknownInclude { - span: Span, - inner: &'static str, - value: Symbol, - }, - DocUnknownSpotlight { - span: Span, - }, - DocUnknownPasses { - name: Symbol, - span: Span, - }, - DocUnknownPlugins { - span: Span, - }, - DocUnknownAny { - name: Symbol, - }, + DocUnknownInclude { span: Span, inner: &'static str, value: Symbol }, + DocUnknownSpotlight { span: Span }, + DocUnknownPasses { name: Symbol, span: Span }, + DocUnknownPlugins { span: Span }, + DocUnknownAny { name: Symbol }, DocAutoCfgWrongLiteral, DocTestTakesList, - DocTestUnknown { - name: Symbol, - }, + DocTestUnknown { name: Symbol }, DocTestLiteral, AttrCrateLevelOnly, DoNotRecommendDoesNotExpectArgs, - CrateTypeUnknown { - span: Span, - suggested: Option, - }, + CrateTypeUnknown { span: Span, suggested: Option }, MalformedDoc, ExpectedNoArgs, ExpectedNameValue, - MalFormedDiagnosticAttribute { - attribute: &'static str, - options: &'static str, - span: Span, - }, - MalformedDiagnosticFormat { - warning: FormatWarning, - }, - DiagnosticWrappedParserError { - description: String, - label: String, - span: Span, - }, - IgnoredDiagnosticOption { - option_name: Symbol, - first_span: Span, - later_span: Span, - }, - MissingOptionsForDiagnosticAttribute { - attribute: &'static str, - options: &'static str, - }, + MalFormedDiagnosticAttribute { attribute: &'static str, options: &'static str, span: Span }, + MalformedDiagnosticFormat { warning: FormatWarning }, + DiagnosticWrappedParserError { description: String, label: String, span: Span }, + IgnoredDiagnosticOption { option_name: Symbol, first_span: Span, later_span: Span }, + MissingOptionsForDiagnosticAttribute { attribute: &'static str, options: &'static str }, NonMetaItemDiagnosticAttribute, }