rustc_hir/
stable_hash_impls.rs1use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
2use rustc_span::def_id::DefPathHash;
3
4use crate::hir::{
5 Attribute, AttributeMap, BodyId, Crate, ForeignItemId, ImplItemId, ItemId, OwnerNodes,
6 TraitItemId,
7};
8use crate::hir_id::{HirId, ItemLocalId};
9
10pub trait HashStableContext: rustc_ast::HashStableContext + rustc_abi::HashStableContext {
14 fn hash_attr(&mut self, _: &Attribute, hasher: &mut StableHasher);
15}
16
17impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
18 type KeyType = (DefPathHash, ItemLocalId);
19
20 #[inline]
21 fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
22 let def_path_hash = self.owner.def_id.to_stable_hash_key(hcx);
23 (def_path_hash, self.local_id)
24 }
25}
26
27impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemLocalId {
28 type KeyType = ItemLocalId;
29
30 #[inline]
31 fn to_stable_hash_key(&self, _: &HirCtx) -> ItemLocalId {
32 *self
33 }
34}
35
36impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for BodyId {
37 type KeyType = (DefPathHash, ItemLocalId);
38
39 #[inline]
40 fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
41 let BodyId { hir_id } = *self;
42 hir_id.to_stable_hash_key(hcx)
43 }
44}
45
46impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId {
47 type KeyType = DefPathHash;
48
49 #[inline]
50 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
51 self.owner_id.def_id.to_stable_hash_key(hcx)
52 }
53}
54
55impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
56 type KeyType = DefPathHash;
57
58 #[inline]
59 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
60 self.owner_id.def_id.to_stable_hash_key(hcx)
61 }
62}
63
64impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
65 type KeyType = DefPathHash;
66
67 #[inline]
68 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
69 self.owner_id.def_id.to_stable_hash_key(hcx)
70 }
71}
72
73impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId {
74 type KeyType = DefPathHash;
75
76 #[inline]
77 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
78 self.owner_id.def_id.to_stable_hash_key(hcx)
79 }
80}
81
82impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'tcx> {
90 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
91 let OwnerNodes { opt_hash_including_bodies, nodes: _, bodies: _ } = *self;
97 opt_hash_including_bodies.unwrap().hash_stable(hcx, hasher);
98 }
99}
100
101impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap<'tcx> {
102 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
103 let AttributeMap { opt_hash, map: _ } = *self;
106 opt_hash.unwrap().hash_stable(hcx, hasher);
107 }
108}
109
110impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate<'_> {
111 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
112 let Crate { owners: _, opt_hir_hash } = self;
113 opt_hir_hash.unwrap().hash_stable(hcx, hasher)
114 }
115}
116
117impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Attribute {
118 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
119 hcx.hash_attr(self, hasher)
120 }
121}