rustc_hir/
stable_hash_impls.rs

1use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
2use rustc_span::def_id::DefPathHash;
3
4use crate::hir::{
5    Attribute, AttributeMap, BodyId, Crate, ForeignItemId, ImplItemId, ItemId, OwnerNodes,
6    TraitItemId,
7};
8use crate::hir_id::{HirId, ItemLocalId};
9
10/// Requirements for a `StableHashingContext` to be used in this crate.
11/// This is a hack to allow using the `HashStable_Generic` derive macro
12/// instead of implementing everything in `rustc_middle`.
13pub trait HashStableContext: rustc_ast::HashStableContext + rustc_abi::HashStableContext {
14    fn hash_attr(&mut self, _: &Attribute, hasher: &mut StableHasher);
15}
16
17impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
18    type KeyType = (DefPathHash, ItemLocalId);
19
20    #[inline]
21    fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
22        let def_path_hash = self.owner.def_id.to_stable_hash_key(hcx);
23        (def_path_hash, self.local_id)
24    }
25}
26
27impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemLocalId {
28    type KeyType = ItemLocalId;
29
30    #[inline]
31    fn to_stable_hash_key(&self, _: &HirCtx) -> ItemLocalId {
32        *self
33    }
34}
35
36impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for BodyId {
37    type KeyType = (DefPathHash, ItemLocalId);
38
39    #[inline]
40    fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
41        let BodyId { hir_id } = *self;
42        hir_id.to_stable_hash_key(hcx)
43    }
44}
45
46impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId {
47    type KeyType = DefPathHash;
48
49    #[inline]
50    fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
51        self.owner_id.def_id.to_stable_hash_key(hcx)
52    }
53}
54
55impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
56    type KeyType = DefPathHash;
57
58    #[inline]
59    fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
60        self.owner_id.def_id.to_stable_hash_key(hcx)
61    }
62}
63
64impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
65    type KeyType = DefPathHash;
66
67    #[inline]
68    fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
69        self.owner_id.def_id.to_stable_hash_key(hcx)
70    }
71}
72
73impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId {
74    type KeyType = DefPathHash;
75
76    #[inline]
77    fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
78        self.owner_id.def_id.to_stable_hash_key(hcx)
79    }
80}
81
82// The following implementations of HashStable for `ItemId`, `TraitItemId`, and
83// `ImplItemId` deserve special attention. Normally we do not hash `NodeId`s within
84// the HIR, since they just signify a HIR nodes own path. But `ItemId` et al
85// are used when another item in the HIR is *referenced* and we certainly
86// want to pick up on a reference changing its target, so we hash the NodeIds
87// in "DefPath Mode".
88
89impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'tcx> {
90    fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
91        // We ignore the `nodes` and `bodies` fields since these refer to information included in
92        // `hash` which is hashed in the collector and used for the crate hash.
93        // `local_id_to_def_id` is also ignored because is dependent on the body, then just hashing
94        // the body satisfies the condition of two nodes being different have different
95        // `hash_stable` results.
96        let OwnerNodes { opt_hash_including_bodies, nodes: _, bodies: _ } = *self;
97        opt_hash_including_bodies.unwrap().hash_stable(hcx, hasher);
98    }
99}
100
101impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap<'tcx> {
102    fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
103        // We ignore the `map` since it refers to information included in `opt_hash` which is
104        // hashed in the collector and used for the crate hash.
105        let AttributeMap { opt_hash, map: _ } = *self;
106        opt_hash.unwrap().hash_stable(hcx, hasher);
107    }
108}
109
110impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate<'_> {
111    fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
112        let Crate { owners: _, opt_hir_hash } = self;
113        opt_hir_hash.unwrap().hash_stable(hcx, hasher)
114    }
115}
116
117impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Attribute {
118    fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
119        hcx.hash_attr(self, hasher)
120    }
121}