]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir/src/stable_hash_impls.rs
Rollup merge of #81682 - JulianKnodt:bit_set_iter_benchmarks, r=oli-obk
[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, ForeignItemId, ImplItem, ImplItemId, Item, ItemId, Mod, TraitItem, TraitItemId,
5     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 TraitItemId {
38     type KeyType = (DefPathHash, ItemLocalId);
39
40     #[inline]
41     fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
42         self.hir_id.to_stable_hash_key(hcx)
43     }
44 }
45
46 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
47     type KeyType = (DefPathHash, ItemLocalId);
48
49     #[inline]
50     fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
51         self.hir_id.to_stable_hash_key(hcx)
52     }
53 }
54
55 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId {
56     type KeyType = (DefPathHash, ItemLocalId);
57
58     #[inline]
59     fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
60         self.hir_id.to_stable_hash_key(hcx)
61     }
62 }
63
64 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HirId {
65     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
66         hcx.hash_hir_id(*self, hasher)
67     }
68 }
69
70 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId {
71     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
72         hcx.hash_body_id(*self, hasher)
73     }
74 }
75
76 // The following implementations of HashStable for `ItemId`, `TraitItemId`, and
77 // `ImplItemId` deserve special attention. Normally we do not hash `NodeId`s within
78 // the HIR, since they just signify a HIR nodes own path. But `ItemId` et al
79 // are used when another item in the HIR is *referenced* and we certainly
80 // want to pick up on a reference changing its target, so we hash the NodeIds
81 // in "DefPath Mode".
82
83 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ItemId {
84     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
85         hcx.hash_reference_to_item(self.id, hasher)
86     }
87 }
88
89 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ForeignItemId {
90     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
91         hcx.hash_reference_to_item(self.hir_id, hasher)
92     }
93 }
94
95 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItemId {
96     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
97         hcx.hash_reference_to_item(self.hir_id, hasher)
98     }
99 }
100
101 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItemId {
102     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
103         hcx.hash_reference_to_item(self.hir_id, hasher)
104     }
105 }
106
107 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Mod<'_> {
108     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
109         hcx.hash_hir_mod(self, hasher)
110     }
111 }
112
113 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Expr<'_> {
114     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
115         hcx.hash_hir_expr(self, hasher)
116     }
117 }
118
119 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Ty<'_> {
120     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
121         hcx.hash_hir_ty(self, hasher)
122     }
123 }
124
125 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for VisibilityKind<'_> {
126     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
127         hcx.hash_hir_visibility_kind(self, hasher)
128     }
129 }
130
131 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItem<'_> {
132     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
133         let TraitItem { hir_id: _, ident, ref attrs, ref generics, ref kind, span } = *self;
134
135         hcx.hash_hir_item_like(|hcx| {
136             ident.name.hash_stable(hcx, hasher);
137             attrs.hash_stable(hcx, hasher);
138             generics.hash_stable(hcx, hasher);
139             kind.hash_stable(hcx, hasher);
140             span.hash_stable(hcx, hasher);
141         });
142     }
143 }
144
145 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItem<'_> {
146     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
147         let ImplItem {
148             hir_id: _,
149             ident,
150             ref vis,
151             defaultness,
152             ref attrs,
153             ref generics,
154             ref kind,
155             span,
156         } = *self;
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);
162             attrs.hash_stable(hcx, hasher);
163             generics.hash_stable(hcx, hasher);
164             kind.hash_stable(hcx, hasher);
165             span.hash_stable(hcx, hasher);
166         });
167     }
168 }
169
170 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
171     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
172         let Item { ident, ref attrs, hir_id: _, ref kind, ref vis, span } = *self;
173
174         hcx.hash_hir_item_like(|hcx| {
175             ident.name.hash_stable(hcx, hasher);
176             attrs.hash_stable(hcx, hasher);
177             kind.hash_stable(hcx, hasher);
178             vis.hash_stable(hcx, hasher);
179             span.hash_stable(hcx, hasher);
180         });
181     }
182 }