Skip to content

Commit 739a1ef

Browse files
committed
Create the hir_to_node_id map before TyCtxt
1 parent 396aeb8 commit 739a1ef

File tree

3 files changed

+25
-31
lines changed

3 files changed

+25
-31
lines changed

src/librustc/hir/map/collector.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::hir::map::{Entry, HirOwnerData, Map};
44
use crate::hir::{HirItem, HirOwner, HirOwnerItems};
55
use crate::ich::StableHashingContext;
66
use crate::middle::cstore::CrateStore;
7-
use rustc_ast::ast::NodeId;
87
use rustc_data_structures::fingerprint::Fingerprint;
98
use rustc_data_structures::fx::FxHashMap;
109
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -39,7 +38,6 @@ pub(super) struct NodeCollector<'a, 'hir> {
3938
current_dep_node_owner: DefIndex,
4039

4140
definitions: &'a definitions::Definitions,
42-
hir_to_node_id: &'a FxHashMap<HirId, NodeId>,
4341

4442
hcx: StableHashingContext<'a>,
4543

@@ -98,7 +96,6 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
9896
arena: &'hir Arena<'hir>,
9997
krate: &'hir Crate<'hir>,
10098
definitions: &'a definitions::Definitions,
101-
hir_to_node_id: &'a FxHashMap<HirId, NodeId>,
10299
mut hcx: StableHashingContext<'a>,
103100
) -> NodeCollector<'a, 'hir> {
104101
let root_mod_def_path_hash = definitions.def_path_hash(CRATE_DEF_INDEX);
@@ -131,7 +128,6 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
131128
parent_node: hir::CRATE_HIR_ID,
132129
current_dep_node_owner: CRATE_DEF_INDEX,
133130
definitions,
134-
hir_to_node_id,
135131
hcx,
136132
hir_body_nodes,
137133
map: (0..definitions.def_index_count())
@@ -244,7 +240,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
244240
// Make sure that the DepNode of some node coincides with the HirId
245241
// owner of that node.
246242
if cfg!(debug_assertions) {
247-
let node_id = self.hir_to_node_id[&hir_id];
243+
let node_id = self.definitions.hir_to_node_id(hir_id);
248244
assert_eq!(self.definitions.node_to_hir_id(node_id), hir_id);
249245

250246
if hir_id.owner != self.current_dep_node_owner {
@@ -345,7 +341,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
345341
debug!("visit_item: {:?}", i);
346342
debug_assert_eq!(
347343
i.hir_id.owner,
348-
self.definitions.opt_def_index(self.hir_to_node_id[&i.hir_id]).unwrap()
344+
self.definitions.opt_def_index(self.definitions.hir_to_node_id(i.hir_id)).unwrap()
349345
);
350346
self.with_dep_node_owner(i.hir_id.owner, i, |this, hash| {
351347
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> {
377373
fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) {
378374
debug_assert_eq!(
379375
ti.hir_id.owner,
380-
self.definitions.opt_def_index(self.hir_to_node_id[&ti.hir_id]).unwrap()
376+
self.definitions.opt_def_index(self.definitions.hir_to_node_id(ti.hir_id)).unwrap()
381377
);
382378
self.with_dep_node_owner(ti.hir_id.owner, ti, |this, hash| {
383379
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> {
391387
fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) {
392388
debug_assert_eq!(
393389
ii.hir_id.owner,
394-
self.definitions.opt_def_index(self.hir_to_node_id[&ii.hir_id]).unwrap()
390+
self.definitions.opt_def_index(self.definitions.hir_to_node_id(ii.hir_id)).unwrap()
395391
);
396392
self.with_dep_node_owner(ii.hir_id.owner, ii, |this, hash| {
397393
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> {
510506
}
511507

512508
fn visit_macro_def(&mut self, macro_def: &'hir MacroDef<'hir>) {
513-
let node_id = self.hir_to_node_id[&macro_def.hir_id];
509+
let node_id = self.definitions.hir_to_node_id(macro_def.hir_id);
514510
let def_index = self.definitions.opt_def_index(node_id).unwrap();
515511

516512
self.with_dep_node_owner(def_index, macro_def, |this, hash| {

src/librustc/hir/map/definitions.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ pub struct Definitions {
8080
table: DefPathTable,
8181
node_to_def_index: NodeMap<DefIndex>,
8282
def_index_to_node: IndexVec<DefIndex, ast::NodeId>,
83+
8384
pub(super) node_to_hir_id: IndexVec<ast::NodeId, hir::HirId>,
85+
/// The reverse mapping of `node_to_hir_id`.
86+
pub(super) hir_to_node_id: FxHashMap<hir::HirId, ast::NodeId>,
87+
8488
/// If `ExpnId` is an ID of some macro expansion,
8589
/// then `DefId` is the normal module (`mod`) in which the expanded macro was defined.
8690
parent_modules_of_macro_defs: FxHashMap<ExpnId, DefId>,
@@ -346,6 +350,11 @@ impl Definitions {
346350
}
347351
}
348352

353+
#[inline]
354+
pub fn hir_to_node_id(&self, hir_id: hir::HirId) -> ast::NodeId {
355+
self.hir_to_node_id[&hir_id]
356+
}
357+
349358
#[inline]
350359
pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId {
351360
self.node_to_hir_id[node_id]
@@ -472,6 +481,13 @@ impl Definitions {
472481
"trying to initialize `NodeId` -> `HirId` mapping twice"
473482
);
474483
self.node_to_hir_id = mapping;
484+
485+
// Build the reverse mapping of `node_to_hir_id`.
486+
self.hir_to_node_id = self
487+
.node_to_hir_id
488+
.iter_enumerated()
489+
.map(|(node_id, &hir_id)| (hir_id, node_id))
490+
.collect();
475491
}
476492

477493
pub fn expansion_that_defined(&self, index: DefIndex) -> ExpnId {

src/librustc/hir/map/mod.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::hir::{HirOwner, HirOwnerItems};
77
use crate::ty::query::Providers;
88
use crate::ty::TyCtxt;
99
use rustc_ast::ast::{self, Name, NodeId};
10-
use rustc_data_structures::fx::FxHashMap;
1110
use rustc_data_structures::svh::Svh;
1211
use rustc_hir::def::{DefKind, Res};
1312
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, LOCAL_CRATE};
@@ -139,9 +138,6 @@ pub struct IndexedHir<'hir> {
139138
pub crate_hash: Svh,
140139

141140
pub(super) map: IndexVec<DefIndex, HirOwnerData<'hir>>,
142-
143-
/// The reverse mapping of `node_to_hir_id`.
144-
pub(super) hir_to_node_id: FxHashMap<HirId, NodeId>,
145141
}
146142

147143
#[derive(Copy, Clone)]
@@ -251,7 +247,7 @@ impl<'hir> Map<'hir> {
251247

252248
#[inline]
253249
pub fn hir_to_node_id(&self, hir_id: HirId) -> NodeId {
254-
self.tcx.index_hir(LOCAL_CRATE).hir_to_node_id[&hir_id]
250+
self.tcx.definitions.hir_to_node_id(hir_id)
255251
}
256252

257253
#[inline]
@@ -1033,33 +1029,19 @@ pub(super) fn index_hir<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> &'tcx Indexe
10331029

10341030
let _prof_timer = tcx.sess.prof.generic_activity("build_hir_map");
10351031

1036-
// Build the reverse mapping of `node_to_hir_id`.
1037-
let hir_to_node_id = tcx
1038-
.definitions
1039-
.node_to_hir_id
1040-
.iter_enumerated()
1041-
.map(|(node_id, &hir_id)| (hir_id, node_id))
1042-
.collect();
1043-
10441032
let (map, crate_hash) = {
10451033
let hcx = tcx.create_stable_hashing_context();
10461034

1047-
let mut collector = NodeCollector::root(
1048-
tcx.sess,
1049-
&**tcx.arena,
1050-
tcx.untracked_crate,
1051-
&tcx.definitions,
1052-
&hir_to_node_id,
1053-
hcx,
1054-
);
1035+
let mut collector =
1036+
NodeCollector::root(tcx.sess, &**tcx.arena, tcx.untracked_crate, &tcx.definitions, hcx);
10551037
intravisit::walk_crate(&mut collector, tcx.untracked_crate);
10561038

10571039
let crate_disambiguator = tcx.sess.local_crate_disambiguator();
10581040
let cmdline_args = tcx.sess.opts.dep_tracking_hash();
10591041
collector.finalize_and_compute_crate_hash(crate_disambiguator, &*tcx.cstore, cmdline_args)
10601042
};
10611043

1062-
let map = tcx.arena.alloc(IndexedHir { crate_hash, map, hir_to_node_id });
1044+
let map = tcx.arena.alloc(IndexedHir { crate_hash, map });
10631045

10641046
map
10651047
}

0 commit comments

Comments
 (0)