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