Skip to content

Commit

Permalink
Replace def_id_to_node_id table by a def_id_to_hir_table
Browse files Browse the repository at this point in the history
Since the removal of all calls to `as_local_node_id`, the `def_id_to_node_id`
table was useless. It is being replaced by a `def_id_to_hir_id` table
which allows to lookup `HirId` from `LocalDefId` without indirection
through `NodeId` tables.
  • Loading branch information
marmeladema committed Jun 6, 2020
1 parent 118b505 commit 67fcf63
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/librustc_hir/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ pub struct Definitions {

// FIXME(eddyb) don't go through `ast::NodeId` to convert between `HirId`
// and `LocalDefId` - ideally all `LocalDefId`s would be HIR owners.
def_id_to_hir_id: IndexVec<LocalDefId, Option<hir::HirId>>,

node_id_to_def_id: FxHashMap<ast::NodeId, LocalDefId>,
def_id_to_node_id: IndexVec<LocalDefId, ast::NodeId>,

pub(super) node_id_to_hir_id: IndexVec<ast::NodeId, Option<hir::HirId>>,
/// The reverse mapping of `node_id_to_hir_id`.
Expand Down Expand Up @@ -354,14 +355,12 @@ impl Definitions {

#[inline]
pub fn local_def_id_to_hir_id(&self, id: LocalDefId) -> hir::HirId {
let node_id = self.def_id_to_node_id[id];
self.node_id_to_hir_id[node_id].unwrap()
self.def_id_to_hir_id[id].unwrap()
}

#[inline]
pub fn opt_local_def_id_to_hir_id(&self, id: LocalDefId) -> Option<hir::HirId> {
let node_id = self.def_id_to_node_id[id];
self.node_id_to_hir_id[node_id]
self.def_id_to_hir_id[id]
}

#[inline]
Expand Down Expand Up @@ -397,7 +396,7 @@ impl Definitions {
let root = LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) };
assert_eq!(root.local_def_index, CRATE_DEF_INDEX);

assert_eq!(self.def_id_to_node_id.push(ast::CRATE_NODE_ID), root);
assert_eq!(self.def_id_to_hir_id.push(None), root);
assert_eq!(self.def_id_to_span.push(rustc_span::DUMMY_SP), root);

self.node_id_to_def_id.insert(ast::CRATE_NODE_ID, root);
Expand Down Expand Up @@ -452,7 +451,7 @@ impl Definitions {
// Create the definition.
let def_id = LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) };

assert_eq!(self.def_id_to_node_id.push(node_id), def_id);
assert_eq!(self.def_id_to_hir_id.push(None), def_id);
assert_eq!(self.def_id_to_span.push(span), def_id);

// Some things for which we allocate `LocalDefId`s don't correspond to
Expand Down Expand Up @@ -482,6 +481,11 @@ impl Definitions {
);
self.node_id_to_hir_id = mapping;

for (&node_id, &def_id) in self.node_id_to_def_id.iter() {
self.def_id_to_hir_id[def_id] =
self.node_id_to_hir_id.get(node_id).copied().unwrap_or(None);
}

// Build the reverse mapping of `node_id_to_hir_id`.
self.hir_id_to_node_id = self
.node_id_to_hir_id
Expand Down

0 comments on commit 67fcf63

Please sign in to comment.