diff --git a/compiler/rustc_hir/src/attrs/mod.rs b/compiler/rustc_hir/src/attrs/mod.rs index 660e7e0aa8be..e89feeb6129b 100644 --- a/compiler/rustc_hir/src/attrs/mod.rs +++ b/compiler/rustc_hir/src/attrs/mod.rs @@ -57,9 +57,11 @@ macro_rules! find_attr { ($tcx: expr, $def_id: expr, $pattern: pat $(if $guard: expr)?) => { $crate::find_attr!($tcx, $def_id, $pattern $(if $guard)? => ()).is_some() }; - ($tcx: expr, $def_id: expr, $pattern: pat $(if $guard: expr)? => $e: expr) => { - $crate::find_attr!($tcx.get_all_attrs($def_id), $pattern $(if $guard)? => $e) - }; + ($tcx: expr, $def_id: expr, $pattern: pat $(if $guard: expr)? => $e: expr) => {{ + #[allow(deprecated)] { + $crate::find_attr!($tcx.get_all_attrs($def_id), $pattern $(if $guard)? => $e) + } + }}; ($attributes_list: expr, $pattern: pat $(if $guard: expr)?) => {{ diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 2b5bc8e86c61..9fa936d87ffd 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -895,6 +895,8 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef { _ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"), }; + // we do a bunch of find_attr calls here, probably faster to get them from the tcx just once. + #[allow(deprecated)] let attrs = tcx.get_all_attrs(def_id); let paren_sugar = find_attr!(attrs, AttributeKind::RustcParenSugar(_)); diff --git a/compiler/rustc_middle/src/ty/instance.rs b/compiler/rustc_middle/src/ty/instance.rs index 0e9dd7dd169c..2444046c604b 100644 --- a/compiler/rustc_middle/src/ty/instance.rs +++ b/compiler/rustc_middle/src/ty/instance.rs @@ -10,7 +10,7 @@ use rustc_index::bit_set::FiniteBitSet; use rustc_macros::{Decodable, Encodable, HashStable, Lift, TyDecodable, TyEncodable}; use rustc_span::def_id::LOCAL_CRATE; -use rustc_span::{DUMMY_SP, Span, Symbol}; +use rustc_span::{DUMMY_SP, Span}; use tracing::{debug, instrument}; use crate::error; @@ -286,15 +286,6 @@ pub fn def_id_if_not_guaranteed_local_codegen(self) -> Option { } } - #[inline] - pub fn get_attrs( - &self, - tcx: TyCtxt<'tcx>, - attr: Symbol, - ) -> impl Iterator { - tcx.get_attrs(self.def_id(), attr) - } - /// Returns `true` if the LLVM version of this instance is unconditionally /// marked with `inline`. This implies that a copy of this instance is /// generated in every codegen unit. diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 8454eaa9e430..7cdd48b9bf35 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1708,11 +1708,13 @@ pub fn instance_mir(self, instance: ty::InstanceKind<'tcx>) -> &'tcx Body<'tcx> } /// Gets all attributes with the given name. + #[deprecated = "Though there are valid usecases for this method, especially when your attribute is not a parsed attribute, usually you want to call rustc_hir::find_attr! instead."] pub fn get_attrs( self, did: impl Into, attr: Symbol, ) -> impl Iterator { + #[allow(deprecated)] self.get_all_attrs(did).iter().filter(move |a: &&hir::Attribute| a.has_name(attr)) } @@ -1720,6 +1722,7 @@ pub fn get_attrs( /// /// To see if an item has a specific attribute, you should use /// [`rustc_hir::find_attr!`] so you can use matching. + #[deprecated = "Though there are valid usecases for this method, especially when your attribute is not a parsed attribute, usually you want to call rustc_hir::find_attr! instead."] pub fn get_all_attrs(self, did: impl Into) -> &'tcx [hir::Attribute] { let did: DefId = did.into(); if let Some(did) = did.as_local() { @@ -1742,17 +1745,21 @@ pub fn get_attrs_by_path( } } + #[deprecated = "Though there are valid usecases for this method, especially when your attribute is not a parsed attribute, usually you want to call rustc_hir::find_attr! instead."] pub fn get_attr(self, did: impl Into, attr: Symbol) -> Option<&'tcx hir::Attribute> { if cfg!(debug_assertions) && !rustc_feature::is_valid_for_get_attr(attr) { let did: DefId = did.into(); bug!("get_attr: unexpected called with DefId `{:?}`, attr `{:?}`", did, attr); } else { + #[allow(deprecated)] self.get_attrs(did, attr).next() } } /// Determines whether an item is annotated with an attribute. + #[deprecated = "Though there are valid usecases for this method, especially when your attribute is not a parsed attribute, usually you want to call rustc_hir::find_attr! instead."] pub fn has_attr(self, did: impl Into, attr: Symbol) -> bool { + #[allow(deprecated)] self.get_attrs(did, attr).next().is_some() }