1 use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
4 AttributeMap, BodyId, Crate, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId,
6 use crate::hir_id::{HirId, ItemLocalId};
7 use rustc_span::def_id::DefPathHash;
9 /// Requirements for a `StableHashingContext` to be used in this crate.
10 /// This is a hack to allow using the `HashStable_Generic` derive macro
11 /// instead of implementing everything in `rustc_middle`.
12 pub trait HashStableContext:
13 rustc_ast::HashStableContext + rustc_target::HashStableContext
15 fn hash_body_id(&mut self, _: BodyId, hasher: &mut StableHasher);
18 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
19 type KeyType = (DefPathHash, ItemLocalId);
22 fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
23 let def_path_hash = self.owner.to_stable_hash_key(hcx);
24 (def_path_hash, self.local_id)
28 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemLocalId {
29 type KeyType = ItemLocalId;
32 fn to_stable_hash_key(&self, _: &HirCtx) -> ItemLocalId {
37 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for BodyId {
38 type KeyType = (DefPathHash, ItemLocalId);
41 fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
42 let BodyId { hir_id } = *self;
43 hir_id.to_stable_hash_key(hcx)
47 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId {
48 type KeyType = DefPathHash;
51 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
52 self.def_id.to_stable_hash_key(hcx)
56 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
57 type KeyType = DefPathHash;
60 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
61 self.def_id.to_stable_hash_key(hcx)
65 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
66 type KeyType = DefPathHash;
69 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
70 self.def_id.to_stable_hash_key(hcx)
74 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId {
75 type KeyType = DefPathHash;
78 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
79 self.def_id.to_stable_hash_key(hcx)
83 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId {
84 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
85 hcx.hash_body_id(*self, hasher)
89 // The following implementations of HashStable for `ItemId`, `TraitItemId`, and
90 // `ImplItemId` deserve special attention. Normally we do not hash `NodeId`s within
91 // the HIR, since they just signify a HIR nodes own path. But `ItemId` et al
92 // are used when another item in the HIR is *referenced* and we certainly
93 // want to pick up on a reference changing its target, so we hash the NodeIds
96 impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'tcx> {
97 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
98 // We ignore the `nodes` and `bodies` fields since these refer to information included in
99 // `hash` which is hashed in the collector and used for the crate hash.
100 // `local_id_to_def_id` is also ignored because is dependent on the body, then just hashing
101 // the body satisfies the condition of two nodes being different have different
102 // `hash_stable` results.
104 hash_including_bodies,
105 hash_without_bodies: _,
108 local_id_to_def_id: _,
110 hash_including_bodies.hash_stable(hcx, hasher);
114 impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap<'tcx> {
115 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
116 // We ignore the `map` since it refers to information included in `hash` which is hashed in
117 // the collector and used for the crate hash.
118 let AttributeMap { hash, map: _ } = *self;
119 hash.hash_stable(hcx, hasher);
123 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate<'_> {
124 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
125 let Crate { owners: _, hir_hash } = self;
126 hir_hash.hash_stable(hcx, hasher)