1 use std::hash::{Hash, Hasher, BuildHasher};
2 use std::marker::PhantomData;
4 use smallvec::SmallVec;
5 use crate::sip128::SipHasher128;
6 use crate::indexed_vec;
9 /// When hashing something that ends up affecting properties like symbol names,
10 /// we want these symbol names to be calculated independently of other factors
11 /// like what architecture you're compiling *from*.
13 /// To that end we always convert integers to little-endian format before
14 /// hashing and the architecture dependent `isize` and `usize` types are
15 /// extended to 64 bits if needed.
16 pub struct StableHasher<W> {
19 width: PhantomData<W>,
22 impl<W: StableHasherResult> ::std::fmt::Debug for StableHasher<W> {
23 fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
24 write!(f, "{:?}", self.state)
28 pub trait StableHasherResult: Sized {
29 fn finish(hasher: StableHasher<Self>) -> Self;
32 impl<W: StableHasherResult> StableHasher<W> {
33 pub fn new() -> Self {
35 state: SipHasher128::new_with_keys(0, 0),
41 pub fn finish(self) -> W {
46 impl StableHasherResult for u128 {
47 fn finish(hasher: StableHasher<Self>) -> Self {
48 let (_0, _1) = hasher.finalize();
49 (_0 as u128) | ((_1 as u128) << 64)
53 impl StableHasherResult for u64 {
54 fn finish(hasher: StableHasher<Self>) -> Self {
59 impl<W> StableHasher<W> {
61 pub fn finalize(self) -> (u64, u64) {
62 self.state.finish128()
66 pub fn bytes_hashed(&self) -> u64 {
71 impl<W> Hasher for StableHasher<W> {
72 fn finish(&self) -> u64 {
73 panic!("use StableHasher::finalize instead");
77 fn write(&mut self, bytes: &[u8]) {
78 self.state.write(bytes);
79 self.bytes_hashed += bytes.len() as u64;
83 fn write_u8(&mut self, i: u8) {
84 self.state.write_u8(i);
85 self.bytes_hashed += 1;
89 fn write_u16(&mut self, i: u16) {
90 self.state.write_u16(i.to_le());
91 self.bytes_hashed += 2;
95 fn write_u32(&mut self, i: u32) {
96 self.state.write_u32(i.to_le());
97 self.bytes_hashed += 4;
101 fn write_u64(&mut self, i: u64) {
102 self.state.write_u64(i.to_le());
103 self.bytes_hashed += 8;
107 fn write_u128(&mut self, i: u128) {
108 self.state.write_u128(i.to_le());
109 self.bytes_hashed += 16;
113 fn write_usize(&mut self, i: usize) {
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,
117 self.state.write_u64((i as u64).to_le());
118 self.bytes_hashed += 8;
122 fn write_i8(&mut self, i: i8) {
123 self.state.write_i8(i);
124 self.bytes_hashed += 1;
128 fn write_i16(&mut self, i: i16) {
129 self.state.write_i16(i.to_le());
130 self.bytes_hashed += 2;
134 fn write_i32(&mut self, i: i32) {
135 self.state.write_i32(i.to_le());
136 self.bytes_hashed += 4;
140 fn write_i64(&mut self, i: i64) {
141 self.state.write_i64(i.to_le());
142 self.bytes_hashed += 8;
146 fn write_i128(&mut self, i: i128) {
147 self.state.write_i128(i.to_le());
148 self.bytes_hashed += 16;
152 fn write_isize(&mut self, i: isize) {
153 // Always treat isize as i64 so we get the same results on 32 and 64 bit
154 // platforms. This is important for symbol hashes when cross compiling,
156 self.state.write_i64((i as i64).to_le());
157 self.bytes_hashed += 8;
161 /// Something that implements `HashStable<CTX>` can be hashed in a way that is
162 /// stable across multiple compilation sessions.
163 pub trait HashStable<CTX> {
164 fn hash_stable<W: StableHasherResult>(&self,
166 hasher: &mut StableHasher<W>);
169 /// Implement this for types that can be turned into stable keys like, for
170 /// example, for DefId that can be converted to a DefPathHash. This is used for
171 /// bringing maps into a predictable order before hashing them.
172 pub trait ToStableHashKey<HCX> {
173 type KeyType: Ord + Clone + Sized + HashStable<HCX>;
174 fn to_stable_hash_key(&self, hcx: &HCX) -> Self::KeyType;
177 // Implement HashStable by just calling `Hash::hash()`. This works fine for
178 // self-contained values that don't depend on the hashing context `CTX`.
180 macro_rules! impl_stable_hash_via_hash {
182 impl<CTX> $crate::stable_hasher::HashStable<CTX> for $t {
184 fn hash_stable<W: $crate::stable_hasher::StableHasherResult>(
187 hasher: &mut $crate::stable_hasher::StableHasher<W>
189 ::std::hash::Hash::hash(self, hasher);
195 impl_stable_hash_via_hash!(i8);
196 impl_stable_hash_via_hash!(i16);
197 impl_stable_hash_via_hash!(i32);
198 impl_stable_hash_via_hash!(i64);
199 impl_stable_hash_via_hash!(isize);
201 impl_stable_hash_via_hash!(u8);
202 impl_stable_hash_via_hash!(u16);
203 impl_stable_hash_via_hash!(u32);
204 impl_stable_hash_via_hash!(u64);
205 impl_stable_hash_via_hash!(usize);
207 impl_stable_hash_via_hash!(u128);
208 impl_stable_hash_via_hash!(i128);
210 impl_stable_hash_via_hash!(char);
211 impl_stable_hash_via_hash!(());
213 impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
214 fn hash_stable<W: StableHasherResult>(&self,
216 hasher: &mut StableHasher<W>) {
217 self.get().hash_stable(ctx, hasher)
221 impl<CTX> HashStable<CTX> for f32 {
222 fn hash_stable<W: StableHasherResult>(&self,
224 hasher: &mut StableHasher<W>) {
225 let val: u32 = unsafe {
226 ::std::mem::transmute(*self)
228 val.hash_stable(ctx, hasher);
232 impl<CTX> HashStable<CTX> for f64 {
233 fn hash_stable<W: StableHasherResult>(&self,
235 hasher: &mut StableHasher<W>) {
236 let val: u64 = unsafe {
237 ::std::mem::transmute(*self)
239 val.hash_stable(ctx, hasher);
243 impl<CTX> HashStable<CTX> for ::std::cmp::Ordering {
244 fn hash_stable<W: StableHasherResult>(&self,
246 hasher: &mut StableHasher<W>) {
247 (*self as i8).hash_stable(ctx, hasher);
251 impl<T1: HashStable<CTX>, CTX> HashStable<CTX> for (T1,) {
252 fn hash_stable<W: StableHasherResult>(&self,
254 hasher: &mut StableHasher<W>) {
255 let (ref _0,) = *self;
256 _0.hash_stable(ctx, hasher);
260 impl<T1: HashStable<CTX>, T2: HashStable<CTX>, CTX> HashStable<CTX> for (T1, T2) {
261 fn hash_stable<W: StableHasherResult>(&self,
263 hasher: &mut StableHasher<W>) {
264 let (ref _0, ref _1) = *self;
265 _0.hash_stable(ctx, hasher);
266 _1.hash_stable(ctx, hasher);
270 impl<T1, T2, T3, CTX> HashStable<CTX> for (T1, T2, T3)
271 where T1: HashStable<CTX>,
275 fn hash_stable<W: StableHasherResult>(&self,
277 hasher: &mut StableHasher<W>) {
278 let (ref _0, ref _1, ref _2) = *self;
279 _0.hash_stable(ctx, hasher);
280 _1.hash_stable(ctx, hasher);
281 _2.hash_stable(ctx, hasher);
285 impl<T1, T2, T3, T4, CTX> HashStable<CTX> for (T1, T2, T3, T4)
286 where T1: HashStable<CTX>,
291 fn hash_stable<W: StableHasherResult>(&self,
293 hasher: &mut StableHasher<W>) {
294 let (ref _0, ref _1, ref _2, ref _3) = *self;
295 _0.hash_stable(ctx, hasher);
296 _1.hash_stable(ctx, hasher);
297 _2.hash_stable(ctx, hasher);
298 _3.hash_stable(ctx, hasher);
302 impl<T: HashStable<CTX>, CTX> HashStable<CTX> for [T] {
303 default fn hash_stable<W: StableHasherResult>(&self,
305 hasher: &mut StableHasher<W>) {
306 self.len().hash_stable(ctx, hasher);
308 item.hash_stable(ctx, hasher);
313 impl<T: HashStable<CTX>, CTX> HashStable<CTX> for Vec<T> {
315 fn hash_stable<W: StableHasherResult>(&self,
317 hasher: &mut StableHasher<W>) {
318 (&self[..]).hash_stable(ctx, hasher);
322 impl<A, CTX> HashStable<CTX> for SmallVec<[A; 1]> where A: HashStable<CTX> {
324 fn hash_stable<W: StableHasherResult>(&self,
326 hasher: &mut StableHasher<W>) {
328 item.hash_stable(ctx, hasher);
333 impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for Box<T> {
335 fn hash_stable<W: StableHasherResult>(&self,
337 hasher: &mut StableHasher<W>) {
338 (**self).hash_stable(ctx, hasher);
342 impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for ::std::rc::Rc<T> {
344 fn hash_stable<W: StableHasherResult>(&self,
346 hasher: &mut StableHasher<W>) {
347 (**self).hash_stable(ctx, hasher);
351 impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for ::std::sync::Arc<T> {
353 fn hash_stable<W: StableHasherResult>(&self,
355 hasher: &mut StableHasher<W>) {
356 (**self).hash_stable(ctx, hasher);
360 impl<CTX> HashStable<CTX> for str {
362 fn hash_stable<W: StableHasherResult>(&self,
364 hasher: &mut StableHasher<W>) {
365 self.len().hash(hasher);
366 self.as_bytes().hash(hasher);
371 impl<CTX> HashStable<CTX> for String {
373 fn hash_stable<W: StableHasherResult>(&self,
375 hasher: &mut StableHasher<W>) {
376 (&self[..]).hash_stable(hcx, hasher);
380 impl<HCX> ToStableHashKey<HCX> for String {
381 type KeyType = String;
383 fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType {
388 impl<CTX> HashStable<CTX> for bool {
390 fn hash_stable<W: StableHasherResult>(&self,
392 hasher: &mut StableHasher<W>) {
393 (if *self { 1u8 } else { 0u8 }).hash_stable(ctx, hasher);
398 impl<T, CTX> HashStable<CTX> for Option<T>
399 where T: HashStable<CTX>
402 fn hash_stable<W: StableHasherResult>(&self,
404 hasher: &mut StableHasher<W>) {
405 if let Some(ref value) = *self {
406 1u8.hash_stable(ctx, hasher);
407 value.hash_stable(ctx, hasher);
409 0u8.hash_stable(ctx, hasher);
414 impl<T1, T2, CTX> HashStable<CTX> for Result<T1, T2>
415 where T1: HashStable<CTX>,
419 fn hash_stable<W: StableHasherResult>(&self,
421 hasher: &mut StableHasher<W>) {
422 mem::discriminant(self).hash_stable(ctx, hasher);
424 Ok(ref x) => x.hash_stable(ctx, hasher),
425 Err(ref x) => x.hash_stable(ctx, hasher),
430 impl<'a, T, CTX> HashStable<CTX> for &'a T
431 where T: HashStable<CTX> + ?Sized
434 fn hash_stable<W: StableHasherResult>(&self,
436 hasher: &mut StableHasher<W>) {
437 (**self).hash_stable(ctx, hasher);
441 impl<T, CTX> HashStable<CTX> for ::std::mem::Discriminant<T> {
443 fn hash_stable<W: StableHasherResult>(&self,
445 hasher: &mut StableHasher<W>) {
446 ::std::hash::Hash::hash(self, hasher);
450 impl<I: indexed_vec::Idx, T, CTX> HashStable<CTX> for indexed_vec::IndexVec<I, T>
451 where T: HashStable<CTX>,
453 fn hash_stable<W: StableHasherResult>(&self,
455 hasher: &mut StableHasher<W>) {
456 self.len().hash_stable(ctx, hasher);
458 v.hash_stable(ctx, hasher);
464 impl<I: indexed_vec::Idx, CTX> HashStable<CTX> for bit_set::BitSet<I>
466 fn hash_stable<W: StableHasherResult>(&self,
468 hasher: &mut StableHasher<W>) {
469 self.words().hash_stable(ctx, hasher);
473 impl_stable_hash_via_hash!(::std::path::Path);
474 impl_stable_hash_via_hash!(::std::path::PathBuf);
476 impl<K, V, R, HCX> HashStable<HCX> for ::std::collections::HashMap<K, V, R>
477 where K: ToStableHashKey<HCX> + Eq + Hash,
482 fn hash_stable<W: StableHasherResult>(&self,
484 hasher: &mut StableHasher<W>) {
485 hash_stable_hashmap(hcx, hasher, self, ToStableHashKey::to_stable_hash_key);
489 impl<K, R, HCX> HashStable<HCX> for ::std::collections::HashSet<K, R>
490 where K: ToStableHashKey<HCX> + Eq + Hash,
493 fn hash_stable<W: StableHasherResult>(&self,
495 hasher: &mut StableHasher<W>) {
496 let mut keys: Vec<_> = self.iter()
497 .map(|k| k.to_stable_hash_key(hcx))
499 keys.sort_unstable();
500 keys.hash_stable(hcx, hasher);
504 impl<K, V, HCX> HashStable<HCX> for ::std::collections::BTreeMap<K, V>
505 where K: ToStableHashKey<HCX>,
508 fn hash_stable<W: StableHasherResult>(&self,
510 hasher: &mut StableHasher<W>) {
511 let mut entries: Vec<_> = self.iter()
512 .map(|(k, v)| (k.to_stable_hash_key(hcx), v))
514 entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2));
515 entries.hash_stable(hcx, hasher);
519 impl<K, HCX> HashStable<HCX> for ::std::collections::BTreeSet<K>
520 where K: ToStableHashKey<HCX>,
522 fn hash_stable<W: StableHasherResult>(&self,
524 hasher: &mut StableHasher<W>) {
525 let mut keys: Vec<_> = self.iter()
526 .map(|k| k.to_stable_hash_key(hcx))
528 keys.sort_unstable();
529 keys.hash_stable(hcx, hasher);
533 pub fn hash_stable_hashmap<HCX, K, V, R, SK, F, W>(
535 hasher: &mut StableHasher<W>,
536 map: &::std::collections::HashMap<K, V, R>,
537 to_stable_hash_key: F)
541 SK: HashStable<HCX> + Ord + Clone,
542 F: Fn(&K, &HCX) -> SK,
543 W: StableHasherResult,
545 let mut entries: Vec<_> = map.iter()
546 .map(|(k, v)| (to_stable_hash_key(k, hcx), v))
548 entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2));
549 entries.hash_stable(hcx, hasher);
553 /// A vector container that makes sure that its items are hashed in a stable
555 pub struct StableVec<T>(Vec<T>);
557 impl<T> StableVec<T> {
558 pub fn new(v: Vec<T>) -> Self {
563 impl<T> ::std::ops::Deref for StableVec<T> {
564 type Target = Vec<T>;
566 fn deref(&self) -> &Vec<T> {
571 impl<T, HCX> HashStable<HCX> for StableVec<T>
572 where T: HashStable<HCX> + ToStableHashKey<HCX>
574 fn hash_stable<W: StableHasherResult>(&self,
576 hasher: &mut StableHasher<W>) {
577 let StableVec(ref v) = *self;
579 let mut sorted: Vec<_> = v.iter()
580 .map(|x| x.to_stable_hash_key(hcx))
582 sorted.sort_unstable();
583 sorted.hash_stable(hcx, hasher);