]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir/src/stable_hash_impls.rs
Rollup merge of #82510 - jyn514:fix-typo, r=Dylan-DPC
[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     BodyId, Expr, ForeignItem, ForeignItemId, ImplItem, ImplItemId, Item, ItemId, MacroDef, Mod,
5     TraitItem, TraitItemId, Ty, VisibilityKind,
6 };
7 use crate::hir_id::{HirId, ItemLocalId};
8 use rustc_span::def_id::{DefPathHash, LocalDefId};
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 librustc_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_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);
23     fn hash_hir_item_like<F: FnOnce(&mut Self)>(&mut self, f: F);
24     fn local_def_path_hash(&self, def_id: LocalDefId) -> DefPathHash;
25 }
26
27 impl<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) {
32         let def_path_hash = hcx.local_def_path_hash(self.owner);
33         (def_path_hash, self.local_id)
34     }
35 }
36
37 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId {
38     type KeyType = DefPathHash;
39
40     #[inline]
41     fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
42         hcx.local_def_path_hash(self.def_id)
43     }
44 }
45
46 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
47     type KeyType = DefPathHash;
48
49     #[inline]
50     fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
51         hcx.local_def_path_hash(self.def_id)
52     }
53 }
54
55 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
56     type KeyType = DefPathHash;
57
58     #[inline]
59     fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
60         hcx.local_def_path_hash(self.def_id)
61     }
62 }
63
64 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId {
65     type KeyType = DefPathHash;
66
67     #[inline]
68     fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
69         hcx.local_def_path_hash(self.def_id)
70     }
71 }
72
73 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HirId {
74     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
75         hcx.hash_hir_id(*self, hasher)
76     }
77 }
78
79 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId {
80     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
81         hcx.hash_body_id(*self, hasher)
82     }
83 }
84
85 // The following implementations of HashStable for `ItemId`, `TraitItemId`, and
86 // `ImplItemId` deserve special attention. Normally we do not hash `NodeId`s within
87 // the HIR, since they just signify a HIR nodes own path. But `ItemId` et al
88 // are used when another item in the HIR is *referenced* and we certainly
89 // want to pick up on a reference changing its target, so we hash the NodeIds
90 // in "DefPath Mode".
91
92 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ItemId {
93     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
94         hcx.hash_reference_to_item(self.hir_id(), hasher)
95     }
96 }
97
98 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ForeignItemId {
99     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
100         hcx.hash_reference_to_item(self.hir_id(), hasher)
101     }
102 }
103
104 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItemId {
105     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
106         hcx.hash_reference_to_item(self.hir_id(), hasher)
107     }
108 }
109
110 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItemId {
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 Mod<'_> {
117     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
118         hcx.hash_hir_mod(self, hasher)
119     }
120 }
121
122 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Expr<'_> {
123     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
124         hcx.hash_hir_expr(self, hasher)
125     }
126 }
127
128 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Ty<'_> {
129     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
130         hcx.hash_hir_ty(self, hasher)
131     }
132 }
133
134 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for VisibilityKind<'_> {
135     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
136         hcx.hash_hir_visibility_kind(self, hasher)
137     }
138 }
139
140 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItem<'_> {
141     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
142         let TraitItem { def_id: _, ident, ref attrs, ref generics, ref kind, span } = *self;
143
144         hcx.hash_hir_item_like(|hcx| {
145             ident.name.hash_stable(hcx, hasher);
146             attrs.hash_stable(hcx, hasher);
147             generics.hash_stable(hcx, hasher);
148             kind.hash_stable(hcx, hasher);
149             span.hash_stable(hcx, hasher);
150         });
151     }
152 }
153
154 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItem<'_> {
155     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
156         let ImplItem {
157             def_id: _,
158             ident,
159             ref vis,
160             defaultness,
161             ref attrs,
162             ref generics,
163             ref kind,
164             span,
165         } = *self;
166
167         hcx.hash_hir_item_like(|hcx| {
168             ident.name.hash_stable(hcx, hasher);
169             vis.hash_stable(hcx, hasher);
170             defaultness.hash_stable(hcx, hasher);
171             attrs.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 attrs, ref kind, span, ref vis } = *self;
182
183         hcx.hash_hir_item_like(|hcx| {
184             ident.name.hash_stable(hcx, hasher);
185             attrs.hash_stable(hcx, hasher);
186             kind.hash_stable(hcx, hasher);
187             span.hash_stable(hcx, hasher);
188             vis.hash_stable(hcx, hasher);
189         });
190     }
191 }
192
193 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
194     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
195         let Item { ident, ref attrs, def_id: _, ref kind, ref vis, span } = *self;
196
197         hcx.hash_hir_item_like(|hcx| {
198             ident.name.hash_stable(hcx, hasher);
199             attrs.hash_stable(hcx, hasher);
200             kind.hash_stable(hcx, hasher);
201             vis.hash_stable(hcx, hasher);
202             span.hash_stable(hcx, hasher);
203         });
204     }
205 }
206
207 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for MacroDef<'_> {
208     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
209         let MacroDef { ident, ref attrs, def_id: _, ref ast, ref vis, span } = *self;
210
211         hcx.hash_hir_item_like(|hcx| {
212             ident.name.hash_stable(hcx, hasher);
213             attrs.hash_stable(hcx, hasher);
214             ast.hash_stable(hcx, hasher);
215             vis.hash_stable(hcx, hasher);
216             span.hash_stable(hcx, hasher);
217         });
218     }
219 }