]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_hir/src/stable_hash_impls.rs
New upstream version 1.56.0+dfsg1
[rustc.git] / compiler / rustc_hir / src / stable_hash_impls.rs
CommitLineData
ba9703b0 1use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
dfeec247 2
ba9703b0 3use crate::hir::{
94222f64
XL
4 BodyId, Expr, ForeignItem, ForeignItemId, ImplItem, ImplItemId, Item, ItemId, Mod, TraitItem,
5 TraitItemId, Ty, VisibilityKind,
ba9703b0
XL
6};
7use crate::hir_id::{HirId, ItemLocalId};
136023e0 8use rustc_span::def_id::DefPathHash;
dfeec247
XL
9
10/// Requirements for a `StableHashingContext` to be used in this crate.
11/// This is a hack to allow using the `HashStable_Generic` derive macro
cdc7bbd5 12/// instead of implementing everything in `rustc_middle`.
74b04a01
XL
13pub trait HashStableContext:
14 rustc_ast::HashStableContext + rustc_target::HashStableContext
15{
dfeec247
XL
16 fn hash_hir_id(&mut self, _: HirId, hasher: &mut StableHasher);
17 fn hash_body_id(&mut self, _: BodyId, hasher: &mut StableHasher);
74b04a01 18 fn hash_reference_to_item(&mut self, _: HirId, hasher: &mut StableHasher);
dfeec247
XL
19 fn hash_hir_mod(&mut self, _: &Mod<'_>, hasher: &mut StableHasher);
20 fn hash_hir_expr(&mut self, _: &Expr<'_>, hasher: &mut StableHasher);
21 fn hash_hir_ty(&mut self, _: &Ty<'_>, hasher: &mut StableHasher);
22 fn hash_hir_visibility_kind(&mut self, _: &VisibilityKind<'_>, hasher: &mut StableHasher);
ba9703b0 23 fn hash_hir_item_like<F: FnOnce(&mut Self)>(&mut self, f: F);
ba9703b0
XL
24}
25
26impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
27 type KeyType = (DefPathHash, ItemLocalId);
28
29 #[inline]
30 fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
136023e0 31 let def_path_hash = self.owner.to_stable_hash_key(hcx);
ba9703b0
XL
32 (def_path_hash, self.local_id)
33 }
34}
35
6a06907d
XL
36impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId {
37 type KeyType = DefPathHash;
38
39 #[inline]
40 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
136023e0 41 self.def_id.to_stable_hash_key(hcx)
6a06907d
XL
42 }
43}
44
ba9703b0 45impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
6a06907d 46 type KeyType = DefPathHash;
ba9703b0
XL
47
48 #[inline]
6a06907d 49 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
136023e0 50 self.def_id.to_stable_hash_key(hcx)
ba9703b0
XL
51 }
52}
53
54impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
6a06907d 55 type KeyType = DefPathHash;
ba9703b0
XL
56
57 #[inline]
6a06907d 58 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
136023e0 59 self.def_id.to_stable_hash_key(hcx)
ba9703b0 60 }
dfeec247
XL
61}
62
fc512014 63impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId {
6a06907d 64 type KeyType = DefPathHash;
fc512014
XL
65
66 #[inline]
6a06907d 67 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
136023e0 68 self.def_id.to_stable_hash_key(hcx)
fc512014
XL
69 }
70}
71
dfeec247
XL
72impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HirId {
73 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
74 hcx.hash_hir_id(*self, hasher)
75 }
76}
77
dfeec247
XL
78impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId {
79 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
80 hcx.hash_body_id(*self, hasher)
81 }
82}
83
74b04a01
XL
84// The following implementations of HashStable for `ItemId`, `TraitItemId`, and
85// `ImplItemId` deserve special attention. Normally we do not hash `NodeId`s within
86// the HIR, since they just signify a HIR nodes own path. But `ItemId` et al
87// are used when another item in the HIR is *referenced* and we certainly
88// want to pick up on a reference changing its target, so we hash the NodeIds
89// in "DefPath Mode".
90
dfeec247
XL
91impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ItemId {
92 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d 93 hcx.hash_reference_to_item(self.hir_id(), hasher)
dfeec247
XL
94 }
95}
96
fc512014
XL
97impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ForeignItemId {
98 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d 99 hcx.hash_reference_to_item(self.hir_id(), hasher)
fc512014
XL
100 }
101}
102
dfeec247
XL
103impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItemId {
104 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d 105 hcx.hash_reference_to_item(self.hir_id(), hasher)
dfeec247
XL
106 }
107}
108
109impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItemId {
110 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d 111 hcx.hash_reference_to_item(self.hir_id(), hasher)
dfeec247
XL
112 }
113}
114
115impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Mod<'_> {
116 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
117 hcx.hash_hir_mod(self, hasher)
118 }
119}
120
121impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Expr<'_> {
122 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
123 hcx.hash_hir_expr(self, hasher)
124 }
125}
126
127impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Ty<'_> {
128 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
129 hcx.hash_hir_ty(self, hasher)
130 }
131}
132
133impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for VisibilityKind<'_> {
134 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
135 hcx.hash_hir_visibility_kind(self, hasher)
136 }
137}
ba9703b0
XL
138
139impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItem<'_> {
140 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d 141 let TraitItem { def_id: _, ident, ref generics, ref kind, span } = *self;
ba9703b0
XL
142
143 hcx.hash_hir_item_like(|hcx| {
144 ident.name.hash_stable(hcx, hasher);
ba9703b0
XL
145 generics.hash_stable(hcx, hasher);
146 kind.hash_stable(hcx, hasher);
147 span.hash_stable(hcx, hasher);
148 });
149 }
150}
151
152impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItem<'_> {
153 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d
XL
154 let ImplItem { def_id: _, ident, ref vis, defaultness, ref generics, ref kind, span } =
155 *self;
ba9703b0
XL
156
157 hcx.hash_hir_item_like(|hcx| {
158 ident.name.hash_stable(hcx, hasher);
159 vis.hash_stable(hcx, hasher);
160 defaultness.hash_stable(hcx, hasher);
ba9703b0
XL
161 generics.hash_stable(hcx, hasher);
162 kind.hash_stable(hcx, hasher);
163 span.hash_stable(hcx, hasher);
164 });
165 }
166}
167
6a06907d
XL
168impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ForeignItem<'_> {
169 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
170 let ForeignItem { def_id: _, ident, ref kind, span, ref vis } = *self;
171
172 hcx.hash_hir_item_like(|hcx| {
173 ident.name.hash_stable(hcx, hasher);
174 kind.hash_stable(hcx, hasher);
175 span.hash_stable(hcx, hasher);
176 vis.hash_stable(hcx, hasher);
177 });
178 }
179}
180
ba9703b0
XL
181impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
182 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d 183 let Item { ident, def_id: _, ref kind, ref vis, span } = *self;
ba9703b0
XL
184
185 hcx.hash_hir_item_like(|hcx| {
186 ident.name.hash_stable(hcx, hasher);
ba9703b0
XL
187 kind.hash_stable(hcx, hasher);
188 vis.hash_stable(hcx, hasher);
189 span.hash_stable(hcx, hasher);
190 });
191 }
192}