Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions compiler/rustc_attr_parsing/src/attributes/deprecation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ use rustc_ast::LitKind;
use rustc_feature::AttributeStability;
use rustc_hir::VERSION_PLACEHOLDER;
use rustc_hir::attrs::{DeprecatedSince, Deprecation, RustcVersion};
use rustc_session::lint::builtin::UNUSED_ATTRIBUTES;

use super::prelude::*;
use super::util::parse_version;
use crate::session_diagnostics::{
DeprecatedItemSuggestion, InvalidSince, MissingNote, MissingSince,
DeprecatedAnnotationHasNoEffect, DeprecatedItemSuggestion, InvalidSince, MissingNote,
MissingSince,
};
use crate::target_checking::Policy::AllowSilent;

fn get(
cx: &mut AcceptContext<'_, '_>,
Expand Down Expand Up @@ -51,8 +54,12 @@ impl SingleAttributeParser for DeprecatedParser {
Allow(Target::ForeignTy),
Allow(Target::Field),
Allow(Target::Trait),
Allow(Target::AssocTy),
Allow(Target::AssocConst),
Allow(Target::AssocConst(AssocKind::Inherent)),
Allow(Target::AssocConst(AssocKind::Trait)),
AllowSilent(Target::AssocConst(AssocKind::TraitImpl)),
Allow(Target::AssocTy(AssocKind::Inherent)),
Allow(Target::AssocTy(AssocKind::Trait)),
AllowSilent(Target::AssocTy(AssocKind::TraitImpl)),
Allow(Target::Variant),
Allow(Target::Impl { of_trait: false }),
Allow(Target::Crate),
Expand Down Expand Up @@ -185,6 +192,22 @@ impl SingleAttributeParser for DeprecatedParser {
return None;
}

// `#[deprecated]` on trait-impl associated items has no effect (deprecation comes from the
// trait definition). Methods also get `useless_deprecated` from target checking.
if matches!(
cx.target,
Target::Method(MethodKind::TraitImpl)
| Target::AssocConst(AssocKind::TraitImpl)
| Target::AssocTy(AssocKind::TraitImpl)
) {
let attr_span = cx.attr_span;
cx.emit_lint(
UNUSED_ATTRIBUTES,
DeprecatedAnnotationHasNoEffect { span: attr_span },

@JonathanBrouwer JonathanBrouwer Aug 7, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be its own diagnostic rather than just the default Policy::Warn diagnostic?

View changes since the review

@JonathanBrouwer JonathanBrouwer Aug 7, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this path covered by any tests?

View changes since the review

attr_span,
);
}

Some(AttributeKind::Deprecated {
deprecation: Deprecation { since, note, suggestion },
span: cx.attr_span,
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_attr_parsing/src/attributes/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,12 +822,16 @@ impl AttributeParser for DocParser {
// Allow(Target::TraitAlias),
// Allow(Target::Impl { of_trait: true }),
// Allow(Target::Impl { of_trait: false }),
// Allow(Target::AssocConst),
// Allow(Target::AssocConst(AssocKind::Inherent)),
// Allow(Target::AssocConst(AssocKind::Trait)),
// Allow(Target::AssocConst(AssocKind::TraitImpl)),
// Allow(Target::Method(MethodKind::Inherent)),
// Allow(Target::Method(MethodKind::Trait { body: true })),
// Allow(Target::Method(MethodKind::Trait { body: false })),
// Allow(Target::Method(MethodKind::TraitImpl)),
// Allow(Target::AssocTy),
// Allow(Target::AssocTy(AssocKind::Inherent)),
// Allow(Target::AssocTy(AssocKind::Trait)),
// Allow(Target::AssocTy(AssocKind::TraitImpl)),
// Allow(Target::ForeignFn),
// Allow(Target::ForeignStatic),
// Allow(Target::ForeignTy),
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_attr_parsing/src/attributes/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ impl SingleAttributeParser for InlineParser {
Warn(Target::Field),
Warn(Target::MacroDef),
Warn(Target::Arm),
Warn(Target::AssocConst),
Warn(Target::AssocConst(AssocKind::Inherent)),
Warn(Target::AssocConst(AssocKind::Trait)),
Warn(Target::AssocConst(AssocKind::TraitImpl)),
Warn(Target::MacroCall),
]);
const TEMPLATE: AttributeTemplate = template!(
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_attr_parsing/src/attributes/link_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,9 @@ impl NoArgsAttributeParser for ExportStableParser {
Allow(Target::Enum),
Allow(Target::Union),
Allow(Target::TyAlias),
Allow(Target::AssocTy),
Allow(Target::AssocTy(AssocKind::Inherent)),
Allow(Target::AssocTy(AssocKind::Trait)),
Allow(Target::AssocTy(AssocKind::TraitImpl)),
Allow(Target::Use),
Allow(Target::Mod),
Allow(Target::Impl { of_trait: false }),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_attr_parsing/src/attributes/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#[doc(hidden)]
pub(super) use rustc_hir::attrs::AttributeKind;
#[doc(hidden)]
pub(super) use rustc_hir::{MethodKind, Target};
pub(super) use rustc_hir::{AssocKind, MethodKind, Target};
#[doc(hidden)]
pub(super) use rustc_span::{Ident, Span, Symbol, sym};
#[doc(hidden)]
Expand Down
33 changes: 24 additions & 9 deletions compiler/rustc_attr_parsing/src/attributes/rustc_dump.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_feature::AttributeStability;
use rustc_hir::attrs::{AttributeKind, RustcDumpLayoutKind};
use rustc_hir::{MethodKind, Target};
use rustc_hir::{AssocKind, MethodKind, Target};
use rustc_span::{Span, Symbol, sym};

use super::prelude::*;
Expand Down Expand Up @@ -59,8 +59,12 @@ impl NoArgsAttributeParser for RustcDumpGenericsParser {
Allow(Target::Closure),
Allow(Target::TyAlias),
Allow(Target::Const),
Allow(Target::AssocConst),
Allow(Target::AssocTy),
Allow(Target::AssocConst(AssocKind::Inherent)),
Allow(Target::AssocConst(AssocKind::Trait)),
Allow(Target::AssocConst(AssocKind::TraitImpl)),
Allow(Target::AssocTy(AssocKind::Inherent)),
Allow(Target::AssocTy(AssocKind::Trait)),
Allow(Target::AssocTy(AssocKind::TraitImpl)),
Allow(Target::Impl { of_trait: false }),
Allow(Target::Impl { of_trait: true }),
Allow(Target::Method(MethodKind::Inherent)),
Expand Down Expand Up @@ -101,8 +105,11 @@ pub(crate) struct RustcDumpItemBoundsParser;

impl NoArgsAttributeParser for RustcDumpItemBoundsParser {
const PATH: &[Symbol] = &[sym::rustc_dump_item_bounds];
const ALLOWED_TARGETS: AllowedTargets<'_> =
AllowedTargets::AllowList(&[Allow(Target::AssocTy)]);
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
Allow(Target::AssocTy(AssocKind::Inherent)),
Allow(Target::AssocTy(AssocKind::Trait)),
Allow(Target::AssocTy(AssocKind::TraitImpl)),
]);
const STABILITY: AttributeStability = unstable!(rustc_attrs);
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcDumpItemBounds;
}
Expand Down Expand Up @@ -176,8 +183,12 @@ pub(crate) struct RustcDumpObjectLifetimeDefaultsParser;
impl NoArgsAttributeParser for RustcDumpObjectLifetimeDefaultsParser {
const PATH: &[Symbol] = &[sym::rustc_dump_object_lifetime_defaults];
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
Allow(Target::AssocConst),
Allow(Target::AssocTy),
Allow(Target::AssocConst(AssocKind::Inherent)),
Allow(Target::AssocConst(AssocKind::Trait)),
Allow(Target::AssocConst(AssocKind::TraitImpl)),
Allow(Target::AssocTy(AssocKind::Inherent)),
Allow(Target::AssocTy(AssocKind::Trait)),
Allow(Target::AssocTy(AssocKind::TraitImpl)),
Allow(Target::Const),
Allow(Target::Enum),
Allow(Target::Fn),
Expand All @@ -203,8 +214,12 @@ pub(crate) struct RustcDumpPredicatesParser;
impl NoArgsAttributeParser for RustcDumpPredicatesParser {
const PATH: &[Symbol] = &[sym::rustc_dump_predicates];
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
Allow(Target::AssocConst),
Allow(Target::AssocTy),
Allow(Target::AssocConst(AssocKind::Inherent)),
Allow(Target::AssocConst(AssocKind::Trait)),
Allow(Target::AssocConst(AssocKind::TraitImpl)),
Allow(Target::AssocTy(AssocKind::Inherent)),
Allow(Target::AssocTy(AssocKind::Trait)),
Allow(Target::AssocTy(AssocKind::TraitImpl)),
Allow(Target::Const),
Allow(Target::Delegation { mac: false }),
Allow(Target::Delegation { mac: true }),
Expand Down
40 changes: 30 additions & 10 deletions compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,8 +764,12 @@ impl CombineAttributeParser for RustcCleanParser {
const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::RustcClean(items);
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
// tidy-alphabetical-start
Allow(Target::AssocConst),
Allow(Target::AssocTy),
Allow(Target::AssocConst(AssocKind::Inherent)),
Allow(Target::AssocConst(AssocKind::Trait)),
Allow(Target::AssocConst(AssocKind::TraitImpl)),
Allow(Target::AssocTy(AssocKind::Inherent)),
Allow(Target::AssocTy(AssocKind::Trait)),
Allow(Target::AssocTy(AssocKind::TraitImpl)),
Allow(Target::Const),
Allow(Target::Enum),
Allow(Target::Expression),
Expand Down Expand Up @@ -857,8 +861,12 @@ impl SingleAttributeParser for RustcIfThisChangedParser {
const PATH: &[Symbol] = &[sym::rustc_if_this_changed];
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
// tidy-alphabetical-start
Allow(Target::AssocConst),
Allow(Target::AssocTy),
Allow(Target::AssocConst(AssocKind::Inherent)),
Allow(Target::AssocConst(AssocKind::Trait)),
Allow(Target::AssocConst(AssocKind::TraitImpl)),
Allow(Target::AssocTy(AssocKind::Inherent)),
Allow(Target::AssocTy(AssocKind::Trait)),
Allow(Target::AssocTy(AssocKind::TraitImpl)),
Allow(Target::Const),
Allow(Target::Enum),
Allow(Target::Expression),
Expand Down Expand Up @@ -915,8 +923,12 @@ impl CombineAttributeParser for RustcThenThisWouldNeedParser {
|items, _span| AttributeKind::RustcThenThisWouldNeed(items);
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
// tidy-alphabetical-start
Allow(Target::AssocConst),
Allow(Target::AssocTy),
Allow(Target::AssocConst(AssocKind::Inherent)),
Allow(Target::AssocConst(AssocKind::Trait)),
Allow(Target::AssocConst(AssocKind::TraitImpl)),
Allow(Target::AssocTy(AssocKind::Inherent)),
Allow(Target::AssocTy(AssocKind::Trait)),
Allow(Target::AssocTy(AssocKind::TraitImpl)),
Allow(Target::Const),
Allow(Target::Enum),
Allow(Target::Expression),
Expand Down Expand Up @@ -991,12 +1003,16 @@ impl NoArgsAttributeParser for RustcEffectiveVisibilityParser {
Allow(Target::TraitAlias),
Allow(Target::Impl { of_trait: false }),
Allow(Target::Impl { of_trait: true }),
Allow(Target::AssocConst),
Allow(Target::AssocConst(AssocKind::Inherent)),
Allow(Target::AssocConst(AssocKind::Trait)),
Allow(Target::AssocConst(AssocKind::TraitImpl)),
Allow(Target::Method(MethodKind::Inherent)),
Allow(Target::Method(MethodKind::Trait { body: false })),
Allow(Target::Method(MethodKind::Trait { body: true })),
Allow(Target::Method(MethodKind::TraitImpl)),
Allow(Target::AssocTy),
Allow(Target::AssocTy(AssocKind::Inherent)),
Allow(Target::AssocTy(AssocKind::Trait)),
Allow(Target::AssocTy(AssocKind::TraitImpl)),
Allow(Target::ForeignFn),
Allow(Target::ForeignStatic),
Allow(Target::ForeignTy),
Expand All @@ -1018,8 +1034,12 @@ impl SingleAttributeParser for RustcDiagnosticItemParser {
Allow(Target::Enum),
Allow(Target::MacroDef),
Allow(Target::TyAlias),
Allow(Target::AssocTy),
Allow(Target::AssocConst),
Allow(Target::AssocConst(AssocKind::Inherent)),
Allow(Target::AssocConst(AssocKind::Trait)),
Allow(Target::AssocConst(AssocKind::TraitImpl)),
Allow(Target::AssocTy(AssocKind::Inherent)),
Allow(Target::AssocTy(AssocKind::Trait)),
Allow(Target::AssocTy(AssocKind::TraitImpl)),
Allow(Target::Fn),
Allow(Target::Const),
Allow(Target::Mod),
Expand Down
14 changes: 10 additions & 4 deletions compiler/rustc_attr_parsing/src/attributes/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_feature::{ACCEPTED_LANG_FEATURES, AttributeStability};
use rustc_hir::attrs::UnstableRemovedFeature;
use rustc_hir::target::GenericParamKind;
use rustc_hir::{
DefaultBodyStability, MethodKind, PartialConstStability, Stability, StabilityLevel,
AssocKind, DefaultBodyStability, MethodKind, PartialConstStability, Stability, StabilityLevel,
StableSince, Target, UnstableReason, VERSION_PLACEHOLDER,
};

Expand All @@ -29,8 +29,12 @@ const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
Allow(Target::Mod),
Allow(Target::Use), // FIXME I don't think this does anything?
Allow(Target::Const),
Allow(Target::AssocConst),
Allow(Target::AssocTy),
Allow(Target::AssocConst(AssocKind::Inherent)),
Allow(Target::AssocConst(AssocKind::Trait)),
Allow(Target::AssocConst(AssocKind::TraitImpl)),
Allow(Target::AssocTy(AssocKind::Inherent)),
Allow(Target::AssocTy(AssocKind::Trait)),
Allow(Target::AssocTy(AssocKind::TraitImpl)),
Allow(Target::Trait),
Allow(Target::TraitAlias),
Allow(Target::TyAlias),
Expand Down Expand Up @@ -243,7 +247,9 @@ impl AttributeParser for ConstStabilityParser {
Allow(Target::Impl { of_trait: true }),
Allow(Target::Use), // FIXME I don't think this does anything?
Allow(Target::Const),
Allow(Target::AssocConst),
Allow(Target::AssocConst(AssocKind::Inherent)),
Allow(Target::AssocConst(AssocKind::Trait)),
Allow(Target::AssocConst(AssocKind::TraitImpl)),
Allow(Target::Trait),
Allow(Target::Static),
Allow(Target::Crate),
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_attr_parsing/src/session_diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ pub(crate) struct BothFfiConstAndPure {
pub attr_span: Span,
}

#[derive(Diagnostic)]
#[diag("this `#[deprecated]` annotation has no effect")]
pub(crate) struct DeprecatedAnnotationHasNoEffect {
#[suggestion(
"remove the unnecessary deprecation attribute",
applicability = "machine-applicable",
code = ""
)]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag("attribute should be applied to `#[repr(transparent)]` types")]
pub(crate) struct RustcPubTransparent {
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_attr_parsing/src/target_checking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_ast::{AttrStyle, Safety};
use rustc_errors::{DiagArgValue, MultiSpan, StashKey};
use rustc_feature::Features;
use rustc_hir::attrs::AttributeKind;
use rustc_hir::{AttrItem, Attribute, MethodKind, Target};
use rustc_hir::{AssocKind, AttrItem, Attribute, MethodKind, Target};
use rustc_span::{BytePos, FileName, RemapPathScopeComponents, Span, Symbol, sym};

use crate::context::AcceptContext;
Expand Down Expand Up @@ -507,12 +507,16 @@ pub(crate) const ALL_TARGETS: &[Policy] = {
Allow(Target::Expression),
Allow(Target::Statement),
Allow(Target::Arm),
Allow(Target::AssocConst),
Allow(Target::AssocConst(AssocKind::Inherent)),
Allow(Target::AssocConst(AssocKind::Trait)),
Allow(Target::AssocConst(AssocKind::TraitImpl)),
Allow(Target::Method(MethodKind::Inherent)),
Allow(Target::Method(MethodKind::Trait { body: false })),
Allow(Target::Method(MethodKind::Trait { body: true })),
Allow(Target::Method(MethodKind::TraitImpl)),
Allow(Target::AssocTy),
Allow(Target::AssocTy(AssocKind::Inherent)),
Allow(Target::AssocTy(AssocKind::Trait)),
Allow(Target::AssocTy(AssocKind::TraitImpl)),
Allow(Target::ForeignFn),
Allow(Target::ForeignStatic),
Allow(Target::ForeignTy),
Expand Down
Loading
Loading