Skip to content

Querify hir attr fetching again #113696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
16 changes: 8 additions & 8 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
11 changes: 5 additions & 6 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 0 additions & 9 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_ty_utils/src/assoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 2 additions & 5 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Span> {
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down