From f113540cec8632b6c8ab023c5175734ef96b5cb9 Mon Sep 17 00:00:00 2001 From: Jonathan Brouwer Date: Sat, 25 Apr 2026 21:19:26 +0200 Subject: [PATCH 1/2] Remove unnecessary uses of `AttributeExt` --- compiler/rustc_ast/src/attr/mod.rs | 41 +++++++++++++++---- .../rustc_attr_parsing/src/attributes/util.rs | 7 ++-- compiler/rustc_resolve/src/rustdoc.rs | 2 +- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs index 369fe12539fa8..bb2e444c722ad 100644 --- a/compiler/rustc_ast/src/attr/mod.rs +++ b/compiler/rustc_ast/src/attr/mod.rs @@ -341,6 +341,34 @@ impl Attribute { )], } } + + pub fn deprecation_note(&self) -> Option { + match &self.kind { + AttrKind::Normal(normal) if normal.item.path == sym::deprecated => { + let meta = &normal.item; + + // #[deprecated = "..."] + if let Some(s) = meta.value_str() { + return Some(Ident { name: s, span: meta.span() }); + } + + // #[deprecated(note = "...")] + if let Some(list) = meta.meta_item_list() { + for nested in list { + if let Some(mi) = nested.meta_item() + && mi.path == sym::note + && let Some(s) = mi.value_str() + { + return Some(Ident { name: s, span: mi.span }); + } + } + } + + None + } + _ => None, + } + } } impl AttrItem { @@ -824,19 +852,19 @@ pub fn mk_attr_name_value_str( mk_attr(g, style, unsafety, path, args, span) } -pub fn filter_by_name(attrs: &[A], name: Symbol) -> impl Iterator { +pub fn filter_by_name(attrs: &[Attribute], name: Symbol) -> impl Iterator { attrs.iter().filter(move |attr| attr.has_name(name)) } -pub fn find_by_name(attrs: &[A], name: Symbol) -> Option<&A> { +pub fn find_by_name(attrs: &[Attribute], name: Symbol) -> Option<&Attribute> { filter_by_name(attrs, name).next() } -pub fn first_attr_value_str_by_name(attrs: &[impl AttributeExt], name: Symbol) -> Option { +pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: Symbol) -> Option { find_by_name(attrs, name).and_then(|attr| attr.value_str()) } -pub fn contains_name(attrs: &[impl AttributeExt], name: Symbol) -> bool { +pub fn contains_name(attrs: &[Attribute], name: Symbol) -> bool { find_by_name(attrs, name).is_some() } @@ -911,11 +939,6 @@ pub trait AttributeExt: Debug { /// * `#[doc(...)]` returns `None`. fn doc_str(&self) -> Option; - /// Returns the deprecation note if this is deprecation attribute. - /// * `#[deprecated = "note"]` returns `Some("note")`. - /// * `#[deprecated(note = "note", ...)]` returns `Some("note")`. - fn deprecation_note(&self) -> Option; - /// Returns whether this attribute is any of the proc macro attributes. /// i.e. `proc_macro`, `proc_macro_attribute` or `proc_macro_derive`. fn is_proc_macro_attr(&self) -> bool { diff --git a/compiler/rustc_attr_parsing/src/attributes/util.rs b/compiler/rustc_attr_parsing/src/attributes/util.rs index 98f8cc23b5001..b6773df67cd03 100644 --- a/compiler/rustc_attr_parsing/src/attributes/util.rs +++ b/compiler/rustc_attr_parsing/src/attributes/util.rs @@ -1,7 +1,6 @@ use std::num::IntErrorKind; -use rustc_ast::LitKind; -use rustc_ast::attr::AttributeExt; +use rustc_ast::{LitKind, ast}; use rustc_feature::is_builtin_attr_name; use rustc_hir::RustcVersion; use rustc_hir::limit::Limit; @@ -27,8 +26,8 @@ pub fn parse_version(s: Symbol) -> Option { Some(RustcVersion { major, minor, patch }) } -pub fn is_builtin_attr(attr: &impl AttributeExt) -> bool { - attr.is_doc_comment().is_some() || attr.name().is_some_and(|name| is_builtin_attr_name(name)) +pub fn is_builtin_attr(attr: &ast::Attribute) -> bool { + attr.is_doc_comment() || attr.name().is_some_and(|name| is_builtin_attr_name(name)) } /// Parse a single integer. diff --git a/compiler/rustc_resolve/src/rustdoc.rs b/compiler/rustc_resolve/src/rustdoc.rs index 89b561bd2e901..f26405cb223c0 100644 --- a/compiler/rustc_resolve/src/rustdoc.rs +++ b/compiler/rustc_resolve/src/rustdoc.rs @@ -409,7 +409,7 @@ pub fn may_be_doc_link(link_type: LinkType) -> bool { /// Simplified version of `preprocessed_markdown_links` from rustdoc. /// Must return at least the same links as it, but may add some more links on top of that. -pub(crate) fn attrs_to_preprocessed_links(attrs: &[A]) -> Vec> { +pub(crate) fn attrs_to_preprocessed_links(attrs: &[ast::Attribute]) -> Vec> { let (doc_fragments, other_attrs) = attrs_to_doc_fragments(attrs.iter().map(|attr| (attr, None)), false); let mut doc = From e3b0e9db08425cf80090a193028ef49eae9761b1 Mon Sep 17 00:00:00 2001 From: Jonathan Brouwer Date: Sat, 25 Apr 2026 21:26:00 +0200 Subject: [PATCH 2/2] Remove `deprecation_note` from `AttributeExt` --- compiler/rustc_ast/src/attr/mod.rs | 28 ---------------------------- compiler/rustc_hir/src/hir.rs | 8 -------- src/librustdoc/clean/types.rs | 7 +------ 3 files changed, 1 insertion(+), 42 deletions(-) diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs index bb2e444c722ad..0374a86d3eb1c 100644 --- a/compiler/rustc_ast/src/attr/mod.rs +++ b/compiler/rustc_ast/src/attr/mod.rs @@ -235,34 +235,6 @@ impl AttributeExt for Attribute { } } - fn deprecation_note(&self) -> Option { - match &self.kind { - AttrKind::Normal(normal) if normal.item.path == sym::deprecated => { - let meta = &normal.item; - - // #[deprecated = "..."] - if let Some(s) = meta.value_str() { - return Some(Ident { name: s, span: meta.span() }); - } - - // #[deprecated(note = "...")] - if let Some(list) = meta.meta_item_list() { - for nested in list { - if let Some(mi) = nested.meta_item() - && mi.path == sym::note - && let Some(s) = mi.value_str() - { - return Some(Ident { name: s, span: mi.span }); - } - } - } - - None - } - _ => None, - } - } - fn doc_resolution_scope(&self) -> Option { match &self.kind { AttrKind::DocComment(..) => Some(self.style), diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 5608bd82fdacd..60bfc55282564 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1415,14 +1415,6 @@ impl AttributeExt for Attribute { } } - #[inline] - fn deprecation_note(&self) -> Option { - match &self { - Attribute::Parsed(AttributeKind::Deprecated { deprecation, .. }) => deprecation.note, - _ => None, - } - } - fn is_automatically_derived_attr(&self) -> bool { matches!(self, Attribute::Parsed(AttributeKind::AutomaticallyDerived(..))) } diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index a1eb093d7fcf2..e81c6eae1996f 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -8,7 +8,6 @@ use arrayvec::ArrayVec; use itertools::Either; use rustc_abi::{ExternAbi, VariantIdx}; use rustc_ast as ast; -use rustc_ast::attr::AttributeExt; use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet}; use rustc_data_structures::thin_vec::ThinVec; use rustc_hir as hir; @@ -501,11 +500,7 @@ impl Item { } pub(crate) fn attr_span(&self, tcx: TyCtxt<'_>) -> rustc_span::Span { - let deprecation_notes = self - .attrs - .other_attrs - .iter() - .filter_map(|attr| attr.deprecation_note().map(|note| note.span)); + let deprecation_notes = find_attr!(&self.attrs.other_attrs, Deprecated { deprecation, .. } => deprecation.note.map(|note| note.span)).flatten(); span_of_fragments(&self.attrs.doc_strings) .into_iter()