Skip to main content

rustc_hir/
stable_hash_impls.rs

1use rustc_data_structures::stable_hasher::{
2    StableHash, StableHashCtxt, 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: StableHashCtxt>(&self, hcx: &mut Hcx) -> (DefPathHash, ItemLocalId) {
17        let BodyId { hir_id } = *self;
18        hir_id.to_stable_hash_key(hcx)
19    }
20}
21
22impl ToStableHashKey for ItemId {
23    type KeyType = DefPathHash;
24
25    #[inline]
26    fn to_stable_hash_key<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx) -> DefPathHash {
27        self.owner_id.def_id.to_stable_hash_key(hcx)
28    }
29}
30
31impl ToStableHashKey for TraitItemId {
32    type KeyType = DefPathHash;
33
34    #[inline]
35    fn to_stable_hash_key<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx) -> DefPathHash {
36        self.owner_id.def_id.to_stable_hash_key(hcx)
37    }
38}
39
40impl ToStableHashKey for ImplItemId {
41    type KeyType = DefPathHash;
42
43    #[inline]
44    fn to_stable_hash_key<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx) -> DefPathHash {
45        self.owner_id.def_id.to_stable_hash_key(hcx)
46    }
47}
48
49impl ToStableHashKey for ForeignItemId {
50    type KeyType = DefPathHash;
51
52    #[inline]
53    fn to_stable_hash_key<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx) -> DefPathHash {
54        self.owner_id.def_id.to_stable_hash_key(hcx)
55    }
56}
57
58// The following implementations of StableHash 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> StableHash for OwnerNodes<'tcx> {
66    fn stable_hash<Hcx: StableHashCtxt>(&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        // `stable_hash` results.
72        let OwnerNodes { opt_hash_including_bodies, nodes: _, bodies: _ } = *self;
73        opt_hash_including_bodies.unwrap().stable_hash(hcx, hasher);
74    }
75}
76
77impl<'tcx> StableHash for AttributeMap<'tcx> {
78    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
79        // We ignore the `map` since it refers to information included in `opt_hash` which is
80        // hashed in the collector and used for the crate hash.
81        let AttributeMap { opt_hash, define_opaque: _, map: _ } = *self;
82        opt_hash.unwrap().stable_hash(hcx, hasher);
83    }
84}
85
86impl StableHash for HashIgnoredAttrId {
87    fn stable_hash<Hcx: StableHashCtxt>(&self, _hcx: &mut Hcx, _hasher: &mut StableHasher) {
88        /* we don't hash HashIgnoredAttrId, we ignore them */
89    }
90}