]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_hir/src/stable_hash_impls.rs
New upstream version 1.58.1+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::{
3c0e092e
XL
4 AttributeMap, BodyId, Crate, Expr, ForeignItem, ForeignItemId, ImplItem, ImplItemId, Item,
5 ItemId, Mod, OwnerNodes, TraitCandidate, TraitItem, 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);
3c0e092e 24 fn hash_hir_trait_candidate(&mut self, _: &TraitCandidate, hasher: &mut StableHasher);
ba9703b0
XL
25}
26
27impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
28 type KeyType = (DefPathHash, ItemLocalId);
29
30 #[inline]
31 fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
136023e0 32 let def_path_hash = self.owner.to_stable_hash_key(hcx);
ba9703b0
XL
33 (def_path_hash, self.local_id)
34 }
35}
36
c295e0f8
XL
37impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemLocalId {
38 type KeyType = ItemLocalId;
39
40 #[inline]
41 fn to_stable_hash_key(&self, _: &HirCtx) -> ItemLocalId {
42 *self
43 }
44}
45
46impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for BodyId {
47 type KeyType = (DefPathHash, ItemLocalId);
48
49 #[inline]
50 fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
51 let BodyId { hir_id } = *self;
52 hir_id.to_stable_hash_key(hcx)
53 }
54}
55
6a06907d
XL
56impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId {
57 type KeyType = DefPathHash;
58
59 #[inline]
60 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
136023e0 61 self.def_id.to_stable_hash_key(hcx)
6a06907d
XL
62 }
63}
64
ba9703b0 65impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
6a06907d 66 type KeyType = DefPathHash;
ba9703b0
XL
67
68 #[inline]
6a06907d 69 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
136023e0 70 self.def_id.to_stable_hash_key(hcx)
ba9703b0
XL
71 }
72}
73
74impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
6a06907d 75 type KeyType = DefPathHash;
ba9703b0
XL
76
77 #[inline]
6a06907d 78 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
136023e0 79 self.def_id.to_stable_hash_key(hcx)
ba9703b0 80 }
dfeec247
XL
81}
82
fc512014 83impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId {
6a06907d 84 type KeyType = DefPathHash;
fc512014
XL
85
86 #[inline]
6a06907d 87 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
136023e0 88 self.def_id.to_stable_hash_key(hcx)
fc512014
XL
89 }
90}
91
dfeec247
XL
92impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HirId {
93 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
94 hcx.hash_hir_id(*self, hasher)
95 }
96}
97
dfeec247
XL
98impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId {
99 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
100 hcx.hash_body_id(*self, hasher)
101 }
102}
103
74b04a01
XL
104// The following implementations of HashStable for `ItemId`, `TraitItemId`, and
105// `ImplItemId` deserve special attention. Normally we do not hash `NodeId`s within
106// the HIR, since they just signify a HIR nodes own path. But `ItemId` et al
107// are used when another item in the HIR is *referenced* and we certainly
108// want to pick up on a reference changing its target, so we hash the NodeIds
109// in "DefPath Mode".
110
dfeec247
XL
111impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ItemId {
112 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d 113 hcx.hash_reference_to_item(self.hir_id(), hasher)
dfeec247
XL
114 }
115}
116
fc512014
XL
117impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ForeignItemId {
118 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d 119 hcx.hash_reference_to_item(self.hir_id(), hasher)
fc512014
XL
120 }
121}
122
dfeec247
XL
123impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItemId {
124 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d 125 hcx.hash_reference_to_item(self.hir_id(), hasher)
dfeec247
XL
126 }
127}
128
129impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItemId {
130 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d 131 hcx.hash_reference_to_item(self.hir_id(), hasher)
dfeec247
XL
132 }
133}
134
135impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Mod<'_> {
136 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
137 hcx.hash_hir_mod(self, hasher)
138 }
139}
140
141impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Expr<'_> {
142 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
143 hcx.hash_hir_expr(self, hasher)
144 }
145}
146
147impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Ty<'_> {
148 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
149 hcx.hash_hir_ty(self, hasher)
150 }
151}
152
153impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for VisibilityKind<'_> {
154 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
155 hcx.hash_hir_visibility_kind(self, hasher)
156 }
157}
ba9703b0
XL
158
159impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItem<'_> {
160 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d 161 let TraitItem { def_id: _, ident, ref generics, ref kind, span } = *self;
ba9703b0
XL
162
163 hcx.hash_hir_item_like(|hcx| {
164 ident.name.hash_stable(hcx, hasher);
ba9703b0
XL
165 generics.hash_stable(hcx, hasher);
166 kind.hash_stable(hcx, hasher);
167 span.hash_stable(hcx, hasher);
168 });
169 }
170}
171
172impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItem<'_> {
173 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d
XL
174 let ImplItem { def_id: _, ident, ref vis, defaultness, ref generics, ref kind, span } =
175 *self;
ba9703b0
XL
176
177 hcx.hash_hir_item_like(|hcx| {
178 ident.name.hash_stable(hcx, hasher);
179 vis.hash_stable(hcx, hasher);
180 defaultness.hash_stable(hcx, hasher);
ba9703b0
XL
181 generics.hash_stable(hcx, hasher);
182 kind.hash_stable(hcx, hasher);
183 span.hash_stable(hcx, hasher);
184 });
185 }
186}
187
6a06907d
XL
188impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ForeignItem<'_> {
189 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
190 let ForeignItem { def_id: _, ident, ref kind, span, ref vis } = *self;
191
192 hcx.hash_hir_item_like(|hcx| {
193 ident.name.hash_stable(hcx, hasher);
194 kind.hash_stable(hcx, hasher);
195 span.hash_stable(hcx, hasher);
196 vis.hash_stable(hcx, hasher);
197 });
198 }
199}
200
ba9703b0
XL
201impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
202 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d 203 let Item { ident, def_id: _, ref kind, ref vis, span } = *self;
ba9703b0
XL
204
205 hcx.hash_hir_item_like(|hcx| {
206 ident.name.hash_stable(hcx, hasher);
ba9703b0
XL
207 kind.hash_stable(hcx, hasher);
208 vis.hash_stable(hcx, hasher);
209 span.hash_stable(hcx, hasher);
210 });
211 }
212}
3c0e092e
XL
213
214impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'tcx> {
215 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
216 // We ignore the `nodes` and `bodies` fields since these refer to information included in
217 // `hash` which is hashed in the collector and used for the crate hash.
218 let OwnerNodes { hash_including_bodies, hash_without_bodies: _, nodes: _, bodies: _ } =
219 *self;
220 hash_including_bodies.hash_stable(hcx, hasher);
221 }
222}
223
224impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap<'tcx> {
225 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
226 // We ignore the `map` since it refers to information included in `hash` which is hashed in
227 // the collector and used for the crate hash.
228 let AttributeMap { hash, map: _ } = *self;
229 hash.hash_stable(hcx, hasher);
230 }
231}
232
233impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate<'_> {
234 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
235 let Crate { owners: _, hir_hash } = self;
236 hir_hash.hash_stable(hcx, hasher)
237 }
238}
239
240impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitCandidate {
241 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
242 hcx.hash_hir_trait_candidate(self, hasher)
243 }
244}