Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store hir_id_to_def_id in OwnerInfo. #93301

Merged
merged 3 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let attrs = std::mem::take(&mut self.attrs);
let mut bodies = std::mem::take(&mut self.bodies);
let local_node_ids = std::mem::take(&mut self.local_node_ids);

let local_id_to_def_id = local_node_ids
.iter()
.filter_map(|&node_id| {
let hir_id = self.node_id_to_hir_id[node_id]?;
if hir_id.local_id == hir::ItemLocalId::new(0) {
None
} else {
let def_id = self.resolver.opt_local_def_id(node_id)?;
Some((hir_id.local_id, def_id))
}
})
.collect();

let trait_map = local_node_ids
.into_iter()
.filter_map(|node_id| {
Expand All @@ -501,7 +515,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let (hash_including_bodies, hash_without_bodies) = self.hash_owner(node, &bodies);
let (nodes, parenting) =
index::index_hir(self.sess, self.resolver.definitions(), node, &bodies);
let nodes = hir::OwnerNodes { hash_including_bodies, hash_without_bodies, nodes, bodies };
let nodes = hir::OwnerNodes {
hash_including_bodies,
hash_without_bodies,
nodes,
bodies,
local_id_to_def_id,
};
let attrs = {
let mut hcx = self.resolver.create_stable_hashing_context();
let mut stable_hasher = StableHasher::new();
Expand Down
14 changes: 0 additions & 14 deletions compiler/rustc_hir/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ pub struct Definitions {
/// Their `HirId`s are defined by their position while lowering the enclosing owner.
// FIXME(cjgillot) Some `LocalDefId`s from `use` items are dropped during lowering and lack a `HirId`.
pub(super) def_id_to_hir_id: IndexVec<LocalDefId, Option<hir::HirId>>,
/// The reverse mapping of `def_id_to_hir_id`.
pub(super) hir_id_to_def_id: FxHashMap<hir::HirId, LocalDefId>,

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

#[inline]
pub fn opt_hir_id_to_local_def_id(&self, hir_id: hir::HirId) -> Option<LocalDefId> {
self.hir_id_to_def_id.get(&hir_id).copied()
}

/// Adds a root definition (no parent) and a few other reserved definitions.
pub fn new(stable_crate_id: StableCrateId, crate_span: Span) -> Definitions {
let key = DefKey {
Expand Down Expand Up @@ -362,7 +355,6 @@ impl Definitions {
Definitions {
table,
def_id_to_hir_id: Default::default(),
hir_id_to_def_id: Default::default(),
expansions_that_defined: Default::default(),
def_id_to_span,
stable_crate_id,
Expand Down Expand Up @@ -425,12 +417,6 @@ impl Definitions {
"trying to initialize `LocalDefId` <-> `HirId` mappings twice"
);

// Build the reverse mapping of `def_id_to_hir_id`.
self.hir_id_to_def_id = mapping
.iter_enumerated()
.filter_map(|(def_id, hir_id)| hir_id.map(|hir_id| (hir_id, def_id)))
.collect();

self.def_id_to_hir_id = mapping;
}

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,8 @@ pub struct OwnerNodes<'tcx> {
pub nodes: IndexVec<ItemLocalId, Option<ParentedNode<'tcx>>>,
/// Content of local bodies.
pub bodies: SortedMap<ItemLocalId, &'tcx Body<'tcx>>,
/// Non-owning definitions contained in this owner.
pub local_id_to_def_id: SortedMap<ItemLocalId, LocalDefId>,
}

/// Full information resulting from lowering an AST node.
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_hir/src/stable_hash_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,13 @@ impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
// We ignore the `nodes` and `bodies` fields since these refer to information included in
// `hash` which is hashed in the collector and used for the crate hash.
let OwnerNodes { hash_including_bodies, hash_without_bodies: _, nodes: _, bodies: _ } =
*self;
let OwnerNodes {
hash_including_bodies,
hash_without_bodies: _,
nodes: _,
bodies: _,
local_id_to_def_id: _,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! I think that skipping the hash for local_id_to_def_id is safe, but the rationale definitely deserves a comment: all the information used to create a DefPathHash exist within the HIR: the parent as the top node's LocalDefId, it's DefPathData as the node's ident and kind, and the disambiguator from the order in nodes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😆 this is your code, we just accidentally took it over and I didn't realize at all what was going on.

Since we require

x != y implies hash_stable(x) != hash_stable(y)

for stable hashing, but any info in local_id_to_def_id is dependent on the body, then just hashing the body satisfies this condition.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not intend to congratulate myself. Sorry for the impoliteness.
I just thought it was not obvious that any info in local_id_to_def_id is dependent on the body, because the LocalDefIds do not appear inside HIR. I'll probably end up adding the comment in another PR, no need to block this one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops I thought we added the comment. Yea, let's do it in a follow up

cc @spastorino

} = *self;
hash_including_bodies.hash_stable(hcx, hasher);
}
}
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,11 @@ impl<'hir> Map<'hir> {
if hir_id.local_id == ItemLocalId::new(0) {
Some(hir_id.owner)
} else {
// FIXME(#85914) is this access safe for incr. comp.?
self.tcx.untracked_resolutions.definitions.opt_hir_id_to_local_def_id(hir_id)
self.tcx
.hir_owner_nodes(hir_id.owner)?
.local_id_to_def_id
.get(&hir_id.local_id)
.copied()
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/test/incremental/hashes/extern_mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ extern "C" {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg = "cfail2", except = "hir_owner")]
#[rustc_clean(cfg = "cfail2", except = "hir_owner,hir_owner_nodes")]
#[rustc_clean(cfg = "cfail3")]
#[rustc_clean(cfg = "cfail5", except = "hir_owner")]
#[rustc_clean(cfg = "cfail5", except = "hir_owner,hir_owner_nodes")]
#[rustc_clean(cfg = "cfail6")]
extern "C" {
pub fn change_function_name2(c: i64) -> i32;
Expand Down Expand Up @@ -132,9 +132,9 @@ extern "C" {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg = "cfail2", except = "hir_owner")]
#[rustc_clean(cfg = "cfail2", except = "hir_owner,hir_owner_nodes")]
#[rustc_clean(cfg = "cfail3")]
#[rustc_clean(cfg = "cfail5", except = "hir_owner")]
#[rustc_clean(cfg = "cfail5", except = "hir_owner,hir_owner_nodes")]
#[rustc_clean(cfg = "cfail6")]
extern "rust-call" {
pub fn change_calling_convention(c: i32);
Expand Down Expand Up @@ -162,9 +162,9 @@ extern "C" {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg = "cfail2", except = "hir_owner")]
#[rustc_clean(cfg = "cfail2", except = "hir_owner,hir_owner_nodes")]
#[rustc_clean(cfg = "cfail3")]
#[rustc_clean(cfg = "cfail5", except = "hir_owner")]
#[rustc_clean(cfg = "cfail5", except = "hir_owner,hir_owner_nodes")]
#[rustc_clean(cfg = "cfail6")]
extern "C" {
pub fn add_function1(c: i32);
Expand Down
Loading