Skip to main content

rustc_hir/
stable_hash_impls.rs

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