rustc_hir/
stable_hash_impls.rs

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