]>
Commit | Line | Data |
---|---|---|
9fa01778 | 1 | use crate::sip128::SipHasher128; |
e74abb32 | 2 | use rustc_index::bit_set; |
dfeec247 XL |
3 | use rustc_index::vec; |
4 | use smallvec::SmallVec; | |
5 | use std::hash::{BuildHasher, Hash, Hasher}; | |
6 | use std::mem; | |
476ff2be | 7 | |
1b1a35ee XL |
8 | #[cfg(test)] |
9 | mod tests; | |
10 | ||
abe05a73 XL |
11 | /// When hashing something that ends up affecting properties like symbol names, |
12 | /// we want these symbol names to be calculated independently of other factors | |
13 | /// like what architecture you're compiling *from*. | |
476ff2be | 14 | /// |
abe05a73 XL |
15 | /// To that end we always convert integers to little-endian format before |
16 | /// hashing and the architecture dependent `isize` and `usize` types are | |
17 | /// extended to 64 bits if needed. | |
e74abb32 | 18 | pub struct StableHasher { |
abe05a73 | 19 | state: SipHasher128, |
476ff2be SL |
20 | } |
21 | ||
e74abb32 | 22 | impl ::std::fmt::Debug for StableHasher { |
29967ef6 | 23 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
cc61c64b XL |
24 | write!(f, "{:?}", self.state) |
25 | } | |
26 | } | |
27 | ||
476ff2be | 28 | pub trait StableHasherResult: Sized { |
e74abb32 | 29 | fn finish(hasher: StableHasher) -> Self; |
476ff2be SL |
30 | } |
31 | ||
e74abb32 | 32 | impl StableHasher { |
74b04a01 | 33 | #[inline] |
476ff2be | 34 | pub fn new() -> Self { |
dfeec247 | 35 | StableHasher { state: SipHasher128::new_with_keys(0, 0) } |
476ff2be SL |
36 | } |
37 | ||
6a06907d | 38 | #[inline] |
e74abb32 | 39 | pub fn finish<W: StableHasherResult>(self) -> W { |
476ff2be SL |
40 | W::finish(self) |
41 | } | |
42 | } | |
43 | ||
041b39d2 | 44 | impl StableHasherResult for u128 { |
a2a8927a | 45 | #[inline] |
e74abb32 | 46 | fn finish(hasher: StableHasher) -> Self { |
abe05a73 | 47 | let (_0, _1) = hasher.finalize(); |
dc9dc135 | 48 | u128::from(_0) | (u128::from(_1) << 64) |
041b39d2 XL |
49 | } |
50 | } | |
51 | ||
476ff2be | 52 | impl StableHasherResult for u64 { |
a2a8927a | 53 | #[inline] |
e74abb32 | 54 | fn finish(hasher: StableHasher) -> Self { |
abe05a73 | 55 | hasher.finalize().0 |
476ff2be SL |
56 | } |
57 | } | |
58 | ||
e74abb32 | 59 | impl StableHasher { |
476ff2be | 60 | #[inline] |
abe05a73 XL |
61 | pub fn finalize(self) -> (u64, u64) { |
62 | self.state.finish128() | |
476ff2be | 63 | } |
476ff2be SL |
64 | } |
65 | ||
e74abb32 | 66 | impl Hasher for StableHasher { |
476ff2be | 67 | fn finish(&self) -> u64 { |
abe05a73 | 68 | panic!("use StableHasher::finalize instead"); |
476ff2be SL |
69 | } |
70 | ||
71 | #[inline] | |
72 | fn write(&mut self, bytes: &[u8]) { | |
73 | self.state.write(bytes); | |
476ff2be SL |
74 | } |
75 | ||
76 | #[inline] | |
77 | fn write_u8(&mut self, i: u8) { | |
78 | self.state.write_u8(i); | |
476ff2be SL |
79 | } |
80 | ||
81 | #[inline] | |
82 | fn write_u16(&mut self, i: u16) { | |
5099ac24 | 83 | self.state.short_write(i.to_le_bytes()); |
476ff2be SL |
84 | } |
85 | ||
86 | #[inline] | |
87 | fn write_u32(&mut self, i: u32) { | |
5099ac24 | 88 | self.state.short_write(i.to_le_bytes()); |
476ff2be SL |
89 | } |
90 | ||
91 | #[inline] | |
92 | fn write_u64(&mut self, i: u64) { | |
5099ac24 | 93 | self.state.short_write(i.to_le_bytes()); |
abe05a73 XL |
94 | } |
95 | ||
96 | #[inline] | |
97 | fn write_u128(&mut self, i: u128) { | |
5099ac24 | 98 | self.state.write(&i.to_le_bytes()); |
476ff2be SL |
99 | } |
100 | ||
101 | #[inline] | |
102 | fn write_usize(&mut self, i: usize) { | |
abe05a73 XL |
103 | // Always treat usize as u64 so we get the same results on 32 and 64 bit |
104 | // platforms. This is important for symbol hashes when cross compiling, | |
105 | // for example. | |
5099ac24 | 106 | self.state.short_write((i as u64).to_le_bytes()); |
476ff2be SL |
107 | } |
108 | ||
109 | #[inline] | |
110 | fn write_i8(&mut self, i: i8) { | |
111 | self.state.write_i8(i); | |
476ff2be SL |
112 | } |
113 | ||
114 | #[inline] | |
115 | fn write_i16(&mut self, i: i16) { | |
5099ac24 | 116 | self.state.short_write((i as u16).to_le_bytes()); |
476ff2be SL |
117 | } |
118 | ||
119 | #[inline] | |
120 | fn write_i32(&mut self, i: i32) { | |
5099ac24 | 121 | self.state.short_write((i as u32).to_le_bytes()); |
476ff2be SL |
122 | } |
123 | ||
124 | #[inline] | |
125 | fn write_i64(&mut self, i: i64) { | |
5099ac24 | 126 | self.state.short_write((i as u64).to_le_bytes()); |
abe05a73 XL |
127 | } |
128 | ||
129 | #[inline] | |
130 | fn write_i128(&mut self, i: i128) { | |
5099ac24 | 131 | self.state.write(&(i as u128).to_le_bytes()); |
476ff2be SL |
132 | } |
133 | ||
134 | #[inline] | |
135 | fn write_isize(&mut self, i: isize) { | |
5099ac24 | 136 | // Always treat isize as a 64-bit number so we get the same results on 32 and 64 bit |
abe05a73 | 137 | // platforms. This is important for symbol hashes when cross compiling, |
1b1a35ee XL |
138 | // for example. Sign extending here is preferable as it means that the |
139 | // same negative number hashes the same on both 32 and 64 bit platforms. | |
5099ac24 FG |
140 | let value = i as u64; |
141 | ||
142 | // Cold path | |
143 | #[cold] | |
144 | #[inline(never)] | |
145 | fn hash_value(state: &mut SipHasher128, value: u64) { | |
146 | state.write_u8(0xFF); | |
147 | state.short_write(value.to_le_bytes()); | |
148 | } | |
149 | ||
150 | // `isize` values often seem to have a small (positive) numeric value in practice. | |
151 | // To exploit this, if the value is small, we will hash a smaller amount of bytes. | |
152 | // However, we cannot just skip the leading zero bytes, as that would produce the same hash | |
153 | // e.g. if you hash two values that have the same bit pattern when they are swapped. | |
154 | // See https://github.com/rust-lang/rust/pull/93014 for context. | |
155 | // | |
156 | // Therefore, we employ the following strategy: | |
157 | // 1) When we encounter a value that fits within a single byte (the most common case), we | |
158 | // hash just that byte. This is the most common case that is being optimized. However, we do | |
159 | // not do this for the value 0xFF, as that is a reserved prefix (a bit like in UTF-8). | |
160 | // 2) When we encounter a larger value, we hash a "marker" 0xFF and then the corresponding | |
161 | // 8 bytes. Since this prefix cannot occur when we hash a single byte, when we hash two | |
162 | // `isize`s that fit within a different amount of bytes, they should always produce a different | |
163 | // byte stream for the hasher. | |
164 | if value < 0xFF { | |
165 | self.state.write_u8(value as u8); | |
166 | } else { | |
167 | hash_value(&mut self.state, value); | |
168 | } | |
476ff2be SL |
169 | } |
170 | } | |
cc61c64b | 171 | |
cc61c64b | 172 | /// Something that implements `HashStable<CTX>` can be hashed in a way that is |
3b2f2976 | 173 | /// stable across multiple compilation sessions. |
48663c56 XL |
174 | /// |
175 | /// Note that `HashStable` imposes rather more strict requirements than usual | |
176 | /// hash functions: | |
177 | /// | |
178 | /// - Stable hashes are sometimes used as identifiers. Therefore they must | |
179 | /// conform to the corresponding `PartialEq` implementations: | |
180 | /// | |
181 | /// - `x == y` implies `hash_stable(x) == hash_stable(y)`, and | |
182 | /// - `x != y` implies `hash_stable(x) != hash_stable(y)`. | |
183 | /// | |
184 | /// That second condition is usually not required for hash functions | |
185 | /// (e.g. `Hash`). In practice this means that `hash_stable` must feed any | |
60c5eb7d | 186 | /// information into the hasher that a `PartialEq` comparison takes into |
48663c56 XL |
187 | /// account. See [#49300](https://github.com/rust-lang/rust/issues/49300) |
188 | /// for an example where violating this invariant has caused trouble in the | |
189 | /// past. | |
190 | /// | |
191 | /// - `hash_stable()` must be independent of the current | |
192 | /// compilation session. E.g. they must not hash memory addresses or other | |
193 | /// things that are "randomly" assigned per compilation session. | |
194 | /// | |
195 | /// - `hash_stable()` must be independent of the host architecture. The | |
196 | /// `StableHasher` takes care of endianness and `isize`/`usize` platform | |
197 | /// differences. | |
cc61c64b | 198 | pub trait HashStable<CTX> { |
e74abb32 | 199 | fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher); |
cc61c64b XL |
200 | } |
201 | ||
ea8adc8c XL |
202 | /// Implement this for types that can be turned into stable keys like, for |
203 | /// example, for DefId that can be converted to a DefPathHash. This is used for | |
204 | /// bringing maps into a predictable order before hashing them. | |
205 | pub trait ToStableHashKey<HCX> { | |
e74abb32 | 206 | type KeyType: Ord + Sized + HashStable<HCX>; |
ea8adc8c XL |
207 | fn to_stable_hash_key(&self, hcx: &HCX) -> Self::KeyType; |
208 | } | |
209 | ||
cc61c64b XL |
210 | // Implement HashStable by just calling `Hash::hash()`. This works fine for |
211 | // self-contained values that don't depend on the hashing context `CTX`. | |
b7449926 | 212 | #[macro_export] |
cc61c64b | 213 | macro_rules! impl_stable_hash_via_hash { |
dfeec247 | 214 | ($t:ty) => { |
b7449926 | 215 | impl<CTX> $crate::stable_hasher::HashStable<CTX> for $t { |
cc61c64b | 216 | #[inline] |
dfeec247 | 217 | fn hash_stable(&self, _: &mut CTX, hasher: &mut $crate::stable_hasher::StableHasher) { |
cc61c64b XL |
218 | ::std::hash::Hash::hash(self, hasher); |
219 | } | |
220 | } | |
dfeec247 | 221 | }; |
cc61c64b XL |
222 | } |
223 | ||
224 | impl_stable_hash_via_hash!(i8); | |
225 | impl_stable_hash_via_hash!(i16); | |
226 | impl_stable_hash_via_hash!(i32); | |
227 | impl_stable_hash_via_hash!(i64); | |
228 | impl_stable_hash_via_hash!(isize); | |
229 | ||
230 | impl_stable_hash_via_hash!(u8); | |
231 | impl_stable_hash_via_hash!(u16); | |
232 | impl_stable_hash_via_hash!(u32); | |
233 | impl_stable_hash_via_hash!(u64); | |
234 | impl_stable_hash_via_hash!(usize); | |
235 | ||
236 | impl_stable_hash_via_hash!(u128); | |
237 | impl_stable_hash_via_hash!(i128); | |
238 | ||
239 | impl_stable_hash_via_hash!(char); | |
240 | impl_stable_hash_via_hash!(()); | |
241 | ||
c295e0f8 XL |
242 | impl<CTX> HashStable<CTX> for ! { |
243 | fn hash_stable(&self, _ctx: &mut CTX, _hasher: &mut StableHasher) { | |
244 | unreachable!() | |
245 | } | |
246 | } | |
247 | ||
b7449926 | 248 | impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 { |
e74abb32 | 249 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
b7449926 XL |
250 | self.get().hash_stable(ctx, hasher) |
251 | } | |
252 | } | |
253 | ||
ba9703b0 XL |
254 | impl<CTX> HashStable<CTX> for ::std::num::NonZeroUsize { |
255 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { | |
256 | self.get().hash_stable(ctx, hasher) | |
257 | } | |
258 | } | |
259 | ||
cc61c64b | 260 | impl<CTX> HashStable<CTX> for f32 { |
e74abb32 | 261 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
3c0e092e | 262 | let val: u32 = unsafe { ::std::mem::transmute(*self) }; |
cc61c64b XL |
263 | val.hash_stable(ctx, hasher); |
264 | } | |
265 | } | |
266 | ||
267 | impl<CTX> HashStable<CTX> for f64 { | |
e74abb32 | 268 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
3c0e092e | 269 | let val: u64 = unsafe { ::std::mem::transmute(*self) }; |
cc61c64b XL |
270 | val.hash_stable(ctx, hasher); |
271 | } | |
272 | } | |
273 | ||
0531ce1d | 274 | impl<CTX> HashStable<CTX> for ::std::cmp::Ordering { |
e74abb32 | 275 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
0531ce1d XL |
276 | (*self as i8).hash_stable(ctx, hasher); |
277 | } | |
278 | } | |
279 | ||
041b39d2 | 280 | impl<T1: HashStable<CTX>, CTX> HashStable<CTX> for (T1,) { |
e74abb32 | 281 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
ea8adc8c XL |
282 | let (ref _0,) = *self; |
283 | _0.hash_stable(ctx, hasher); | |
041b39d2 XL |
284 | } |
285 | } | |
286 | ||
cc61c64b | 287 | impl<T1: HashStable<CTX>, T2: HashStable<CTX>, CTX> HashStable<CTX> for (T1, T2) { |
e74abb32 | 288 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
ea8adc8c XL |
289 | let (ref _0, ref _1) = *self; |
290 | _0.hash_stable(ctx, hasher); | |
291 | _1.hash_stable(ctx, hasher); | |
292 | } | |
293 | } | |
294 | ||
295 | impl<T1, T2, T3, CTX> HashStable<CTX> for (T1, T2, T3) | |
dfeec247 XL |
296 | where |
297 | T1: HashStable<CTX>, | |
298 | T2: HashStable<CTX>, | |
299 | T3: HashStable<CTX>, | |
ea8adc8c | 300 | { |
e74abb32 | 301 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
ea8adc8c XL |
302 | let (ref _0, ref _1, ref _2) = *self; |
303 | _0.hash_stable(ctx, hasher); | |
304 | _1.hash_stable(ctx, hasher); | |
305 | _2.hash_stable(ctx, hasher); | |
cc61c64b XL |
306 | } |
307 | } | |
308 | ||
b7449926 | 309 | impl<T1, T2, T3, T4, CTX> HashStable<CTX> for (T1, T2, T3, T4) |
dfeec247 XL |
310 | where |
311 | T1: HashStable<CTX>, | |
312 | T2: HashStable<CTX>, | |
313 | T3: HashStable<CTX>, | |
314 | T4: HashStable<CTX>, | |
b7449926 | 315 | { |
e74abb32 | 316 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
b7449926 XL |
317 | let (ref _0, ref _1, ref _2, ref _3) = *self; |
318 | _0.hash_stable(ctx, hasher); | |
319 | _1.hash_stable(ctx, hasher); | |
320 | _2.hash_stable(ctx, hasher); | |
321 | _3.hash_stable(ctx, hasher); | |
322 | } | |
323 | } | |
324 | ||
cc61c64b | 325 | impl<T: HashStable<CTX>, CTX> HashStable<CTX> for [T] { |
e74abb32 | 326 | default fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
cc61c64b XL |
327 | self.len().hash_stable(ctx, hasher); |
328 | for item in self { | |
329 | item.hash_stable(ctx, hasher); | |
330 | } | |
331 | } | |
332 | } | |
333 | ||
3c0e092e XL |
334 | impl<CTX> HashStable<CTX> for [u8] { |
335 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { | |
336 | self.len().hash_stable(ctx, hasher); | |
337 | hasher.write(self); | |
338 | } | |
339 | } | |
340 | ||
cc61c64b XL |
341 | impl<T: HashStable<CTX>, CTX> HashStable<CTX> for Vec<T> { |
342 | #[inline] | |
e74abb32 | 343 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
cc61c64b XL |
344 | (&self[..]).hash_stable(ctx, hasher); |
345 | } | |
346 | } | |
347 | ||
dc9dc135 | 348 | impl<K, V, R, CTX> HashStable<CTX> for indexmap::IndexMap<K, V, R> |
dfeec247 XL |
349 | where |
350 | K: HashStable<CTX> + Eq + Hash, | |
351 | V: HashStable<CTX>, | |
352 | R: BuildHasher, | |
dc9dc135 XL |
353 | { |
354 | #[inline] | |
e74abb32 | 355 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
dc9dc135 XL |
356 | self.len().hash_stable(ctx, hasher); |
357 | for kv in self { | |
358 | kv.hash_stable(ctx, hasher); | |
359 | } | |
360 | } | |
361 | } | |
362 | ||
363 | impl<K, R, CTX> HashStable<CTX> for indexmap::IndexSet<K, R> | |
dfeec247 XL |
364 | where |
365 | K: HashStable<CTX> + Eq + Hash, | |
366 | R: BuildHasher, | |
dc9dc135 XL |
367 | { |
368 | #[inline] | |
e74abb32 | 369 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
dc9dc135 XL |
370 | self.len().hash_stable(ctx, hasher); |
371 | for key in self { | |
372 | key.hash_stable(ctx, hasher); | |
373 | } | |
374 | } | |
375 | } | |
376 | ||
dfeec247 XL |
377 | impl<A, CTX> HashStable<CTX> for SmallVec<[A; 1]> |
378 | where | |
379 | A: HashStable<CTX>, | |
380 | { | |
48663c56 | 381 | #[inline] |
e74abb32 | 382 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
48663c56 XL |
383 | (&self[..]).hash_stable(ctx, hasher); |
384 | } | |
385 | } | |
386 | ||
ea8adc8c | 387 | impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for Box<T> { |
041b39d2 | 388 | #[inline] |
e74abb32 | 389 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
041b39d2 XL |
390 | (**self).hash_stable(ctx, hasher); |
391 | } | |
392 | } | |
393 | ||
ea8adc8c XL |
394 | impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for ::std::rc::Rc<T> { |
395 | #[inline] | |
e74abb32 | 396 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
ea8adc8c XL |
397 | (**self).hash_stable(ctx, hasher); |
398 | } | |
399 | } | |
400 | ||
401 | impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for ::std::sync::Arc<T> { | |
041b39d2 | 402 | #[inline] |
e74abb32 | 403 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
041b39d2 XL |
404 | (**self).hash_stable(ctx, hasher); |
405 | } | |
406 | } | |
407 | ||
cc61c64b XL |
408 | impl<CTX> HashStable<CTX> for str { |
409 | #[inline] | |
a2a8927a XL |
410 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
411 | self.as_bytes().hash_stable(ctx, hasher); | |
cc61c64b XL |
412 | } |
413 | } | |
414 | ||
7cac9316 XL |
415 | impl<CTX> HashStable<CTX> for String { |
416 | #[inline] | |
e74abb32 | 417 | fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { |
7cac9316 XL |
418 | (&self[..]).hash_stable(hcx, hasher); |
419 | } | |
420 | } | |
421 | ||
ea8adc8c XL |
422 | impl<HCX> ToStableHashKey<HCX> for String { |
423 | type KeyType = String; | |
424 | #[inline] | |
425 | fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType { | |
426 | self.clone() | |
427 | } | |
428 | } | |
429 | ||
cc61c64b XL |
430 | impl<CTX> HashStable<CTX> for bool { |
431 | #[inline] | |
e74abb32 | 432 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
cc61c64b XL |
433 | (if *self { 1u8 } else { 0u8 }).hash_stable(ctx, hasher); |
434 | } | |
435 | } | |
436 | ||
cc61c64b | 437 | impl<T, CTX> HashStable<CTX> for Option<T> |
dfeec247 XL |
438 | where |
439 | T: HashStable<CTX>, | |
cc61c64b XL |
440 | { |
441 | #[inline] | |
e74abb32 | 442 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
cc61c64b XL |
443 | if let Some(ref value) = *self { |
444 | 1u8.hash_stable(ctx, hasher); | |
445 | value.hash_stable(ctx, hasher); | |
446 | } else { | |
447 | 0u8.hash_stable(ctx, hasher); | |
448 | } | |
449 | } | |
450 | } | |
451 | ||
ea8adc8c | 452 | impl<T1, T2, CTX> HashStable<CTX> for Result<T1, T2> |
dfeec247 XL |
453 | where |
454 | T1: HashStable<CTX>, | |
455 | T2: HashStable<CTX>, | |
ea8adc8c XL |
456 | { |
457 | #[inline] | |
e74abb32 | 458 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
ea8adc8c XL |
459 | mem::discriminant(self).hash_stable(ctx, hasher); |
460 | match *self { | |
461 | Ok(ref x) => x.hash_stable(ctx, hasher), | |
462 | Err(ref x) => x.hash_stable(ctx, hasher), | |
463 | } | |
464 | } | |
465 | } | |
466 | ||
cc61c64b | 467 | impl<'a, T, CTX> HashStable<CTX> for &'a T |
dfeec247 XL |
468 | where |
469 | T: HashStable<CTX> + ?Sized, | |
cc61c64b XL |
470 | { |
471 | #[inline] | |
e74abb32 | 472 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
cc61c64b XL |
473 | (**self).hash_stable(ctx, hasher); |
474 | } | |
475 | } | |
476 | ||
477 | impl<T, CTX> HashStable<CTX> for ::std::mem::Discriminant<T> { | |
478 | #[inline] | |
e74abb32 | 479 | fn hash_stable(&self, _: &mut CTX, hasher: &mut StableHasher) { |
cc61c64b XL |
480 | ::std::hash::Hash::hash(self, hasher); |
481 | } | |
482 | } | |
483 | ||
60c5eb7d | 484 | impl<T, CTX> HashStable<CTX> for ::std::ops::RangeInclusive<T> |
dfeec247 XL |
485 | where |
486 | T: HashStable<CTX>, | |
60c5eb7d XL |
487 | { |
488 | #[inline] | |
489 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { | |
490 | self.start().hash_stable(ctx, hasher); | |
491 | self.end().hash_stable(ctx, hasher); | |
492 | } | |
493 | } | |
494 | ||
e74abb32 | 495 | impl<I: vec::Idx, T, CTX> HashStable<CTX> for vec::IndexVec<I, T> |
dfeec247 XL |
496 | where |
497 | T: HashStable<CTX>, | |
cc61c64b | 498 | { |
e74abb32 | 499 | fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { |
cc61c64b | 500 | self.len().hash_stable(ctx, hasher); |
ea8adc8c | 501 | for v in &self.raw { |
cc61c64b XL |
502 | v.hash_stable(ctx, hasher); |
503 | } | |
504 | } | |
505 | } | |
506 | ||
dfeec247 | 507 | impl<I: vec::Idx, CTX> HashStable<CTX> for bit_set::BitSet<I> { |
a2a8927a XL |
508 | fn hash_stable(&self, _ctx: &mut CTX, hasher: &mut StableHasher) { |
509 | ::std::hash::Hash::hash(self, hasher); | |
cc61c64b XL |
510 | } |
511 | } | |
512 | ||
dfeec247 | 513 | impl<R: vec::Idx, C: vec::Idx, CTX> HashStable<CTX> for bit_set::BitMatrix<R, C> { |
a2a8927a XL |
514 | fn hash_stable(&self, _ctx: &mut CTX, hasher: &mut StableHasher) { |
515 | ::std::hash::Hash::hash(self, hasher); | |
dc9dc135 XL |
516 | } |
517 | } | |
518 | ||
3dfed10e XL |
519 | impl<T, CTX> HashStable<CTX> for bit_set::FiniteBitSet<T> |
520 | where | |
521 | T: HashStable<CTX> + bit_set::FiniteBitSetTy, | |
522 | { | |
523 | fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { | |
524 | self.0.hash_stable(hcx, hasher); | |
525 | } | |
526 | } | |
527 | ||
ea8adc8c XL |
528 | impl_stable_hash_via_hash!(::std::path::Path); |
529 | impl_stable_hash_via_hash!(::std::path::PathBuf); | |
530 | ||
531 | impl<K, V, R, HCX> HashStable<HCX> for ::std::collections::HashMap<K, V, R> | |
dfeec247 XL |
532 | where |
533 | K: ToStableHashKey<HCX> + Eq, | |
534 | V: HashStable<HCX>, | |
535 | R: BuildHasher, | |
cc61c64b | 536 | { |
ea8adc8c | 537 | #[inline] |
e74abb32 | 538 | fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { |
a2a8927a XL |
539 | stable_hash_reduce(hcx, hasher, self.iter(), self.len(), |hasher, hcx, (key, value)| { |
540 | let key = key.to_stable_hash_key(hcx); | |
541 | key.hash_stable(hcx, hasher); | |
542 | value.hash_stable(hcx, hasher); | |
543 | }); | |
ea8adc8c XL |
544 | } |
545 | } | |
546 | ||
547 | impl<K, R, HCX> HashStable<HCX> for ::std::collections::HashSet<K, R> | |
dfeec247 XL |
548 | where |
549 | K: ToStableHashKey<HCX> + Eq, | |
550 | R: BuildHasher, | |
ea8adc8c | 551 | { |
e74abb32 | 552 | fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { |
a2a8927a XL |
553 | stable_hash_reduce(hcx, hasher, self.iter(), self.len(), |hasher, hcx, key| { |
554 | let key = key.to_stable_hash_key(hcx); | |
555 | key.hash_stable(hcx, hasher); | |
556 | }); | |
ea8adc8c XL |
557 | } |
558 | } | |
559 | ||
560 | impl<K, V, HCX> HashStable<HCX> for ::std::collections::BTreeMap<K, V> | |
dfeec247 XL |
561 | where |
562 | K: ToStableHashKey<HCX>, | |
563 | V: HashStable<HCX>, | |
ea8adc8c | 564 | { |
e74abb32 | 565 | fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { |
a2a8927a XL |
566 | stable_hash_reduce(hcx, hasher, self.iter(), self.len(), |hasher, hcx, (key, value)| { |
567 | let key = key.to_stable_hash_key(hcx); | |
568 | key.hash_stable(hcx, hasher); | |
569 | value.hash_stable(hcx, hasher); | |
570 | }); | |
ea8adc8c XL |
571 | } |
572 | } | |
573 | ||
574 | impl<K, HCX> HashStable<HCX> for ::std::collections::BTreeSet<K> | |
dfeec247 XL |
575 | where |
576 | K: ToStableHashKey<HCX>, | |
ea8adc8c | 577 | { |
e74abb32 | 578 | fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { |
a2a8927a XL |
579 | stable_hash_reduce(hcx, hasher, self.iter(), self.len(), |hasher, hcx, key| { |
580 | let key = key.to_stable_hash_key(hcx); | |
581 | key.hash_stable(hcx, hasher); | |
582 | }); | |
ea8adc8c XL |
583 | } |
584 | } | |
585 | ||
a2a8927a | 586 | fn stable_hash_reduce<HCX, I, C, F>( |
ea8adc8c | 587 | hcx: &mut HCX, |
e74abb32 | 588 | hasher: &mut StableHasher, |
a2a8927a XL |
589 | mut collection: C, |
590 | length: usize, | |
591 | hash_function: F, | |
dfeec247 | 592 | ) where |
a2a8927a XL |
593 | C: Iterator<Item = I>, |
594 | F: Fn(&mut StableHasher, &mut HCX, I), | |
ea8adc8c | 595 | { |
a2a8927a XL |
596 | length.hash_stable(hcx, hasher); |
597 | ||
598 | match length { | |
599 | 1 => { | |
600 | hash_function(hasher, hcx, collection.next().unwrap()); | |
601 | } | |
602 | _ => { | |
603 | let hash = collection | |
604 | .map(|value| { | |
605 | let mut hasher = StableHasher::new(); | |
606 | hash_function(&mut hasher, hcx, value); | |
607 | hasher.finish::<u128>() | |
608 | }) | |
609 | .reduce(|accum, value| accum.wrapping_add(value)); | |
610 | hash.hash_stable(hcx, hasher); | |
611 | } | |
612 | } | |
ea8adc8c | 613 | } |
5099ac24 | 614 | |
5099ac24 FG |
615 | /// Controls what data we do or not not hash. |
616 | /// Whenever a `HashStable` implementation caches its | |
617 | /// result, it needs to include `HashingControls` as part | |
618 | /// of the key, to ensure that is does not produce an incorrect | |
619 | /// result (for example, using a `Fingerprint` produced while | |
5e7ed085 | 620 | /// hashing `Span`s when a `Fingerprint` without `Span`s is |
5099ac24 FG |
621 | /// being requested) |
622 | #[derive(Clone, Hash, Eq, PartialEq, Debug)] | |
623 | pub struct HashingControls { | |
624 | pub hash_spans: bool, | |
5099ac24 | 625 | } |