diff --git a/src/librustc/hir/map/collector.rs b/src/librustc/hir/map/collector.rs index da2a8c05af5e..e8233c0446d1 100644 --- a/src/librustc/hir/map/collector.rs +++ b/src/librustc/hir/map/collector.rs @@ -4,7 +4,6 @@ use crate::hir::map::{Entry, HirOwnerData, Map}; use crate::hir::{HirItem, HirOwner, HirOwnerItems}; use crate::ich::StableHashingContext; use crate::middle::cstore::CrateStore; -use rustc_ast::ast::NodeId; use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; @@ -39,7 +38,6 @@ pub(super) struct NodeCollector<'a, 'hir> { current_dep_node_owner: DefIndex, definitions: &'a definitions::Definitions, - hir_to_node_id: &'a FxHashMap, hcx: StableHashingContext<'a>, @@ -98,7 +96,6 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { arena: &'hir Arena<'hir>, krate: &'hir Crate<'hir>, definitions: &'a definitions::Definitions, - hir_to_node_id: &'a FxHashMap, mut hcx: StableHashingContext<'a>, ) -> NodeCollector<'a, 'hir> { let root_mod_def_path_hash = definitions.def_path_hash(CRATE_DEF_INDEX); @@ -131,7 +128,6 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { parent_node: hir::CRATE_HIR_ID, current_dep_node_owner: CRATE_DEF_INDEX, definitions, - hir_to_node_id, hcx, hir_body_nodes, map: (0..definitions.def_index_count()) @@ -244,7 +240,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { // Make sure that the DepNode of some node coincides with the HirId // owner of that node. if cfg!(debug_assertions) { - let node_id = self.hir_to_node_id[&hir_id]; + let node_id = self.definitions.hir_to_node_id(hir_id); assert_eq!(self.definitions.node_to_hir_id(node_id), hir_id); if hir_id.owner != self.current_dep_node_owner { @@ -345,7 +341,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { debug!("visit_item: {:?}", i); debug_assert_eq!( i.hir_id.owner, - self.definitions.opt_def_index(self.hir_to_node_id[&i.hir_id]).unwrap() + self.definitions.opt_def_index(self.definitions.hir_to_node_id(i.hir_id)).unwrap() ); self.with_dep_node_owner(i.hir_id.owner, i, |this, hash| { this.insert_with_hash(i.span, i.hir_id, Node::Item(i), hash); @@ -377,7 +373,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) { debug_assert_eq!( ti.hir_id.owner, - self.definitions.opt_def_index(self.hir_to_node_id[&ti.hir_id]).unwrap() + self.definitions.opt_def_index(self.definitions.hir_to_node_id(ti.hir_id)).unwrap() ); self.with_dep_node_owner(ti.hir_id.owner, ti, |this, hash| { this.insert_with_hash(ti.span, ti.hir_id, Node::TraitItem(ti), hash); @@ -391,7 +387,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) { debug_assert_eq!( ii.hir_id.owner, - self.definitions.opt_def_index(self.hir_to_node_id[&ii.hir_id]).unwrap() + self.definitions.opt_def_index(self.definitions.hir_to_node_id(ii.hir_id)).unwrap() ); self.with_dep_node_owner(ii.hir_id.owner, ii, |this, hash| { this.insert_with_hash(ii.span, ii.hir_id, Node::ImplItem(ii), hash); @@ -510,7 +506,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { } fn visit_macro_def(&mut self, macro_def: &'hir MacroDef<'hir>) { - let node_id = self.hir_to_node_id[¯o_def.hir_id]; + let node_id = self.definitions.hir_to_node_id(macro_def.hir_id); let def_index = self.definitions.opt_def_index(node_id).unwrap(); self.with_dep_node_owner(def_index, macro_def, |this, hash| { diff --git a/src/librustc/hir/map/definitions.rs b/src/librustc/hir/map/definitions.rs index 50117f73d48c..42ccf7e72504 100644 --- a/src/librustc/hir/map/definitions.rs +++ b/src/librustc/hir/map/definitions.rs @@ -80,7 +80,11 @@ pub struct Definitions { table: DefPathTable, node_to_def_index: NodeMap, def_index_to_node: IndexVec, + pub(super) node_to_hir_id: IndexVec, + /// The reverse mapping of `node_to_hir_id`. + pub(super) hir_to_node_id: FxHashMap, + /// If `ExpnId` is an ID of some macro expansion, /// then `DefId` is the normal module (`mod`) in which the expanded macro was defined. parent_modules_of_macro_defs: FxHashMap, @@ -346,6 +350,11 @@ impl Definitions { } } + #[inline] + pub fn hir_to_node_id(&self, hir_id: hir::HirId) -> ast::NodeId { + self.hir_to_node_id[&hir_id] + } + #[inline] pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId { self.node_to_hir_id[node_id] @@ -472,6 +481,13 @@ impl Definitions { "trying to initialize `NodeId` -> `HirId` mapping twice" ); self.node_to_hir_id = mapping; + + // Build the reverse mapping of `node_to_hir_id`. + self.hir_to_node_id = self + .node_to_hir_id + .iter_enumerated() + .map(|(node_id, &hir_id)| (hir_id, node_id)) + .collect(); } pub fn expansion_that_defined(&self, index: DefIndex) -> ExpnId { diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 3bdd2b1ac3ca..bcbb6f3ec31e 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -7,7 +7,6 @@ use crate::hir::{HirOwner, HirOwnerItems}; use crate::ty::query::Providers; use crate::ty::TyCtxt; use rustc_ast::ast::{self, Name, NodeId}; -use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::svh::Svh; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, LOCAL_CRATE}; @@ -139,9 +138,6 @@ pub struct IndexedHir<'hir> { pub crate_hash: Svh, pub(super) map: IndexVec>, - - /// The reverse mapping of `node_to_hir_id`. - pub(super) hir_to_node_id: FxHashMap, } #[derive(Copy, Clone)] @@ -251,7 +247,7 @@ impl<'hir> Map<'hir> { #[inline] pub fn hir_to_node_id(&self, hir_id: HirId) -> NodeId { - self.tcx.index_hir(LOCAL_CRATE).hir_to_node_id[&hir_id] + self.tcx.definitions.hir_to_node_id(hir_id) } #[inline] @@ -1033,25 +1029,11 @@ pub(super) fn index_hir<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> &'tcx Indexe let _prof_timer = tcx.sess.prof.generic_activity("build_hir_map"); - // Build the reverse mapping of `node_to_hir_id`. - let hir_to_node_id = tcx - .definitions - .node_to_hir_id - .iter_enumerated() - .map(|(node_id, &hir_id)| (hir_id, node_id)) - .collect(); - let (map, crate_hash) = { let hcx = tcx.create_stable_hashing_context(); - let mut collector = NodeCollector::root( - tcx.sess, - &**tcx.arena, - tcx.untracked_crate, - &tcx.definitions, - &hir_to_node_id, - hcx, - ); + let mut collector = + NodeCollector::root(tcx.sess, &**tcx.arena, tcx.untracked_crate, &tcx.definitions, hcx); intravisit::walk_crate(&mut collector, tcx.untracked_crate); let crate_disambiguator = tcx.sess.local_crate_disambiguator(); @@ -1059,7 +1041,7 @@ pub(super) fn index_hir<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> &'tcx Indexe collector.finalize_and_compute_crate_hash(crate_disambiguator, &*tcx.cstore, cmdline_args) }; - let map = tcx.arena.alloc(IndexedHir { crate_hash, map, hir_to_node_id }); + let map = tcx.arena.alloc(IndexedHir { crate_hash, map }); map }