Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 0431fdb

Browse files
committed
Compute full HIR hash during lowering.
1 parent 41e80b8 commit 0431fdb

File tree

6 files changed

+69
-57
lines changed

6 files changed

+69
-57
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
399399
hir::OwnerNode::Crate(lctx.arena.alloc(module))
400400
});
401401

402+
let hir_hash = self.compute_hir_hash();
403+
402404
let mut def_id_to_hir_id = IndexVec::default();
403405

404406
for (node_id, hir_id) in self.node_id_to_hir_id.into_iter_enumerated() {
@@ -412,10 +414,29 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
412414

413415
self.resolver.definitions().init_def_id_to_hir_id_mapping(def_id_to_hir_id);
414416

415-
let krate = hir::Crate { owners: self.owners };
417+
let krate = hir::Crate { owners: self.owners, hir_hash };
416418
self.arena.alloc(krate)
417419
}
418420

421+
fn compute_hir_hash(&mut self) -> Fingerprint {
422+
let definitions = self.resolver.definitions();
423+
let mut hir_body_nodes: Vec<_> = self
424+
.owners
425+
.iter_enumerated()
426+
.filter_map(|(def_id, info)| {
427+
let info = info.as_ref()?;
428+
let def_path_hash = definitions.def_path_hash(def_id);
429+
Some((def_path_hash, info))
430+
})
431+
.collect();
432+
hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
433+
434+
let mut stable_hasher = StableHasher::new();
435+
let mut hcx = self.resolver.create_stable_hashing_context();
436+
hir_body_nodes.hash_stable(&mut hcx, &mut stable_hasher);
437+
stable_hasher.finish()
438+
}
439+
419440
fn with_hir_id_owner(
420441
&mut self,
421442
owner: NodeId,

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ pub struct OwnerNodes<'tcx> {
702702
pub bodies: IndexVec<ItemLocalId, Option<&'tcx Body<'tcx>>>,
703703
}
704704

705-
#[derive(Debug)]
705+
#[derive(Debug, HashStable_Generic)]
706706
pub struct OwnerInfo<'hir> {
707707
/// Contents of the HIR.
708708
pub nodes: OwnerNodes<'hir>,
@@ -734,6 +734,7 @@ impl<'tcx> OwnerInfo<'tcx> {
734734
#[derive(Debug)]
735735
pub struct Crate<'hir> {
736736
pub owners: IndexVec<LocalDefId, Option<OwnerInfo<'hir>>>,
737+
pub hir_hash: Fingerprint,
737738
}
738739

739740
/// A block of statements `{ .. }`, which may have a label (in this case the

compiler/rustc_hir/src/stable_hash_impls.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
22

33
use crate::hir::{
4-
AttributeMap, BodyId, Expr, ForeignItem, ForeignItemId, ImplItem, ImplItemId, Item, ItemId,
5-
Mod, OwnerNodes, TraitItem, TraitItemId, Ty, VisibilityKind,
4+
AttributeMap, BodyId, Crate, Expr, ForeignItem, ForeignItemId, ImplItem, ImplItemId, Item,
5+
ItemId, Mod, OwnerNodes, TraitCandidate, TraitItem, TraitItemId, Ty, VisibilityKind,
66
};
77
use crate::hir_id::{HirId, ItemLocalId};
88
use rustc_span::def_id::DefPathHash;
@@ -21,6 +21,7 @@ pub trait HashStableContext:
2121
fn hash_hir_ty(&mut self, _: &Ty<'_>, hasher: &mut StableHasher);
2222
fn hash_hir_visibility_kind(&mut self, _: &VisibilityKind<'_>, hasher: &mut StableHasher);
2323
fn hash_hir_item_like<F: FnOnce(&mut Self)>(&mut self, f: F);
24+
fn hash_hir_trait_candidate(&mut self, _: &TraitCandidate, hasher: &mut StableHasher);
2425
}
2526

2627
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
@@ -227,3 +228,16 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap<'tcx>
227228
hash.hash_stable(hcx, hasher);
228229
}
229230
}
231+
232+
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate<'_> {
233+
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
234+
let Crate { owners: _, hir_hash } = self;
235+
hir_hash.hash_stable(hcx, hasher)
236+
}
237+
}
238+
239+
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitCandidate {
240+
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
241+
hcx.hash_hir_trait_candidate(self, hasher)
242+
}
243+
}

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

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,18 +1066,8 @@ impl<'hir> intravisit::Map<'hir> for Map<'hir> {
10661066

10671067
pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
10681068
debug_assert_eq!(crate_num, LOCAL_CRATE);
1069-
let mut hir_body_nodes: Vec<_> = tcx
1070-
.untracked_resolutions
1071-
.definitions
1072-
.def_path_table()
1073-
.all_def_path_hashes_and_def_ids()
1074-
.filter_map(|(def_path_hash, local_def_index)| {
1075-
let def_id = LocalDefId { local_def_index };
1076-
let hash = tcx.hir_crate(()).owners[def_id].as_ref()?.nodes.hash;
1077-
Some((def_path_hash, hash, def_id))
1078-
})
1079-
.collect();
1080-
hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
1069+
let krate = tcx.hir_crate(());
1070+
let hir_body_hash = krate.hir_hash;
10811071

