From 36a28060f146a5c5ff6445659ce5962009c8829d Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 5 Feb 2021 15:47:44 +0100 Subject: [PATCH] Merge the BTreeMap in hir::Crate. --- compiler/rustc_ast_lowering/src/item.rs | 16 +- compiler/rustc_ast_lowering/src/lib.rs | 59 ++-- compiler/rustc_hir/src/arena.rs | 3 + compiler/rustc_hir/src/hir.rs | 252 ++++++++++++++---- compiler/rustc_hir/src/intravisit.rs | 2 +- compiler/rustc_lint/src/builtin.rs | 2 +- compiler/rustc_lint/src/levels.rs | 4 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 2 +- .../rustc_middle/src/hir/map/collector.rs | 8 +- compiler/rustc_middle/src/hir/map/mod.rs | 4 +- compiler/rustc_middle/src/ty/print/pretty.rs | 2 +- compiler/rustc_passes/src/diagnostic_items.rs | 2 +- compiler/rustc_resolve/src/late/lifetimes.rs | 29 +- src/librustdoc/visit_ast.rs | 2 +- 14 files changed, 268 insertions(+), 119 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 9f9d41c3f3d..852f696ef14 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -82,15 +82,11 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> { self.lctx.with_hir_id_owner(item.id, |lctx| match ctxt { AssocCtxt::Trait => { let hir_item = lctx.lower_trait_item(item); - let id = hir_item.trait_item_id(); - lctx.trait_items.insert(id, hir_item); - lctx.modules.entry(lctx.current_module).or_default().trait_items.insert(id); + lctx.insert_trait_item(hir_item); } AssocCtxt::Impl => { let hir_item = lctx.lower_impl_item(item); - let id = hir_item.impl_item_id(); - lctx.impl_items.insert(id, hir_item); - lctx.modules.entry(lctx.current_module).or_default().impl_items.insert(id); + lctx.insert_impl_item(hir_item); } }); @@ -101,9 +97,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> { self.lctx.allocate_hir_id_counter(item.id); self.lctx.with_hir_id_owner(item.id, |lctx| { let hir_item = lctx.lower_foreign_item(item); - let id = hir_item.foreign_item_id(); - lctx.foreign_items.insert(id, hir_item); - lctx.modules.entry(lctx.current_module).or_default().foreign_items.insert(id); + lctx.insert_foreign_item(hir_item); }); visit::walk_foreign_item(self, item); @@ -123,7 +117,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ) -> T { let old_len = self.in_scope_lifetimes.len(); - let parent_generics = match self.items.get(&parent_hir_id).unwrap().kind { + let parent_generics = match self.owners[parent_hir_id.def_id].unwrap().expect_item().kind { hir::ItemKind::Impl(hir::Impl { ref generics, .. }) | hir::ItemKind::Trait(_, _, ref generics, ..) => generics.params, _ => &[], @@ -224,7 +218,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let hir_id = self.lower_node_id(i.id); self.lower_attrs(hir_id, &i.attrs); let body = P(self.lower_mac_args(body)); - self.exported_macros.push(hir::MacroDef { + self.insert_macro_def(hir::MacroDef { ident, vis, def_id: hir_id.expect_owner(), diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index d4caba92416..540abe6e48e 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -101,13 +101,8 @@ struct LoweringContext<'a, 'hir: 'a> { arena: &'hir Arena<'hir>, /// The items being lowered are collected here. - items: BTreeMap>, - - trait_items: BTreeMap>, - impl_items: BTreeMap>, - foreign_items: BTreeMap>, + owners: IndexVec>>, bodies: BTreeMap>, - exported_macros: Vec>, non_exported_macro_attrs: Vec, trait_impls: BTreeMap>, @@ -330,15 +325,11 @@ pub fn lower_crate<'a, 'hir>( resolver, nt_to_tokenstream, arena, - items: BTreeMap::new(), - trait_items: BTreeMap::new(), - impl_items: BTreeMap::new(), - foreign_items: BTreeMap::new(), + owners: IndexVec::default(), bodies: BTreeMap::new(), trait_impls: BTreeMap::new(), modules: BTreeMap::new(), attrs: BTreeMap::default(), - exported_macros: Vec::new(), non_exported_macro_attrs: Vec::new(), catch_scopes: Vec::new(), loop_scopes: Vec::new(), @@ -558,12 +549,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let krate = hir::Crate { item: module, - exported_macros: self.arena.alloc_from_iter(self.exported_macros), non_exported_macro_attrs: self.arena.alloc_from_iter(self.non_exported_macro_attrs), - items: self.items, - trait_items: self.trait_items, - impl_items: self.impl_items, - foreign_items: self.foreign_items, + owners: self.owners, bodies: self.bodies, body_ids, trait_impls: self.trait_impls, @@ -576,12 +563,48 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } fn insert_item(&mut self, item: hir::Item<'hir>) -> hir::ItemId { - let id = hir::ItemId { def_id: item.def_id }; - self.items.insert(id, item); + let id = item.item_id(); + let item = self.arena.alloc(item); + self.owners.ensure_contains_elem(id.def_id, || None); + self.owners[id.def_id] = Some(hir::OwnerNode::Item(item)); self.modules.entry(self.current_module).or_default().items.insert(id); id } + fn insert_foreign_item(&mut self, item: hir::ForeignItem<'hir>) -> hir::ForeignItemId { + let id = item.foreign_item_id(); + let item = self.arena.alloc(item); + self.owners.ensure_contains_elem(id.def_id, || None); + self.owners[id.def_id] = Some(hir::OwnerNode::ForeignItem(item)); + self.modules.entry(self.current_module).or_default().foreign_items.insert(id); + id + } + + fn insert_impl_item(&mut self, item: hir::ImplItem<'hir>) -> hir::ImplItemId { + let id = item.impl_item_id(); + let item = self.arena.alloc(item); + self.owners.ensure_contains_elem(id.def_id, || None); + self.owners[id.def_id] = Some(hir::OwnerNode::ImplItem(item)); + self.modules.entry(self.current_module).or_default().impl_items.insert(id); + id + } + + fn insert_trait_item(&mut self, item: hir::TraitItem<'hir>) -> hir::TraitItemId { + let id = item.trait_item_id(); + let item = self.arena.alloc(item); + self.owners.ensure_contains_elem(id.def_id, || None); + self.owners[id.def_id] = Some(hir::OwnerNode::TraitItem(item)); + self.modules.entry(self.current_module).or_default().trait_items.insert(id); + id + } + + fn insert_macro_def(&mut self, item: hir::MacroDef<'hir>) { + let def_id = item.def_id; + let item = self.arena.alloc(item); + self.owners.ensure_contains_elem(def_id, || None); + self.owners[def_id] = Some(hir::OwnerNode::MacroDef(item)); + } + fn allocate_hir_id_counter(&mut self, owner: NodeId) -> hir::HirId { // Set up the counter if needed. self.item_local_id_counters.entry(owner).or_insert(0); diff --git a/compiler/rustc_hir/src/arena.rs b/compiler/rustc_hir/src/arena.rs index b05ca381b8a..4e8cf009495 100644 --- a/compiler/rustc_hir/src/arena.rs +++ b/compiler/rustc_hir/src/arena.rs @@ -29,7 +29,9 @@ macro_rules! arena_types { [] fn_decl: rustc_hir::FnDecl<$tcx>, [] foreign_item: rustc_hir::ForeignItem<$tcx>, [few] foreign_item_ref: rustc_hir::ForeignItemRef<$tcx>, + [] impl_item: rustc_hir::ImplItem<$tcx>, [] impl_item_ref: rustc_hir::ImplItemRef<$tcx>, + [] item: rustc_hir::Item<$tcx>, [few] inline_asm: rustc_hir::InlineAsm<$tcx>, [few] llvm_inline_asm: rustc_hir::LlvmInlineAsm<$tcx>, [] local: rustc_hir::Local<$tcx>, @@ -42,6 +44,7 @@ macro_rules! arena_types { [] qpath: rustc_hir::QPath<$tcx>, [] stmt: rustc_hir::Stmt<$tcx>, [] field_def: rustc_hir::FieldDef<$tcx>, + [] trait_item: rustc_hir::TraitItem<$tcx>, [] trait_item_ref: rustc_hir::TraitItemRef, [] ty: rustc_hir::Ty<$tcx>, [] type_binding: rustc_hir::TypeBinding<$tcx>, diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 6aff2fdbd1f..f9b83f88ee4 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -12,6 +12,7 @@ pub use rustc_ast::{CaptureBy, Movability, Mutability}; use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece}; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::sync::{par_for_each_in, Send, Sync}; +use rustc_index::vec::IndexVec; use rustc_macros::HashStable_Generic; use rustc_span::source_map::Spanned; use rustc_span::symbol::{kw, sym, Ident, Symbol}; @@ -628,21 +629,10 @@ pub struct ModuleItems { #[derive(Debug)] pub struct Crate<'hir> { pub item: Mod<'hir>, - pub exported_macros: &'hir [MacroDef<'hir>], // Attributes from non-exported macros, kept only for collecting the library feature list. pub non_exported_macro_attrs: &'hir [Attribute], - // N.B., we use a `BTreeMap` here so that `visit_all_items` iterates - // over the ids in increasing order. In principle it should not - // matter what order we visit things in, but in *practice* it - // does, because it can affect the order in which errors are - // detected, which in turn can make UI tests yield - // slightly different results. - pub items: BTreeMap>, - - pub trait_items: BTreeMap>, - pub impl_items: BTreeMap>, - pub foreign_items: BTreeMap>, + pub owners: IndexVec>>, pub bodies: BTreeMap>, pub trait_impls: BTreeMap>, @@ -668,20 +658,20 @@ pub struct Crate<'hir> { } impl Crate<'hir> { - pub fn item(&self, id: ItemId) -> &Item<'hir> { - &self.items[&id] + pub fn item(&self, id: ItemId) -> &'hir Item<'hir> { + self.owners[id.def_id].as_ref().unwrap().expect_item() } - pub fn trait_item(&self, id: TraitItemId) -> &TraitItem<'hir> { - &self.trait_items[&id] + pub fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir> { + self.owners[id.def_id].as_ref().unwrap().expect_trait_item() } - pub fn impl_item(&self, id: ImplItemId) -> &ImplItem<'hir> { - &self.impl_items[&id] + pub fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> { + self.owners[id.def_id].as_ref().unwrap().expect_impl_item() } - pub fn foreign_item(&self, id: ForeignItemId) -> &ForeignItem<'hir> { - &self.foreign_items[&id] + pub fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir> { + self.owners[id.def_id].as_ref().unwrap().expect_foreign_item() } pub fn body(&self, id: BodyId) -> &Body<'hir> { @@ -702,20 +692,14 @@ impl Crate<'_> { where V: itemlikevisit::ItemLikeVisitor<'hir>, { - for item in self.items.values() { - visitor.visit_item(item); - } - - for trait_item in self.trait_items.values() { - visitor.visit_trait_item(trait_item); - } - - for impl_item in self.impl_items.values() { - visitor.visit_impl_item(impl_item); - } - - for foreign_item in self.foreign_items.values() { - visitor.visit_foreign_item(foreign_item); + for owner in self.owners.iter().filter_map(Option::as_ref) { + match owner { + OwnerNode::Item(item) => visitor.visit_item(item), + OwnerNode::ForeignItem(item) => visitor.visit_foreign_item(item), + OwnerNode::ImplItem(item) => visitor.visit_impl_item(item), + OwnerNode::TraitItem(item) => visitor.visit_trait_item(item), + OwnerNode::MacroDef(_) => {} + } } } @@ -724,28 +708,27 @@ impl Crate<'_> { where V: itemlikevisit::ParItemLikeVisitor<'hir> + Sync + Send, { - parallel!( - { - par_for_each_in(&self.items, |(_, item)| { - visitor.visit_item(item); - }); - }, - { - par_for_each_in(&self.trait_items, |(_, trait_item)| { - visitor.visit_trait_item(trait_item); - }); - }, - { - par_for_each_in(&self.impl_items, |(_, impl_item)| { - visitor.visit_impl_item(impl_item); - }); - }, - { - par_for_each_in(&self.foreign_items, |(_, foreign_item)| { - visitor.visit_foreign_item(foreign_item); - }); - } - ); + par_for_each_in(&self.owners.raw, |owner| match owner { + Some(OwnerNode::Item(item)) => visitor.visit_item(item), + Some(OwnerNode::ForeignItem(item)) => visitor.visit_foreign_item(item), + Some(OwnerNode::ImplItem(item)) => visitor.visit_impl_item(item), + Some(OwnerNode::TraitItem(item)) => visitor.visit_trait_item(item), + Some(OwnerNode::MacroDef(_)) | None => {} + }) + } + + pub fn items<'hir>(&'hir self) -> impl Iterator> + 'hir { + self.owners.iter().filter_map(|owner| match owner { + Some(OwnerNode::Item(item)) => Some(*item), + _ => None, + }) + } + + pub fn exported_macros<'hir>(&'hir self) -> impl Iterator> + 'hir { + self.owners.iter().filter_map(|owner| match owner { + Some(OwnerNode::MacroDef(macro_def)) => Some(*macro_def), + _ => None, + }) } } @@ -2953,6 +2936,163 @@ pub struct TraitCandidate { pub import_ids: SmallVec<[LocalDefId; 1]>, } +#[derive(Copy, Clone, Debug, HashStable_Generic)] +pub enum OwnerNode<'hir> { + Item(&'hir Item<'hir>), + ForeignItem(&'hir ForeignItem<'hir>), + TraitItem(&'hir TraitItem<'hir>), + ImplItem(&'hir ImplItem<'hir>), + MacroDef(&'hir MacroDef<'hir>), +} + +impl<'hir> OwnerNode<'hir> { + pub fn ident(&self) -> Ident { + match self { + OwnerNode::Item(Item { ident, .. }) + | OwnerNode::ForeignItem(ForeignItem { ident, .. }) + | OwnerNode::ImplItem(ImplItem { ident, .. }) + | OwnerNode::TraitItem(TraitItem { ident, .. }) + | OwnerNode::MacroDef(MacroDef { ident, .. }) => *ident, + } + } + + pub fn fn_decl(&self) -> Option<&FnDecl<'hir>> { + match self { + OwnerNode::TraitItem(TraitItem { kind: TraitItemKind::Fn(fn_sig, _), .. }) + | OwnerNode::ImplItem(ImplItem { kind: ImplItemKind::Fn(fn_sig, _), .. }) + | OwnerNode::Item(Item { kind: ItemKind::Fn(fn_sig, _, _), .. }) => Some(fn_sig.decl), + OwnerNode::ForeignItem(ForeignItem { + kind: ForeignItemKind::Fn(fn_decl, _, _), + .. + }) => Some(fn_decl), + _ => None, + } + } + + pub fn body_id(&self) -> Option { + match self { + OwnerNode::TraitItem(TraitItem { + kind: TraitItemKind::Fn(_, TraitFn::Provided(body_id)), + .. + }) + | OwnerNode::ImplItem(ImplItem { kind: ImplItemKind::Fn(_, body_id), .. }) + | OwnerNode::Item(Item { kind: ItemKind::Fn(.., body_id), .. }) => Some(*body_id), + _ => None, + } + } + + pub fn generics(&self) -> Option<&'hir Generics<'hir>> { + match self { + OwnerNode::TraitItem(TraitItem { generics, .. }) + | OwnerNode::ImplItem(ImplItem { generics, .. }) => Some(generics), + OwnerNode::Item(item) => item.kind.generics(), + _ => None, + } + } + + pub fn def_id(self) -> LocalDefId { + match self { + OwnerNode::Item(Item { def_id, .. }) + | OwnerNode::TraitItem(TraitItem { def_id, .. }) + | OwnerNode::ImplItem(ImplItem { def_id, .. }) + | OwnerNode::ForeignItem(ForeignItem { def_id, .. }) + | OwnerNode::MacroDef(MacroDef { def_id, .. }) => *def_id, + } + } + + pub fn expect_item(self) -> &'hir Item<'hir> { + match self { + OwnerNode::Item(n) => n, + OwnerNode::ForeignItem(_) + | OwnerNode::ImplItem(_) + | OwnerNode::TraitItem(_) + | OwnerNode::MacroDef(_) => panic!(), + } + } + + pub fn expect_foreign_item(self) -> &'hir ForeignItem<'hir> { + match self { + OwnerNode::ForeignItem(n) => n, + OwnerNode::Item(_) + | OwnerNode::ImplItem(_) + | OwnerNode::TraitItem(_) + | OwnerNode::MacroDef(_) => panic!(), + } + } + + pub fn expect_impl_item(self) -> &'hir ImplItem<'hir> { + match self { + OwnerNode::ImplItem(n) => n, + OwnerNode::ForeignItem(_) + | OwnerNode::Item(_) + | OwnerNode::TraitItem(_) + | OwnerNode::MacroDef(_) => panic!(), + } + } + + pub fn expect_trait_item(self) -> &'hir TraitItem<'hir> { + match self { + OwnerNode::TraitItem(n) => n, + OwnerNode::ForeignItem(_) + | OwnerNode::ImplItem(_) + | OwnerNode::Item(_) + | OwnerNode::MacroDef(_) => panic!(), + } + } + + pub fn expect_macro_def(self) -> &'hir MacroDef<'hir> { + match self { + OwnerNode::MacroDef(n) => n, + OwnerNode::ForeignItem(_) + | OwnerNode::ImplItem(_) + | OwnerNode::TraitItem(_) + | OwnerNode::Item(_) => panic!(), + } + } +} + +impl<'hir> Into> for &'hir Item<'hir> { + fn into(self) -> OwnerNode<'hir> { + OwnerNode::Item(self) + } +} + +impl<'hir> Into> for &'hir ForeignItem<'hir> { + fn into(self) -> OwnerNode<'hir> { + OwnerNode::ForeignItem(self) + } +} + +impl<'hir> Into> for &'hir ImplItem<'hir> { + fn into(self) -> OwnerNode<'hir> { + OwnerNode::ImplItem(self) + } +} + +impl<'hir> Into> for &'hir TraitItem<'hir> { + fn into(self) -> OwnerNode<'hir> { + OwnerNode::TraitItem(self) + } +} + +impl<'hir> Into> for &'hir MacroDef<'hir> { + fn into(self) -> OwnerNode<'hir> { + OwnerNode::MacroDef(self) + } +} + +impl<'hir> Into> for OwnerNode<'hir> { + fn into(self) -> Node<'hir> { + match self { + OwnerNode::Item(n) => Node::Item(n), + OwnerNode::ForeignItem(n) => Node::ForeignItem(n), + OwnerNode::ImplItem(n) => Node::ImplItem(n), + OwnerNode::TraitItem(n) => Node::TraitItem(n), + OwnerNode::MacroDef(n) => Node::MacroDef(n), + } + } +} + #[derive(Copy, Clone, Debug, HashStable_Generic)] pub enum Node<'hir> { Param(&'hir Param<'hir>), diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index c08f1f53218..83a4b2370d0 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -479,7 +479,7 @@ pub trait Visitor<'v>: Sized { /// Walks the contents of a crate. See also `Crate::visit_all_items`. pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) { visitor.visit_mod(&krate.item, krate.item.inner, CRATE_HIR_ID); - walk_list!(visitor, visit_macro_def, krate.exported_macros); + walk_list!(visitor, visit_macro_def, krate.exported_macros()); for (&id, attrs) in krate.attrs.iter() { for a in *attrs { visitor.visit_attribute(id, a) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index ccdbccae156..7d87e708eae 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -570,7 +570,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc { fn check_crate(&mut self, cx: &LateContext<'_>, krate: &hir::Crate<'_>) { self.check_missing_docs_attrs(cx, hir::CRATE_HIR_ID, krate.item.inner, "the", "crate"); - for macro_def in krate.exported_macros { + for macro_def in krate.exported_macros() { // Non exported macros should be skipped, since `missing_docs` only // applies to externally visible items. if !cx.access_levels.is_exported(macro_def.hir_id()) { diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index 069fa41fa88..4f223afcc27 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -33,11 +33,11 @@ fn lint_levels(tcx: TyCtxt<'_>, (): ()) -> LintLevelMap { let mut builder = LintLevelMapBuilder { levels, tcx, store }; let krate = tcx.hir().krate(); - builder.levels.id_to_set.reserve(krate.exported_macros.len() + 1); + builder.levels.id_to_set.reserve(krate.owners.len() + 1); let push = builder.levels.push(tcx.hir().attrs(hir::CRATE_HIR_ID), &store, true); builder.levels.register_id(hir::CRATE_HIR_ID); - for macro_def in krate.exported_macros { + for macro_def in krate.exported_macros() { builder.levels.register_id(macro_def.hir_id()); } intravisit::walk_crate(&mut builder, krate); diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index a4913a32e81..e40c885fce9 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -448,7 +448,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } krate.visit_all_item_likes(&mut self.as_deep_visitor()); - for macro_def in krate.exported_macros { + for macro_def in krate.exported_macros() { self.visit_macro_def(macro_def); } } diff --git a/compiler/rustc_middle/src/hir/map/collector.rs b/compiler/rustc_middle/src/hir/map/collector.rs index 8ffd98326f1..be8863be627 100644 --- a/compiler/rustc_middle/src/hir/map/collector.rs +++ b/compiler/rustc_middle/src/hir/map/collector.rs @@ -81,14 +81,10 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { let Crate { ref item, // These fields are handled separately: - exported_macros: _, non_exported_macro_attrs: _, - items: _, - trait_items: _, - impl_items: _, - foreign_items: _, - bodies: _, + owners: _, trait_impls: _, + bodies: _, body_ids: _, modules: _, proc_macros: _, diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 392372fad53..edcbce6b098 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -554,8 +554,8 @@ impl<'hir> Map<'hir> { where V: Visitor<'hir>, { - for id in self.krate().exported_macros { - visitor.visit_macro_def(self.expect_macro_def(id.hir_id())); + for macro_def in self.krate().exported_macros() { + visitor.visit_macro_def(macro_def); } } diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index b5733bd2edc..dc94124e62a 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -2307,7 +2307,7 @@ define_print_and_forward_display! { fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, Namespace, DefId)) { // Iterate all local crate items no matter where they are defined. let hir = tcx.hir(); - for item in hir.krate().items.values() { + for item in hir.krate().items() { if item.ident.name.as_str().is_empty() || matches!(item.kind, ItemKind::Use(_, _)) { continue; } diff --git a/compiler/rustc_passes/src/diagnostic_items.rs b/compiler/rustc_passes/src/diagnostic_items.rs index ddcc6fc123f..3cf1d0cdd94 100644 --- a/compiler/rustc_passes/src/diagnostic_items.rs +++ b/compiler/rustc_passes/src/diagnostic_items.rs @@ -108,7 +108,7 @@ fn diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> FxHashMap Visitor<'tcx> for LifetimeContext<'a, 'tcx> { let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); // Ensure that the parent of the def is an item, not HRTB let parent_id = self.tcx.hir().get_parent_node(hir_id); - let parent_is_item = if let Some(parent_def_id) = - parent_id.as_owner() - { - let parent_item_id = hir::ItemId { def_id: parent_def_id }; - let parent_impl_id = hir::ImplItemId { def_id: parent_def_id }; - let parent_trait_id = - hir::TraitItemId { def_id: parent_def_id }; - let parent_foreign_id = - hir::ForeignItemId { def_id: parent_def_id }; - let krate = self.tcx.hir().krate(); - - krate.items.contains_key(&parent_item_id) - || krate.impl_items.contains_key(&parent_impl_id) - || krate.trait_items.contains_key(&parent_trait_id) - || krate.foreign_items.contains_key(&parent_foreign_id) - } else { - false - }; + // FIXME(cjgillot) Can this check be replaced by + // `let parent_is_item = parent_id.is_owner();`? + let parent_is_item = + if let Some(parent_def_id) = parent_id.as_owner() { + matches!( + self.tcx.hir().krate().owners.get(parent_def_id), + Some(Some(_)), + ) + } else { + false + }; if !parent_is_item { if !self.trait_definition_only { diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index b563c4f4799..3621a6cb7d8 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -83,7 +83,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { // In the case of macros 2.0 (`pub macro`), and for built-in `derive`s or attributes as // well (_e.g._, `Copy`), these are wrongly bundled in there too, so we need to fix that by // moving them back to their correct locations. - 'exported_macros: for def in krate.exported_macros { + 'exported_macros: for def in krate.exported_macros() { // The `def` of a macro in `exported_macros` should correspond to either: // - a `#[macro_export] macro_rules!` macro, // - a built-in `derive` (or attribute) macro such as the ones in `::core`,