Skip to content

Commit 80132c3

Browse files
cjgillotspastorino
authored andcommitted
Store hir_id_to_def_id in OwnerInfo.
1 parent 17dfae7 commit 80132c3

File tree

5 files changed

+31
-19
lines changed

5 files changed

+31
-19
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
479479
let attrs = std::mem::take(&mut self.attrs);
480480
let mut bodies = std::mem::take(&mut self.bodies);
481481
let local_node_ids = std::mem::take(&mut self.local_node_ids);
482+
483+
let local_id_to_def_id = local_node_ids
484+
.iter()
485+
.filter_map(|&node_id| {
486+
let def_id = self.resolver.opt_local_def_id(node_id)?;
487+
let hir_id = self.node_id_to_hir_id[node_id]?;
488+
Some((hir_id.local_id, def_id))
489+
})
490+
.collect();
491+
482492
let trait_map = local_node_ids
483493
.into_iter()
484494
.filter_map(|node_id| {
@@ -501,7 +511,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
501511
let (hash_including_bodies, hash_without_bodies) = self.hash_owner(node, &bodies);
502512
let (nodes, parenting) =
503513
index::index_hir(self.sess, self.resolver.definitions(), node, &bodies);
504-
let nodes = hir::OwnerNodes { hash_including_bodies, hash_without_bodies, nodes, bodies };
514+
let nodes = hir::OwnerNodes {
515+
hash_including_bodies,
516+
hash_without_bodies,
517+
nodes,
518+
bodies,
519+
local_id_to_def_id,
520+
};
505521
let attrs = {
506522
let mut hcx = self.resolver.create_stable_hashing_context();
507523
let mut stable_hasher = StableHasher::new();

compiler/rustc_hir/src/definitions.rs

-14
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ pub struct Definitions {
107107
/// Their `HirId`s are defined by their position while lowering the enclosing owner.
108108
// FIXME(cjgillot) Some `LocalDefId`s from `use` items are dropped during lowering and lack a `HirId`.
109109
pub(super) def_id_to_hir_id: IndexVec<LocalDefId, Option<hir::HirId>>,
110-
/// The reverse mapping of `def_id_to_hir_id`.
111-
pub(super) hir_id_to_def_id: FxHashMap<hir::HirId, LocalDefId>,
112110

113111
/// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
114112
expansions_that_defined: FxHashMap<LocalDefId, ExpnId>,
@@ -330,11 +328,6 @@ impl Definitions {
330328
self.def_id_to_hir_id[id].unwrap()
331329
}
332330

333-
#[inline]
334-
pub fn opt_hir_id_to_local_def_id(&self, hir_id: hir::HirId) -> Option<LocalDefId> {
335-
self.hir_id_to_def_id.get(&hir_id).copied()
336-
}
337-
338331
/// Adds a root definition (no parent) and a few other reserved definitions.
339332
pub fn new(stable_crate_id: StableCrateId, crate_span: Span) -> Definitions {
340333
let key = DefKey {
@@ -362,7 +355,6 @@ impl Definitions {
362355
Definitions {
363356
table,
364357
def_id_to_hir_id: Default::default(),
365-
hir_id_to_def_id: Default::default(),
366358
expansions_that_defined: Default::default(),
367359
def_id_to_span,
368360
stable_crate_id,
@@ -425,12 +417,6 @@ impl Definitions {
425417
"trying to initialize `LocalDefId` <-> `HirId` mappings twice"
426418
);
427419

428-
// Build the reverse mapping of `def_id_to_hir_id`.
429-
self.hir_id_to_def_id = mapping
430-
.iter_enumerated()
431-
.filter_map(|(def_id, hir_id)| hir_id.map(|hir_id| (hir_id, def_id)))
432-
.collect();
433-
434420
self.def_id_to_hir_id = mapping;
435421
}
436422

compiler/rustc_hir/src/hir.rs

+2
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,8 @@ pub struct OwnerNodes<'tcx> {
707707
pub nodes: IndexVec<ItemLocalId, Option<ParentedNode<'tcx>>>,
708708
/// Content of local bodies.
709709
pub bodies: SortedMap<ItemLocalId, &'tcx Body<'tcx>>,
710+
/// Non-owning definitions contained in this owner.
711+
pub local_id_to_def_id: SortedMap<ItemLocalId, LocalDefId>,
710712
}
711713

712714
/// Full information resulting from lowering an AST node.

compiler/rustc_hir/src/stable_hash_impls.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,13 @@ impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'
208208
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
209209
// We ignore the `nodes` and `bodies` fields since these refer to information included in
210210
// `hash` which is hashed in the collector and used for the crate hash.
211-
let OwnerNodes { hash_including_bodies, hash_without_bodies: _, nodes: _, bodies: _ } =
212-
*self;
211+
let OwnerNodes {
212+
hash_including_bodies,
213+
hash_without_bodies: _,
214+
nodes: _,
215+
bodies: _,
216+
local_id_to_def_id: _,
217+
} = *self;
213218
hash_including_bodies.hash_stable(hcx, hasher);
214219
}
215220
}

compiler/rustc_middle/src/hir/map/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,11 @@ impl<'hir> Map<'hir> {
204204
if hir_id.local_id == ItemLocalId::new(0) {
205205
Some(hir_id.owner)
206206
} else {
207-
// FIXME(#85914) is this access safe for incr. comp.?
208-
self.tcx.untracked_resolutions.definitions.opt_hir_id_to_local_def_id(hir_id)
207+
self.tcx
208+
.hir_owner_nodes(hir_id.owner)?
209+
.local_id_to_def_id
210+
.get(&hir_id.local_id)
211+
.copied()
209212
}
210213
}
211214

0 commit comments

Comments
 (0)