]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_hir/src/stable_hash_impls.rs
New upstream version 1.52.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::{
6a06907d
XL
4 BodyId, Expr, ForeignItem, ForeignItemId, ImplItem, ImplItemId, Item, ItemId, MacroDef, Mod,
5 TraitItem, TraitItemId, Ty, VisibilityKind,
ba9703b0
XL
6};
7use crate::hir_id::{HirId, ItemLocalId};
8use rustc_span::def_id::{DefPathHash, LocalDefId};
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
ba9703b0 12/// instead of implementing everything in librustc_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
XL
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
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) {
32 let def_path_hash = hcx.local_def_path_hash(self.owner);
33 (def_path_hash, self.local_id)
34 }
35}
36
6a06907d
XL
37impl<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
ba9703b0 46impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
6a06907d 47 type KeyType = DefPathHash;
ba9703b0
XL
48
49 #[inline]
6a06907d
XL
50 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
51 hcx.local_def_path_hash(self.def_id)
ba9703b0
XL
52 }
53}
54
55impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
6a06907d 56 type KeyType = DefPathHash;
ba9703b0
XL
57
58 #[inline]
6a06907d
XL
59 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
60 hcx.local_def_path_hash(self.def_id)
ba9703b0 61 }
dfeec247
XL
62}
63
fc512014 64impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId {
6a06907d 65 type KeyType = DefPathHash;
fc512014
XL
66
67 #[inline]
6a06907d
XL
68 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
69 hcx.local_def_path_hash(self.def_id)
fc512014
XL
70 }
71}
72
dfeec247
XL
73impl<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
dfeec247
XL
79impl<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
74b04a01
XL
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
dfeec247
XL
92impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ItemId {
93 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d 94 hcx.hash_reference_to_item(self.hir_id(), hasher)
dfeec247
XL
95 }
96}
97
fc512014
XL
98impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ForeignItemId {
99 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d 100 hcx.hash_reference_to_item(self.hir_id(), hasher)
fc512014
XL
101 }
102}
103
dfeec247
XL
104impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItemId {
105 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d 106 hcx.hash_reference_to_item(self.hir_id(), hasher)
dfeec247
XL
107 }
108}
109
110impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItemId {
111 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d 112 hcx.hash_reference_to_item(self.hir_id(), hasher)
dfeec247
XL
113 }
114}
115
116impl<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
122impl<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
128impl<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
134impl<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}
ba9703b0
XL
139
140impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItem<'_> {
141 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d 142 let TraitItem { def_id: _, ident, ref generics, ref kind, span } = *self;
ba9703b0
XL
143
144 hcx.hash_hir_item_like(|hcx| {
145 ident.name.hash_stable(hcx, hasher);
ba9703b0
XL
146 generics.hash_stable(hcx, hasher);
147 kind.hash_stable(hcx, hasher);
148 span.hash_stable(hcx, hasher);
149 });
150 }
151}
152
153impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItem<'_> {
154 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d
XL
155 let ImplItem { def_id: _, ident, ref vis, defaultness, ref generics, ref kind, span } =
156 *self;
ba9703b0
XL
157
158 hcx.hash_hir_item_like(|hcx| {
159 ident.name.hash_stable(hcx, hasher);
160 vis.hash_stable(hcx, hasher);
161 defaultness.hash_stable(hcx, hasher);
ba9703b0
XL
162 generics.hash_stable(hcx, hasher);
163 kind.hash_stable(hcx, hasher);
164 span.hash_stable(hcx, hasher);
165 });
166 }
167}
168
6a06907d
XL
169impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ForeignItem<'_> {
170 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
171 let ForeignItem { def_id: _, ident, ref kind, span, ref vis } = *self;
172
173 hcx.hash_hir_item_like(|hcx| {
174 ident.name.hash_stable(hcx, hasher);
175 kind.hash_stable(hcx, hasher);
176 span.hash_stable(hcx, hasher);
177 vis.hash_stable(hcx, hasher);
178 });
179 }
180}
181
ba9703b0
XL
182impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
183 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
6a06907d 184 let Item { ident, def_id: _, ref kind, ref vis, span } = *self;
ba9703b0
XL
185
186 hcx.hash_hir_item_like(|hcx| {
187 ident.name.hash_stable(hcx, hasher);
ba9703b0
XL
188 kind.hash_stable(hcx, hasher);
189 vis.hash_stable(hcx, hasher);
190 span.hash_stable(hcx, hasher);
191 });
192 }
193}
6a06907d
XL
194
195impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for MacroDef<'_> {
196 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
197 let MacroDef { ident, def_id: _, ref ast, ref vis, span } = *self;
198
199 hcx.hash_hir_item_like(|hcx| {
200 ident.name.hash_stable(hcx, hasher);
201 ast.hash_stable(hcx, hasher);
202 vis.hash_stable(hcx, hasher);
203 span.hash_stable(hcx, hasher);
204 });
205 }
206}