Skip to main content

rustc_hir/
stable_hash_impls.rs

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