10821072
let upstream_crates = upstream_crates(tcx);
10831073

@@ -1099,22 +1089,25 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
10991089

11001090
let mut hcx = tcx.create_stable_hashing_context();
11011091
let mut stable_hasher = StableHasher::new();
1102-
for (def_path_hash, fingerprint, def_id) in hir_body_nodes.iter() {
1103-
def_path_hash.0.hash_stable(&mut hcx, &mut stable_hasher);
1104-
fingerprint.hash_stable(&mut hcx, &mut stable_hasher);
1105-
tcx.untracked_crate.owners[*def_id]
1106-
.as_ref()
1107-
.unwrap()
1108-
.attrs
1109-
.hash_stable(&mut hcx, &mut stable_hasher);
1110-
if tcx.sess.opts.debugging_opts.incremental_relative_spans {
1111-
let span = tcx.untracked_resolutions.definitions.def_span(*def_id);
1112-
debug_assert_eq!(span.parent(), None);
1113-
span.hash_stable(&mut hcx, &mut stable_hasher);
1114-
}
1115-
}
1092+
hir_body_hash.hash_stable(&mut hcx, &mut stable_hasher);
11161093
upstream_crates.hash_stable(&mut hcx, &mut stable_hasher);
11171094
source_file_names.hash_stable(&mut hcx, &mut stable_hasher);
1095+
if tcx.sess.opts.debugging_opts.incremental_relative_spans {
1096+
let definitions = &tcx.untracked_resolutions.definitions;
1097+
let mut owner_spans: Vec<_> = krate
1098+
.owners
1099+
.iter_enumerated()
1100+
.filter_map(|(def_id, info)| {
1101+
let _ = info.as_ref()?;
1102+
let def_path_hash = definitions.def_path_hash(def_id);
1103+
let span = definitions.def_span(def_id);
1104+
debug_assert_eq!(span.parent(), None);
1105+
Some((def_path_hash, span))
1106+
})
1107+
.collect();
1108+
owner_spans.sort_unstable_by_key(|bn| bn.0);
1109+
owner_spans.hash_stable(&mut hcx, &mut stable_hasher);
1110+
}
11181111
tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);
11191112
tcx.sess.local_stable_crate_id().hash_stable(&mut hcx, &mut stable_hasher);
11201113

compiler/rustc_middle/src/query/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ rustc_queries! {
3636
/// prefer wrappers like `tcx.visit_all_items_in_krate()`.
3737
query hir_crate(key: ()) -> &'tcx Crate<'tcx> {
3838
eval_always
39-
no_hash
4039
desc { "get the crate HIR" }
4140
}
4241

compiler/rustc_query_system/src/ich/impls_hir.rs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use crate::ich::{NodeIdHashingMode, StableHashingContext};
66
use rustc_data_structures::fingerprint::Fingerprint;
77
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
88
use rustc_hir as hir;
9-
use rustc_hir::definitions::DefPathHash;
10-
use smallvec::SmallVec;
119
use std::mem;
1210

1311
impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> {
@@ -121,6 +119,16 @@ impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> {
121119

122120
self.node_id_hashing_mode = prev_hash_node_ids;
123121
}
122+
123+
#[inline]
124+
fn hash_hir_trait_candidate(&mut self, tc: &hir::TraitCandidate, hasher: &mut StableHasher) {
125+
self.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
126+
let hir::TraitCandidate { def_id, import_ids } = tc;
127+
128+
def_id.hash_stable(hcx, hasher);
129+
import_ids.hash_stable(hcx, hasher);
130+
});
131+
}
124132
}
125133

126134
impl<'a> HashStable<StableHashingContext<'a>> for hir::Body<'_> {
@@ -135,27 +143,3 @@ impl<'a> HashStable<StableHashingContext<'a>> for hir::Body<'_> {
135143
});
136144
}
137145
}
138-
139-
impl<'a> HashStable<StableHashingContext<'a>> for hir::TraitCandidate {
140-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
141-
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
142-
let hir::TraitCandidate { def_id, import_ids } = self;
143-
144-
def_id.hash_stable(hcx, hasher);
145-
import_ids.hash_stable(hcx, hasher);
146-
});
147-
}
148-
}
149-
150-
impl<'a> ToStableHashKey<StableHashingContext<'a>> for hir::TraitCandidate {
151-
type KeyType = (DefPathHash, SmallVec<[DefPathHash; 1]>);
152-
153-
fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> Self::KeyType {
154-
let hir::TraitCandidate { def_id, import_ids } = self;
155-
156-
(
157-
hcx.def_path_hash(*def_id),
158-
import_ids.iter().map(|def_id| hcx.local_def_path_hash(*def_id)).collect(),
159-
)
160-
}
161-
}

0 commit comments

Comments
 (0)