]> git.lizzy.rs Git - rust.git/commitdiff
Create the `hir_to_node_id` map before `TyCtxt`
authorJohn Kåre Alsaker <john.kare.alsaker@gmail.com>
Wed, 12 Feb 2020 14:11:33 +0000 (15:11 +0100)
committerJohn Kåre Alsaker <john.kare.alsaker@gmail.com>
Sat, 14 Mar 2020 21:52:31 +0000 (22:52 +0100)
src/librustc/hir/map/collector.rs
src/librustc/hir/map/definitions.rs
src/librustc/hir/map/mod.rs

index da2a8c05af5ec1e0a6768b80f9067bf19310b7cf..e8233c0446d1f92dcbf875b03f0d2695d92f5cd9 100644 (file)
@@ -4,7 +4,6 @@
 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<HirId, NodeId>,
 
     hcx: StableHashingContext<'a>,
 
@@ -98,7 +96,6 @@ pub(super) fn root(
         arena: &'hir Arena<'hir>,
         krate: &'hir Crate<'hir>,
         definitions: &'a definitions::Definitions,
-        hir_to_node_id: &'a FxHashMap<HirId, NodeId>,
         mut hcx: StableHashingContext<'a>,
     ) -> NodeCollector<'a, 'hir> {
         let root_mod_def_path_hash = definitions.def_path_hash(CRATE_DEF_INDEX);
@@ -131,7 +128,6 @@ pub(super) fn root(
             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 @@ fn insert_with_hash(&mut self, span: Span, hir_id: HirId, node: Node<'hir>, hash
         // 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 @@ fn visit_item(&mut self, i: &'hir Item<'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 @@ fn visit_generic_param(&mut self, param: &'hir GenericParam<'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 @@ fn visit_trait_item(&mut self, ti: &'hir TraitItem<'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 @@ fn visit_vis(&mut self, visibility: &'hir Visibility<'hir>) {
     }
 
     fn visit_macro_def(&mut self, macro_def: &'hir MacroDef<'hir>) {
-        let node_id = self.hir_to_node_id[&macro_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| {
index 50117f73d48ccce701580e81f206a0ce71f1d9cb..42ccf7e72504b7f34bb39bcaa1cfc013a58edc84 100644 (file)
@@ -80,7 +80,11 @@ pub struct Definitions {
     table: DefPathTable,
     node_to_def_index: NodeMap<DefIndex>,
     def_index_to_node: IndexVec<DefIndex, ast::NodeId>,
+
     pub(super) node_to_hir_id: IndexVec<ast::NodeId, hir::HirId>,
+    /// The reverse mapping of `node_to_hir_id`.
+    pub(super) hir_to_node_id: FxHashMap<hir::HirId, ast::NodeId>,
+
     /// 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<ExpnId, DefId>,
@@ -346,6 +350,11 @@ pub fn as_local_hir_id(&self, def_id: DefId) -> Option<hir::HirId> {
         }
     }
 
+    #[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 @@ pub fn init_node_id_to_hir_id_mapping(&mut self, mapping: IndexVec<ast::NodeId,
             "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 {
index 3bdd2b1ac3ca73cbdadfb3336254e49f5cfe3980..bcbb6f3ec31e6e62c2e41a7b0680dd3636d4a19d 100644 (file)
@@ -7,7 +7,6 @@
 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<DefIndex, HirOwnerData<'hir>>,
-
-    /// The reverse mapping of `node_to_hir_id`.
-    pub(super) hir_to_node_id: FxHashMap<HirId, NodeId>,
 }
 
 #[derive(Copy, Clone)]
@@ -251,7 +247,7 @@ pub fn as_local_hir_id(&self, def_id: DefId) -> Option<HirId> {
 
     #[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
 }