]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir/src/stable_hash_impls.rs
Store macro parent module in ExpnData.
[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;
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_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 }
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 ItemId {
37     type KeyType = DefPathHash;
38
39     #[inline]
40     fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
41         self.def_id.to_stable_hash_key(hcx)
42     }
43 }
44
45 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
46     type KeyType = DefPathHash;
47
48     #[inline]
49     fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
50         self.def_id.to_stable_hash_key(hcx)
51     }
52 }
53
54 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
55     type KeyType = DefPathHash;
56
57     #[inline]
58     fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
59         self.def_id.to_stable_hash_key(hcx)
60     }
61 }
62
63 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId {
64     type KeyType = DefPathHash;
65
66     #[inline]
67     fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
68         self.def_id.to_stable_hash_key(hcx)
69     }
70 }
71
72 impl<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
78 impl<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
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
91 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ItemId {
92     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
93         hcx.hash_reference_to_item(self.hir_id(), hasher)
94     }
95 }
96
97 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ForeignItemId {
98     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
99         hcx.hash_reference_to_item(self.hir_id(), hasher)
100     }
101 }
102
103 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItemId {
104     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
105         hcx.hash_reference_to_item(self.hir_id(), hasher)
106     }
107 }
108
109 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItemId {
110     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
111         hcx.hash_reference_to_item(self.hir_id(), hasher)
112     }
113 }
114
115 impl<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
121 impl<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
127 impl<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
133 impl<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 }
138
139 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItem<'_> {
140     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
141         let TraitItem { def_id: _, ident, ref generics, ref kind, span } = *self;
142
143         hcx.hash_hir_item_like(|hcx| {
144             ident.name.hash_stable(hcx, hasher);
145             generics.hash_stable(hcx, hasher);
146             kind.hash_stable(hcx, hasher);
147             span.hash_stable(hcx, hasher);
148         });
149     }
150 }
151
152 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItem<'_> {
153     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
154         let ImplItem { def_id: _, ident, ref vis, defaultness, ref generics, ref kind, span } =
155             *self;
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);
161             generics.hash_stable(hcx, hasher);
162             kind.hash_stable(hcx, hasher);
163             span.hash_stable(hcx, hasher);
164         });
165     }
166 }
167
168 impl<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
181 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
182     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
183         let Item { ident, def_id: _, ref kind, ref vis, span } = *self;
184
185         hcx.hash_hir_item_like(|hcx| {
186             ident.name.hash_stable(hcx, hasher);
187             kind.hash_stable(hcx, hasher);
188             vis.hash_stable(hcx, hasher);
189             span.hash_stable(hcx, hasher);
190         });
191     }
192 }
193
194 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for MacroDef<'_> {
195     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
196         let MacroDef { ident, def_id: _, ref ast, ref vis, span } = *self;
197
198         hcx.hash_hir_item_like(|hcx| {
199             ident.name.hash_stable(hcx, hasher);
200             ast.hash_stable(hcx, hasher);
201             vis.hash_stable(hcx, hasher);
202             span.hash_stable(hcx, hasher);
203         });
204     }
205 }