From 6cbf87ceb5ebf6571d78036be770d3f37e918f9a Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 14 Jul 2023 14:57:31 +0000 Subject: [PATCH 1/2] Querify hir attr fetching again --- compiler/rustc_middle/src/hir/mod.rs | 1 + compiler/rustc_middle/src/ty/mod.rs | 9 --------- src/librustdoc/clean/inline.rs | 2 +- src/librustdoc/clean/types.rs | 7 ++----- src/librustdoc/html/render/print_item.rs | 2 +- .../src/matches/significant_drop_in_scrutinee.rs | 2 +- .../clippy_lints/src/significant_drop_tightening.rs | 2 +- 7 files changed, 7 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index 45a07fdd29327..e8766c0d92620 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -153,6 +153,7 @@ pub fn provide(providers: &mut Providers) { providers.hir_attrs = |tcx, id| { tcx.hir_crate(()).owners[id.def_id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs) }; + providers.item_attrs = |tcx, def_id| tcx.hir().attrs(tcx.hir().local_def_id_to_hir_id(def_id)); providers.def_span = |tcx, def_id| { let def_id = def_id; let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 519bdb01623e0..4047d15b6539c 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -2491,15 +2491,6 @@ impl<'tcx> TyCtxt<'tcx> { } } - // FIXME(@lcnr): Remove this function. - pub fn get_attrs_unchecked(self, did: DefId) -> &'tcx [ast::Attribute] { - if let Some(did) = did.as_local() { - self.hir().attrs(self.hir().local_def_id_to_hir_id(did)) - } else { - self.item_attrs(did) - } - } - /// Gets all attributes with the given name. pub fn get_attrs( self, diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 870cfa93058aa..161446c706c6b 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -175,7 +175,7 @@ pub(crate) fn try_inline_glob( } pub(crate) fn load_attrs<'hir>(cx: &DocContext<'hir>, did: DefId) -> &'hir [ast::Attribute] { - cx.tcx.get_attrs_unchecked(did) + cx.tcx.item_attrs(did) } /// Record an external fully qualified name in the external_paths cache. diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 26139d5276919..b6c3228c0bb96 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -370,10 +370,7 @@ impl Item { } pub(crate) fn inner_docs(&self, tcx: TyCtxt<'_>) -> bool { - self.item_id - .as_def_id() - .map(|did| inner_docs(tcx.get_attrs_unchecked(did))) - .unwrap_or(false) + self.item_id.as_def_id().map(|did| inner_docs(tcx.item_attrs(did))).unwrap_or(false) } pub(crate) fn span(&self, tcx: TyCtxt<'_>) -> Option { @@ -418,7 +415,7 @@ impl Item { kind: ItemKind, cx: &mut DocContext<'_>, ) -> Item { - let ast_attrs = cx.tcx.get_attrs_unchecked(def_id); + let ast_attrs = cx.tcx.item_attrs(def_id); Self::from_def_id_and_attrs_and_parts( def_id, diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 383e3c170881a..163b930432e22 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -465,7 +465,7 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items: clean::ImportItem(ref import) => { let stab_tags = if let Some(import_def_id) = import.source.did { - let ast_attrs = tcx.get_attrs_unchecked(import_def_id); + let ast_attrs = tcx.item_attrs(import_def_id); let import_attrs = Box::new(clean::Attributes::from_ast(ast_attrs)); // Just need an item with the correct def_id and attrs diff --git a/src/tools/clippy/clippy_lints/src/matches/significant_drop_in_scrutinee.rs b/src/tools/clippy/clippy_lints/src/matches/significant_drop_in_scrutinee.rs index 37528d9f7f200..7930855df9c8d 100644 --- a/src/tools/clippy/clippy_lints/src/matches/significant_drop_in_scrutinee.rs +++ b/src/tools/clippy/clippy_lints/src/matches/significant_drop_in_scrutinee.rs @@ -126,7 +126,7 @@ impl<'a, 'tcx> SigDropChecker<'a, 'tcx> { fn has_sig_drop_attr(&mut self, cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool { if let Some(adt) = ty.ty_adt_def() { - if get_attr(cx.sess(), cx.tcx.get_attrs_unchecked(adt.did()), "has_significant_drop").count() > 0 { + if get_attr(cx.sess(), cx.tcx.item_attrs(adt.did()), "has_significant_drop").count() > 0 { return true; } } diff --git a/src/tools/clippy/clippy_lints/src/significant_drop_tightening.rs b/src/tools/clippy/clippy_lints/src/significant_drop_tightening.rs index fffa8a380c2f8..a31b036e3839c 100644 --- a/src/tools/clippy/clippy_lints/src/significant_drop_tightening.rs +++ b/src/tools/clippy/clippy_lints/src/significant_drop_tightening.rs @@ -176,7 +176,7 @@ impl<'cx, 'others, 'tcx> AttrChecker<'cx, 'others, 'tcx> { if let Some(adt) = ty.ty_adt_def() { let mut iter = get_attr( self.cx.sess(), - self.cx.tcx.get_attrs_unchecked(adt.did()), + self.cx.tcx.item_attrs(adt.did()), "has_significant_drop", ); if iter.next().is_some() { From 561332e33e8f4e3d27701a0f56c6e3eb1f0b9ca8 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 14 Jul 2023 14:58:16 +0000 Subject: [PATCH 2/2] Feed and only optionally encode item attrs if theyre present --- compiler/rustc_metadata/src/rmeta/decoder.rs | 16 ++++++++-------- compiler/rustc_metadata/src/rmeta/encoder.rs | 11 +++++------ compiler/rustc_middle/src/query/mod.rs | 1 + compiler/rustc_ty_utils/src/assoc.rs | 4 ++++ 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index b9318aee58131..a8d29e4c1720c 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1141,19 +1141,19 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { .tables .attributes .get(self, id) - .unwrap_or_else(|| { + .or_else(|| { // Structure and variant constructors don't have any attributes encoded for them, // but we assume that someone passing a constructor ID actually wants to look at // the attributes on the corresponding struct or variant. let def_key = self.def_key(id); - assert_eq!(def_key.disambiguated_data.data, DefPathData::Ctor); - let parent_id = def_key.parent.expect("no parent for a constructor"); - self.root - .tables - .attributes - .get(self, parent_id) - .expect("no encoded attributes for a structure or variant") + if def_key.disambiguated_data.data == DefPathData::Ctor { + let parent_id = def_key.parent.expect("no parent for a constructor"); + self.root.tables.attributes.get(self, parent_id) + } else { + None + } }) + .unwrap_or_else(|| LazyArray::default()) .decode((self, sess)) } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 01dd35b0e5be5..4e8e81e57a37a 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1284,13 +1284,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { is_exported: tcx.effective_visibilities(()).is_exported(def_id), is_doc_hidden: false, }; - let attr_iter = tcx - .opt_local_def_id_to_hir_id(def_id) - .map_or(Default::default(), |hir_id| tcx.hir().attrs(hir_id)) - .iter() - .filter(|attr| analyze_attr(attr, &mut state)); - record_array!(self.tables.attributes[def_id.to_def_id()] <- attr_iter); + let item_attrs = tcx.item_attrs(def_id); + if !item_attrs.is_empty() { + let attr_iter = item_attrs.iter().filter(|attr| analyze_attr(attr, &mut state)); + record_array!(self.tables.attributes[def_id.to_def_id()] <- attr_iter); + } let mut attr_flags = AttrFlags::empty(); if state.is_doc_hidden { diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index a059590e6ad23..1e9da65de30f3 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1214,6 +1214,7 @@ rustc_queries! { query item_attrs(def_id: DefId) -> &'tcx [ast::Attribute] { desc { |tcx| "collecting attributes of `{}`", tcx.def_path_str(def_id) } separate_provide_extern + feedable } query codegen_fn_attrs(def_id: DefId) -> &'tcx CodegenFnAttrs { diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index 7e22ef89ed370..b00c690c14a07 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -256,6 +256,8 @@ fn associated_type_for_impl_trait_in_trait( let local_def_id = trait_assoc_ty.def_id(); let def_id = local_def_id.to_def_id(); + trait_assoc_ty.item_attrs(&[]); + trait_assoc_ty.opt_def_kind(Some(DefKind::AssocTy)); // There's no HIR associated with this new synthesized `def_id`, so feed @@ -351,6 +353,8 @@ fn associated_type_for_impl_trait_in_impl( let local_def_id = impl_assoc_ty.def_id(); let def_id = local_def_id.to_def_id(); + impl_assoc_ty.item_attrs(&[]); + impl_assoc_ty.opt_def_kind(Some(DefKind::AssocTy)); // There's no HIR associated with this new synthesized `def_id`, so feed