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> {
18 width: PhantomData<W>,
21 impl<W: StableHasherResult> ::std::fmt::Debug for StableHasher<W> {
22 fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
23 write!(f, "{:?}", self.state)
27 pub trait StableHasherResult: Sized {
28 fn finish(hasher: StableHasher<Self>) -> Self;
31 impl<W: StableHasherResult> StableHasher<W> {
32 pub fn new() -> Self {
34 state: SipHasher128::new_with_keys(0, 0),
39 pub fn finish(self) -> W {
44 impl StableHasherResult for u128 {
45 fn finish(hasher: StableHasher<Self>) -> Self {
46 let (_0, _1) = hasher.finalize();
47 (_0 as u128) | ((_1 as u128) << 64)
51 impl StableHasherResult for u64 {
52 fn finish(hasher: StableHasher<Self>) -> Self {
57 impl<W> StableHasher<W> {
59 pub fn finalize(self) -> (u64, u64) {
60 self.state.finish128()
64 impl<W> Hasher for StableHasher<W> {
65 fn finish(&self) -> u64 {
66 panic!("use StableHasher::finalize instead");
70 fn write(&mut self, bytes: &[u8]) {
71 self.state.write(bytes);
75 fn write_u8(&mut self, i: u8) {
76 self.state.write_u8(i);
80 fn write_u16(&mut self, i: u16) {
81 self.state.write_u16(i.to_le());
85 fn write_u32(&mut self, i: u32) {
86 self.state.write_u32(i.to_le());
90 fn write_u64(&mut self, i: u64) {
91 self.state.write_u64(i.to_le());
95 fn write_u128(&mut self, i: u128) {
96 self.state.write_u128(i.to_le());
100 fn write_usize(&mut self, i: usize) {
101 // Always treat usize as u64 so we get the same results on 32 and 64 bit
102 // platforms. This is important for symbol hashes when cross compiling,
104 self.state.write_u64((i as u64).to_le());
108 fn write_i8(&mut self, i: i8) {
109 self.state.write_i8(i);
113 fn write_i16(&mut self, i: i16) {
114 self.state.write_i16(i.to_le());
118 fn write_i32(&mut self, i: i32) {
119 self.state.write_i32(i.to_le());
123 fn write_i64(&mut self, i: i64) {
124 self.state.write_i64(i.to_le());
128 fn write_i128(&mut self, i: i128) {
129 self.state.write_i128(i.to_le());
133 fn write_isize(&mut self, i: isize) {
134 // Always treat isize as i64 so we get the same results on 32 and 64 bit
135 // platforms. This is important for symbol hashes when cross compiling,
137 self.state.write_i64((i as i64).to_le());
141 /// Something that implements `HashStable<CTX>` can be hashed in a way that is
142 /// stable across multiple compilation sessions.
144 /// Note that `HashStable` imposes rather more strict requirements than usual
147 /// - Stable hashes are sometimes used as identifiers. Therefore they must
148 /// conform to the corresponding `PartialEq` implementations:
150 /// - `x == y` implies `hash_stable(x) == hash_stable(y)`, and
151 /// - `x != y` implies `hash_stable(x) != hash_stable(y)`.
153 /// That second condition is usually not required for hash functions
154 /// (e.g. `Hash`). In practice this means that `hash_stable` must feed any
155 /// information into the hasher that a `PartialEq` comparision takes into
156 /// account. See [#49300](https://github.com/rust-lang/rust/issues/49300)
157 /// for an example where violating this invariant has caused trouble in the
160 /// - `hash_stable()` must be independent of the current
161 /// compilation session. E.g. they must not hash memory addresses or other
162 /// things that are "randomly" assigned per compilation session.
164 /// - `hash_stable()` must be independent of the host architecture. The
165 /// `StableHasher` takes care of endianness and `isize`/`usize` platform
167 pub trait HashStable<CTX> {
168 fn hash_stable<W: StableHasherResult>(&self,
170 hasher: &mut StableHasher<W>);
173 /// Implement this for types that can be turned into stable keys like, for
174 /// example, for DefId that can be converted to a DefPathHash. This is used for
175 /// bringing maps into a predictable order before hashing them.
176 pub trait ToStableHashKey<HCX> {
177 type KeyType: Ord + Clone + Sized + HashStable<HCX>;
178 fn to_stable_hash_key(&self, hcx: &HCX) -> Self::KeyType;
181 // Implement HashStable by just calling `Hash::hash()`. This works fine for
182 // self-contained values that don't depend on the hashing context `CTX`.
184 macro_rules! impl_stable_hash_via_hash {
186 impl<CTX> $crate::stable_hasher::HashStable<CTX> for $t {
188 fn hash_stable<W: $crate::stable_hasher::StableHasherResult>(
191 hasher: &mut $crate::stable_hasher::StableHasher<W>
193 ::std::hash::Hash::hash(self, hasher);
199 impl_stable_hash_via_hash!(i8);
200 impl_stable_hash_via_hash!(i16);
201 impl_stable_hash_via_hash!(i32);
202 impl_stable_hash_via_hash!(i64);
203 impl_stable_hash_via_hash!(isize);
205 impl_stable_hash_via_hash!(u8);
206 impl_stable_hash_via_hash!(u16);
207 impl_stable_hash_via_hash!(u32);
208 impl_stable_hash_via_hash!(u64);
209 impl_stable_hash_via_hash!(usize);
211 impl_stable_hash_via_hash!(u128);
212 impl_stable_hash_via_hash!(i128);
214 impl_stable_hash_via_hash!(char);
215 impl_stable_hash_via_hash!(());
217 impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
218 fn hash_stable<W: StableHasherResult>(&self,
220 hasher: &mut StableHasher<W>) {
221 self.get().hash_stable(ctx, hasher)
225 impl<CTX> HashStable<CTX> for f32 {
226 fn hash_stable<W: StableHasherResult>(&self,
228 hasher: &mut StableHasher<W>) {
229 let val: u32 = unsafe {
230 ::std::mem::transmute(*self)
232 val.hash_stable(ctx, hasher);
236 impl<CTX> HashStable<CTX> for f64 {
237 fn hash_stable<W: StableHasherResult>(&self,
239 hasher: &mut StableHasher<W>) {
240 let val: u64 = unsafe {
241 ::std::mem::transmute(*self)
243 val.hash_stable(ctx, hasher);
247 impl<CTX> HashStable<CTX> for ::std::cmp::Ordering {
248 fn hash_stable<W: StableHasherResult>(&self,
250 hasher: &mut StableHasher<W>) {
251 (*self as i8).hash_stable(ctx, hasher);
255 impl<T1: HashStable<CTX>, CTX> HashStable<CTX> for (T1,) {
256 fn hash_stable<W: StableHasherResult>(&self,
258 hasher: &mut StableHasher<W>) {
259 let (ref _0,) = *self;
260 _0.hash_stable(ctx, hasher);
264 impl<T1: HashStable<CTX>, T2: HashStable<CTX>, CTX> HashStable<CTX> for (T1, T2) {
265 fn hash_stable<W: StableHasherResult>(&self,
267 hasher: &mut StableHasher<W>) {
268 let (ref _0, ref _1) = *self;
269 _0.hash_stable(ctx, hasher);
270 _1.hash_stable(ctx, hasher);
274 impl<T1, T2, T3, CTX> HashStable<CTX> for (T1, T2, T3)
275 where T1: HashStable<CTX>,
279 fn hash_stable<W: StableHasherResult>(&self,
281 hasher: &mut StableHasher<W>) {
282 let (ref _0, ref _1, ref _2) = *self;
283 _0.hash_stable(ctx, hasher);
284 _1.hash_stable(ctx, hasher);
285 _2.hash_stable(ctx, hasher);
289 impl<T1, T2, T3, T4, CTX> HashStable<CTX> for (T1, T2, T3, T4)
290 where T1: HashStable<CTX>,
295 fn hash_stable<W: StableHasherResult>(&self,
297 hasher: &mut StableHasher<W>) {
298 let (ref _0, ref _1, ref _2, ref _3) = *self;
299 _0.hash_stable(ctx, hasher);
300 _1.hash_stable(ctx, hasher);
301 _2.hash_stable(ctx, hasher);
302 _3.hash_stable(ctx, hasher);
306 impl<T: HashStable<CTX>, CTX> HashStable<CTX> for [T] {
307 default fn hash_stable<W: StableHasherResult>(&self,
309 hasher: &mut StableHasher<W>) {
310 self.len().hash_stable(ctx, hasher);
312 item.hash_stable(ctx, hasher);
317 impl<T: HashStable<CTX>, CTX> HashStable<CTX> for Vec<T> {
319 fn hash_stable<W: StableHasherResult>(&self,
321 hasher: &mut StableHasher<W>) {
322 (&self[..]).hash_stable(ctx, hasher);
326 impl<A, CTX> HashStable<CTX> for SmallVec<[A; 1]> where A: HashStable<CTX> {
328 fn hash_stable<W: StableHasherResult>(&self,
330 hasher: &mut StableHasher<W>) {
331 (&self[..]).hash_stable(ctx, hasher);
335 impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for Box<T> {
337 fn hash_stable<W: StableHasherResult>(&self,
339 hasher: &mut StableHasher<W>) {
340 (**self).hash_stable(ctx, hasher);
344 impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for ::std::rc::Rc<T> {
346 fn hash_stable<W: StableHasherResult>(&self,
348 hasher: &mut StableHasher<W>) {
349 (**self).hash_stable(ctx, hasher);
353 impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for ::std::sync::Arc<T> {
355 fn hash_stable<W: StableHasherResult>(&self,
357 hasher: &mut StableHasher<W>) {
358 (**self).hash_stable(ctx, hasher);
362 impl<CTX> HashStable<CTX> for str {
364 fn hash_stable<W: StableHasherResult>(&self,
366 hasher: &mut StableHasher<W>) {
367 self.len().hash(hasher);
368 self.as_bytes().hash(hasher);
373 impl<CTX> HashStable<CTX> for String {
375 fn hash_stable<W: StableHasherResult>(&self,
377 hasher: &mut StableHasher<W>) {
378 (&self[..]).hash_stable(hcx, hasher);
382 impl<HCX> ToStableHashKey<HCX> for String {
383 type KeyType = String;
385 fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType {
390 impl<CTX> HashStable<CTX> for bool {
392 fn hash_stable<W: StableHasherResult>(&self,
394 hasher: &mut StableHasher<W>) {
395 (if *self { 1u8 } else { 0u8 }).hash_stable(ctx, hasher);
400 impl<T, CTX> HashStable<CTX> for Option<T>
401 where T: HashStable<CTX>
404 fn hash_stable<W: StableHasherResult>(&self,
406 hasher: &mut StableHasher<W>) {
407 if let Some(ref value) = *self {
408 1u8.hash_stable(ctx, hasher);
409 value.hash_stable(ctx, hasher);
411 0u8.hash_stable(ctx, hasher);
416 impl<T1, T2, CTX> HashStable<CTX> for Result<T1, T2>
417 where T1: HashStable<CTX>,
421 fn hash_stable<W: StableHasherResult>(&self,
423 hasher: &mut StableHasher<W>) {
424 mem::discriminant(self).hash_stable(ctx, hasher);
426 Ok(ref x) => x.hash_stable(ctx, hasher),
427 Err(ref x) => x.hash_stable(ctx, hasher),
432 impl<'a, T, CTX> HashStable<CTX> for &'a T
433 where T: HashStable<CTX> + ?Sized
436 fn hash_stable<W: StableHasherResult>(&self,
438 hasher: &mut StableHasher<W>) {
439 (**self).hash_stable(ctx, hasher);
443 impl<T, CTX> HashStable<CTX> for ::std::mem::Discriminant<T> {
445 fn hash_stable<W: StableHasherResult>(&self,
447 hasher: &mut StableHasher<W>) {
448 ::std::hash::Hash::hash(self, hasher);
452 impl<I: indexed_vec::Idx, T, CTX> HashStable<CTX> for indexed_vec::IndexVec<I, T>
453 where T: HashStable<CTX>,
455 fn hash_stable<W: StableHasherResult>(&self,
457 hasher: &mut StableHasher<W>) {
458 self.len().hash_stable(ctx, hasher);
460 v.hash_stable(ctx, hasher);
466 impl<I: indexed_vec::Idx, CTX> HashStable<CTX> for bit_set::BitSet<I>
468 fn hash_stable<W: StableHasherResult>(&self,
470 hasher: &mut StableHasher<W>) {
471 self.words().hash_stable(ctx, hasher);
475 impl_stable_hash_via_hash!(::std::path::Path);
476 impl_stable_hash_via_hash!(::std::path::PathBuf);
478 impl<K, V, R, HCX> HashStable<HCX> for ::std::collections::HashMap<K, V, R>
479 where K: ToStableHashKey<HCX> + Eq + Hash,
484 fn hash_stable<W: StableHasherResult>(&self,
486 hasher: &mut StableHasher<W>) {
487 hash_stable_hashmap(hcx, hasher, self, ToStableHashKey::to_stable_hash_key);
491 impl<K, R, HCX> HashStable<HCX> for ::std::collections::HashSet<K, R>
492 where K: ToStableHashKey<HCX> + Eq + Hash,
495 fn hash_stable<W: StableHasherResult>(&self,
497 hasher: &mut StableHasher<W>) {
498 let mut keys: Vec<_> = self.iter()
499 .map(|k| k.to_stable_hash_key(hcx))
501 keys.sort_unstable();
502 keys.hash_stable(hcx, hasher);
506 impl<K, V, HCX> HashStable<HCX> for ::std::collections::BTreeMap<K, V>
507 where K: ToStableHashKey<HCX>,
510 fn hash_stable<W: StableHasherResult>(&self,
512 hasher: &mut StableHasher<W>) {
513 let mut entries: Vec<_> = self.iter()
514 .map(|(k, v)| (k.to_stable_hash_key(hcx), v))
516 entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2));
517 entries.hash_stable(hcx, hasher);
521 impl<K, HCX> HashStable<HCX> for ::std::collections::BTreeSet<K>
522 where K: ToStableHashKey<HCX>,
524 fn hash_stable<W: StableHasherResult>(&self,
526 hasher: &mut StableHasher<W>) {
527 let mut keys: Vec<_> = self.iter()
528 .map(|k| k.to_stable_hash_key(hcx))
530 keys.sort_unstable();
531 keys.hash_stable(hcx, hasher);
535 pub fn hash_stable_hashmap<HCX, K, V, R, SK, F, W>(
537 hasher: &mut StableHasher<W>,
538 map: &::std::collections::HashMap<K, V, R>,
539 to_stable_hash_key: F)
543 SK: HashStable<HCX> + Ord + Clone,
544 F: Fn(&K, &HCX) -> SK,
545 W: StableHasherResult,
547 let mut entries: Vec<_> = map.iter()
548 .map(|(k, v)| (to_stable_hash_key(k, hcx), v))
550 entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2));
551 entries.hash_stable(hcx, hasher);
555 /// A vector container that makes sure that its items are hashed in a stable
557 pub struct StableVec<T>(Vec<T>);
559 impl<T> StableVec<T> {
560 pub fn new(v: Vec<T>) -> Self {
565 impl<T> ::std::ops::Deref for StableVec<T> {
566 type Target = Vec<T>;
568 fn deref(&self) -> &Vec<T> {
573 impl<T, HCX> HashStable<HCX> for StableVec<T>
574 where T: HashStable<HCX> + ToStableHashKey<HCX>
576 fn hash_stable<W: StableHasherResult>(&self,
578 hasher: &mut StableHasher<W>) {
579 let StableVec(ref v) = *self;
581 let mut sorted: Vec<_> = v.iter()
582 .map(|x| x.to_stable_hash_key(hcx))
584 sorted.sort_unstable();
585 sorted.hash_stable(hcx, hasher);