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