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