Skip to content

Commit 13104ef

Browse files
committed
Pre-compute def_id_to_hir_id table
1 parent 94817e3 commit 13104ef

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/librustc_hir/definitions.rs

+18-19
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,12 @@ pub struct Definitions {
8181

8282
def_id_to_span: IndexVec<LocalDefId, Span>,
8383

84-
// FIXME(eddyb) don't go through `ast::NodeId` to convert between `HirId`
85-
// and `LocalDefId` - ideally all `LocalDefId`s would be HIR owners.
8684
node_id_to_def_id: FxHashMap<ast::NodeId, LocalDefId>,
8785
def_id_to_node_id: IndexVec<LocalDefId, ast::NodeId>,
8886

89-
pub(super) node_id_to_hir_id: IndexVec<ast::NodeId, Option<hir::HirId>>,
90-
/// The pre-computed mapping of `hir_id_to_node_id` -> `node_id_to_def_id`.
87+
// FIXME(eddyb) ideally all `LocalDefId`s would be HIR owners.
88+
pub(super) def_id_to_hir_id: IndexVec<LocalDefId, Option<hir::HirId>>,
89+
/// The reverse mapping of `def_id_to_hir_id`.
9190
pub(super) hir_id_to_def_id: FxHashMap<hir::HirId, LocalDefId>,
9291

9392
/// If `ExpnId` is an ID of some macro expansion,
@@ -327,9 +326,7 @@ impl Definitions {
327326

328327
#[inline]
329328
pub fn local_def_id(&self, node: ast::NodeId) -> LocalDefId {
330-
self.opt_local_def_id(node).unwrap_or_else(|| {
331-
panic!("no entry for node id: `{:?}` / `{:?}`", node, self.node_id_to_hir_id.get(node))
332-
})
329+
self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{:?}`", node))
333330
}
334331

335332
#[inline]
@@ -339,14 +336,12 @@ impl Definitions {
339336

340337
#[inline]
341338
pub fn local_def_id_to_hir_id(&self, id: LocalDefId) -> hir::HirId {
342-
let node_id = self.def_id_to_node_id[id];
343-
self.node_id_to_hir_id[node_id].unwrap()
339+
self.def_id_to_hir_id[id].unwrap()
344340
}
345341

346342
#[inline]
347343
pub fn opt_local_def_id_to_hir_id(&self, id: LocalDefId) -> Option<hir::HirId> {
348-
let node_id = self.def_id_to_node_id[id];
349-
self.node_id_to_hir_id[node_id]
344+
self.def_id_to_hir_id[id]
350345
}
351346

352347
#[inline]
@@ -461,16 +456,20 @@ impl Definitions {
461456
mapping: IndexVec<ast::NodeId, Option<hir::HirId>>,
462457
) {
463458
assert!(
464-
self.node_id_to_hir_id.is_empty(),
465-
"trying to initialize `NodeId` -> `HirId` mapping twice"
459+
self.def_id_to_hir_id.is_empty(),
460+
"trying to initialize `LocalDefId` <-> `HirId` mappings twice"
466461
);
467-
self.node_id_to_hir_id = mapping;
468462

469-
// Build the pre-computed mapping of `hir_id_to_node_id` -> `node_id_to_def_id`.
470-
self.hir_id_to_def_id = self
471-
.node_id_to_hir_id
472-
.iter_enumerated()
473-
.filter_map(|(node_id, &hir_id)| {
463+
self.def_id_to_hir_id = self
464+
.def_id_to_node_id
465+
.iter()
466+
.map(|&node_id| mapping.get(node_id).and_then(|&hir_id| hir_id))
467+
.collect();
468+
469+
// Build the reverse mapping of `def_id_to_hir_id`.
470+
self.hir_id_to_def_id = mapping
471+
.into_iter_enumerated()
472+
.filter_map(|(node_id, hir_id)| {
474473
hir_id.and_then(|hir_id| {
475474
self.node_id_to_def_id.get(&node_id).map(|&def_id| (hir_id, def_id))
476475
})

0 commit comments

Comments
 (0)