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