Skip to main content

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::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: rustc_ast::HashStableContext + rustc_abi::HashStableContext {}
15
16impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for BodyId {
17    type KeyType = (DefPathHash, ItemLocalId);
18
19    #[inline]
20    fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
21        let BodyId { hir_id } = *self;
22        hir_id.to_stable_hash_key(hcx)
23    }
24}
25
26impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId {
27    type KeyType = DefPathHash;
28
29    #[inline]
30    fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
31        self.owner_id.def_id.to_stable_hash_key(hcx)
32    }
33}
34
35impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
36    type KeyType = DefPathHash;
37
38    #[inline]
39    fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
40        self.owner_id.def_id.to_stable_hash_key(hcx)
41    }
42}
43
44impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
45    type KeyType = DefPathHash;
46
47    #[inline]
48    fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
49        self.owner_id.def_id.to_stable_hash_key(hcx)
50    }
51}
52
53impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId {
54    type KeyType = DefPathHash;
55
56    #[inline]
57    fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
58        self.owner_id.def_id.to_stable_hash_key(hcx)
59    }
60}
61
62// The following implementations of HashStable for `ItemId`, `TraitItemId`, and
63// `ImplItemId` deserve special attention. Normally we do not hash `NodeId`s within
64// the HIR, since they just signify a HIR nodes own path. But `ItemId` et al
65// are used when another item in the HIR is *referenced* and we certainly
66// want to pick up on a reference changing its target, so we hash the NodeIds
67// in "DefPath Mode".
68
69impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'tcx> {
70    fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
71        // We ignore the `nodes` and `bodies` fields since these refer to information included in
72        // `hash` which is hashed in the collector and used for the crate hash.
73        // `local_id_to_def_id` is also ignored because is dependent on the body, then just hashing
74        // the body satisfies the condition of two nodes being different have different
75        // `hash_stable` results.
76        let OwnerNodes { opt_hash_including_bodies, nodes: _, bodies: _ } = *self;
77        opt_hash_including_bodies.unwrap().hash_stable(hcx, hasher);
78    }
79}
80
81impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for DelayedLints {
82    fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
83        let DelayedLints { opt_hash, .. } = *self;
84        opt_hash.unwrap().hash_stable(hcx, hasher);
85    }
86}
87
88impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap<'tcx> {
89    fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
90        // We ignore the `map` since it refers to information included in `opt_hash` which is
91        // hashed in the collector and used for the crate hash.
92        let AttributeMap { opt_hash, define_opaque: _, map: _ } = *self;
93        opt_hash.unwrap().hash_stable(hcx, hasher);
94    }
95}
96
97impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate<'_> {
98    fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
99        let Crate { owners: _, opt_hir_hash } = self;
100        opt_hir_hash.unwrap().hash_stable(hcx, hasher)
101    }
102}
103
104impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HashIgnoredAttrId {
105    fn hash_stable(&self, _hcx: &mut HirCtx, _hasher: &mut StableHasher) {
106        /* we don't hash HashIgnoredAttrId, we ignore them */
107    }
108}