]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_middle/src/ich/impls_hir.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / compiler / rustc_middle / src / ich / impls_hir.rs
CommitLineData
cc61c64b
XL
1//! This module contains `HashStable` implementations for various HIR data
2//! types in no particular order.
3
ba9703b0 4use crate::ich::{NodeIdHashingMode, StableHashingContext};
74b04a01 5use rustc_attr as attr;
ba9703b0 6use rustc_data_structures::fingerprint::Fingerprint;
dfeec247
XL
7use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
8use rustc_hir as hir;
9use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
ba9703b0 10use rustc_hir::definitions::DefPathHash;
48663c56 11use smallvec::SmallVec;
cc61c64b 12use std::mem;
cc61c64b 13
dfeec247 14impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> {
dfeec247
XL
15 #[inline]
16 fn hash_hir_id(&mut self, hir_id: hir::HirId, hasher: &mut StableHasher) {
17 let hcx = self;
18 match hcx.node_id_hashing_mode {
19 NodeIdHashingMode::Ignore => {
20 // Don't do anything.
21 }
22 NodeIdHashingMode::HashDefPath => {
23 let hir::HirId { owner, local_id } = hir_id;
24
25 hcx.local_def_path_hash(owner).hash_stable(hcx, hasher);
26 local_id.hash_stable(hcx, hasher);
27 }
28 }
29 }
30
31 fn hash_body_id(&mut self, id: hir::BodyId, hasher: &mut StableHasher) {
32 let hcx = self;
33 if hcx.hash_bodies() {
34 hcx.body_resolver.body(id).hash_stable(hcx, hasher);
35 }
36 }
37
74b04a01 38 fn hash_reference_to_item(&mut self, id: hir::HirId, hasher: &mut StableHasher) {
dfeec247 39 let hcx = self;
dfeec247
XL
40
41 hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
42 id.hash_stable(hcx, hasher);
43 })
44 }
45
dfeec247
XL
46 fn hash_hir_mod(&mut self, module: &hir::Mod<'_>, hasher: &mut StableHasher) {
47 let hcx = self;
48 let hir::Mod { inner: ref inner_span, ref item_ids } = *module;
49
50 inner_span.hash_stable(hcx, hasher);
51
52 // Combining the `DefPathHash`s directly is faster than feeding them
53 // into the hasher. Because we use a commutative combine, we also don't
54 // have to sort the array.
55 let item_ids_hash = item_ids
56 .iter()
57 .map(|id| {
6a06907d 58 let def_path_hash = id.to_stable_hash_key(hcx);
dfeec247
XL
59 def_path_hash.0
60 })
61 .fold(Fingerprint::ZERO, |a, b| a.combine_commutative(b));
62
63 item_ids.len().hash_stable(hcx, hasher);
64 item_ids_hash.hash_stable(hcx, hasher);
65 }
66
67 fn hash_hir_expr(&mut self, expr: &hir::Expr<'_>, hasher: &mut StableHasher) {
68 self.while_hashing_hir_bodies(true, |hcx| {
6a06907d 69 let hir::Expr { hir_id: _, ref span, ref kind } = *expr;
dfeec247
XL
70
71 span.hash_stable(hcx, hasher);
72 kind.hash_stable(hcx, hasher);
dfeec247
XL
73 })
74 }
75
76 fn hash_hir_ty(&mut self, ty: &hir::Ty<'_>, hasher: &mut StableHasher) {
77 self.while_hashing_hir_bodies(true, |hcx| {
78 let hir::Ty { hir_id: _, ref kind, ref span } = *ty;
79
80 kind.hash_stable(hcx, hasher);
81 span.hash_stable(hcx, hasher);
82 })
83 }
84
85 fn hash_hir_visibility_kind(
86 &mut self,
87 vis: &hir::VisibilityKind<'_>,
88 hasher: &mut StableHasher,
89 ) {
90 let hcx = self;
91 mem::discriminant(vis).hash_stable(hcx, hasher);
92 match *vis {
93 hir::VisibilityKind::Public | hir::VisibilityKind::Inherited => {
94 // No fields to hash.
95 }
96 hir::VisibilityKind::Crate(sugar) => {
97 sugar.hash_stable(hcx, hasher);
98 }
99 hir::VisibilityKind::Restricted { ref path, hir_id } => {
100 hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
101 hir_id.hash_stable(hcx, hasher);
102 });
103 path.hash_stable(hcx, hasher);
104 }
105 }
cc61c64b 106 }
ba9703b0
XL
107
108 fn hash_hir_item_like<F: FnOnce(&mut Self)>(&mut self, f: F) {
109 let prev_hash_node_ids = self.node_id_hashing_mode;
110 self.node_id_hashing_mode = NodeIdHashingMode::Ignore;
111
112 f(self);
113
114 self.node_id_hashing_mode = prev_hash_node_ids;
115 }
116
117 #[inline]
118 fn local_def_path_hash(&self, def_id: LocalDefId) -> DefPathHash {
119 self.local_def_path_hash(def_id)
120 }
cc61c64b
XL
121}
122
0531ce1d 123impl<'a> ToStableHashKey<StableHashingContext<'a>> for DefId {
ea8adc8c 124 type KeyType = DefPathHash;
cc61c64b 125
cc61c64b 126 #[inline]
0531ce1d 127 fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> DefPathHash {
ea8adc8c 128 hcx.def_path_hash(*self)
cc61c64b
XL
129 }
130}
131
0531ce1d 132impl<'a> HashStable<StableHashingContext<'a>> for LocalDefId {
abe05a73 133 #[inline]
e74abb32 134 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
abe05a73
XL
135 hcx.def_path_hash(self.to_def_id()).hash_stable(hcx, hasher);
136 }
137}
138
0531ce1d 139impl<'a> ToStableHashKey<StableHashingContext<'a>> for LocalDefId {
abe05a73
XL
140 type KeyType = DefPathHash;
141
142 #[inline]
0531ce1d 143 fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> DefPathHash {
abe05a73
XL
144 hcx.def_path_hash(self.to_def_id())
145 }
146}
147
0531ce1d 148impl<'a> ToStableHashKey<StableHashingContext<'a>> for CrateNum {
ea8adc8c
XL
149 type KeyType = DefPathHash;
150
151 #[inline]
0531ce1d 152 fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> DefPathHash {
ea8adc8c
XL
153 let def_id = DefId { krate: *self, index: CRATE_DEF_INDEX };
154 def_id.to_stable_hash_key(hcx)
155 }
156}
157
dfeec247 158impl<'a> ToStableHashKey<StableHashingContext<'a>> for hir::ItemLocalId {
ea8adc8c
XL
159 type KeyType = hir::ItemLocalId;
160
161 #[inline]
dfeec247 162 fn to_stable_hash_key(&self, _: &StableHashingContext<'a>) -> hir::ItemLocalId {
ea8adc8c
XL
163 *self
164 }
165}
166
dfeec247 167impl<'a> HashStable<StableHashingContext<'a>> for hir::Body<'_> {
e74abb32 168 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
dfeec247 169 let hir::Body { params, value, generator_kind } = self;
ea8adc8c
XL
170
171 hcx.with_node_id_hashing_mode(NodeIdHashingMode::Ignore, |hcx| {
e1599b0c 172 params.hash_stable(hcx, hasher);
ea8adc8c 173 value.hash_stable(hcx, hasher);
dc9dc135 174 generator_kind.hash_stable(hcx, hasher);
ea8adc8c
XL
175 });
176 }
177}
178
0531ce1d 179impl<'a> ToStableHashKey<StableHashingContext<'a>> for hir::BodyId {
ea8adc8c
XL
180 type KeyType = (DefPathHash, hir::ItemLocalId);
181
182 #[inline]
dfeec247
XL
183 fn to_stable_hash_key(
184 &self,
185 hcx: &StableHashingContext<'a>,
186 ) -> (DefPathHash, hir::ItemLocalId) {
9fa01778
XL
187 let hir::BodyId { hir_id } = *self;
188 hir_id.to_stable_hash_key(hcx)
cc61c64b
XL
189 }
190}
191
8faf50e0 192impl<'a> HashStable<StableHashingContext<'a>> for hir::TraitCandidate {
e74abb32 193 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
ea8adc8c 194 hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
dfeec247 195 let hir::TraitCandidate { def_id, import_ids } = self;
ea8adc8c
XL
196
197 def_id.hash_stable(hcx, hasher);
48663c56 198 import_ids.hash_stable(hcx, hasher);
ea8adc8c
XL
199 });
200 }
201}
202
0531ce1d 203impl<'a> ToStableHashKey<StableHashingContext<'a>> for hir::TraitCandidate {
f035d41b 204 type KeyType = (DefPathHash, SmallVec<[DefPathHash; 1]>);
ea8adc8c 205
dfeec247
XL
206 fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> Self::KeyType {
207 let hir::TraitCandidate { def_id, import_ids } = self;
208
f035d41b
XL
209 (
210 hcx.def_path_hash(*def_id),
211 import_ids.iter().map(|def_id| hcx.local_def_path_hash(*def_id)).collect(),
212 )
ea8adc8c
XL
213 }
214}
215
0531ce1d 216impl<'hir> HashStable<StableHashingContext<'hir>> for attr::InlineAttr {
e74abb32 217 fn hash_stable(&self, hcx: &mut StableHashingContext<'hir>, hasher: &mut StableHasher) {
0531ce1d
XL
218 mem::discriminant(self).hash_stable(hcx, hasher);
219 }
220}
ea8adc8c 221
29967ef6
XL
222impl<'hir> HashStable<StableHashingContext<'hir>> for attr::InstructionSetAttr {
223 fn hash_stable(&self, hcx: &mut StableHashingContext<'hir>, hasher: &mut StableHasher) {
224 mem::discriminant(self).hash_stable(hcx, hasher);
225 }
226}
227
9fa01778 228impl<'hir> HashStable<StableHashingContext<'hir>> for attr::OptimizeAttr {
e74abb32 229 fn hash_stable(&self, hcx: &mut StableHashingContext<'hir>, hasher: &mut StableHasher) {
9fa01778
XL
230 mem::discriminant(self).hash_stable(hcx, hasher);
231 }
232}