Skip to main content

rustc_data_structures/
stable_hash.rs

1use std::hash::{BuildHasher, Hash, Hasher};
2use std::marker::PhantomData;
3use std::mem;
4use std::num::NonZero;
5
6use rustc_index::bit_set::{self, DenseBitSet};
7use rustc_index::{Idx, IndexSlice, IndexVec};
8use smallvec::SmallVec;
9use thin_vec::ThinVec;
10
11#[cfg(test)]
12mod tests;
13
14use rustc_hashes::{Hash64, Hash128};
15pub use rustc_stable_hash::{
16    FromStableHash, SipHasher128Hash as StableHasherHash, StableSipHasher128 as StableHasher,
17};
18
19/// This trait lets `StableHash` and `derive(StableHash)` be used in
20/// this crate (and other crates upstream of `rustc_middle`), while leaving
21/// certain operations to be defined in `rustc_middle` where more things are
22/// visible.
23pub trait StableHashCtxt {
24    /// The main event: stable hashing of a span.
25    fn stable_hash_span(&mut self, span: RawSpan, hasher: &mut StableHasher);
26
27    /// Compute a `DefPathHash`.
28    fn def_path_hash(&self, def_id: RawDefId) -> RawDefPathHash;
29
30    /// Get the stable hash controls.
31    fn stable_hash_controls(&self) -> StableHashControls;
32
33    /// Assert that the provided `StableHashCtxt` is configured with the default
34    /// `StableHashControls`. We should always have bailed out before getting to here with a
35    fn assert_default_stable_hash_controls(&self, msg: &str);
36}
37
38// A type used to work around `Span` not being visible in this crate. It is the same layout as
39// `Span`.
40pub struct RawSpan(pub u32, pub u16, pub u16);
41
42// A type used to work around `DefId` not being visible in this crate. It is the same size as
43// `DefId`.
44pub struct RawDefId(pub u32, pub u32);
45
46// A type used to work around `DefPathHash` not being visible in this crate. It is the same size as
47// `DefPathHash`.
48pub struct RawDefPathHash(pub [u8; 16]);
49
50/// Something that implements `StableHash` can be hashed in a way that is
51/// stable across multiple compilation sessions.
52///
53/// Note that `StableHash` imposes rather more strict requirements than usual
54/// hash functions:
55///
56/// - Stable hashes are sometimes used as identifiers. Therefore they must
57///   conform to the corresponding `PartialEq` implementations:
58///
59///     - `x == y` implies `stable_hash(x) == stable_hash(y)`, and
60///     - `x != y` implies `stable_hash(x) != stable_hash(y)`.
61///
62///   That second condition is usually not required for hash functions
63///   (e.g. `Hash`). In practice this means that `stable_hash` must feed any
64///   information into the hasher that a `PartialEq` comparison takes into
65///   account. See [#49300](https://github.com/rust-lang/rust/issues/49300)
66///   for an example where violating this invariant has caused trouble in the
67///   past.
68///
69/// - `stable_hash()` must be independent of the current
70///    compilation session. E.g. they must not hash memory addresses or other
71///    things that are "randomly" assigned per compilation session.
72///
73/// - `stable_hash()` must be independent of the host architecture. The
74///   `StableHasher` takes care of endianness and `isize`/`usize` platform
75///   differences.
76pub trait StableHash {
77    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher);
78}
79
80/// Implement this for types that can be turned into stable keys like, for
81/// example, for DefId that can be converted to a DefPathHash. This is used for
82/// bringing maps into a predictable order before hashing them.
83pub trait ToStableHashKey {
84    type KeyType: Ord + Sized + StableHash;
85    fn to_stable_hash_key<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx) -> Self::KeyType;
86}
87
88/// Trait for marking a type as having a sort order that is
89/// stable across compilation session boundaries. More formally:
90///
91/// ```txt
92/// Ord::cmp(a1, b1) == Ord::cmp(a2, b2)
93///    where a2 = decode(encode(a1, context1), context2)
94///          b2 = decode(encode(b1, context1), context2)
95/// ```
96///
97/// i.e. the result of `Ord::cmp` is not influenced by encoding
98/// the values in one session and then decoding them in another
99/// session.
100///
101/// This is trivially true for types where encoding and decoding
102/// don't change the bytes of the values that are used during
103/// comparison and comparison only depends on these bytes (as
104/// opposed to some non-local state). Examples are u32, String,
105/// Path, etc.
106///
107/// But it is not true for:
108///  - `*const T` and `*mut T` because the values of these pointers
109///    will change between sessions.
110///  - `DefIndex`, `CrateNum`, `LocalDefId`, because their concrete
111///    values depend on state that might be different between
112///    compilation sessions.
113///
114/// The associated constant `CAN_USE_UNSTABLE_SORT` denotes whether
115/// unstable sorting can be used for this type. Set to true if and
116/// only if `a == b` implies `a` and `b` are fully indistinguishable.
117pub trait StableOrd: Ord {
118    const CAN_USE_UNSTABLE_SORT: bool;
119
120    /// Marker to ensure that implementors have carefully considered
121    /// whether their `Ord` implementation obeys this trait's contract.
122    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: ();
123}
124
125impl<T: StableOrd> StableOrd for &T {
126    const CAN_USE_UNSTABLE_SORT: bool = T::CAN_USE_UNSTABLE_SORT;
127
128    // Ordering of a reference is exactly that of the referent, and since
129    // the ordering of the referet is stable so must be the ordering of the
130    // reference.
131    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
132}
133
134/// This is a companion trait to `StableOrd`. Some types like `Symbol` can be
135/// compared in a cross-session stable way, but their `Ord` implementation is
136/// not stable. In such cases, a `StableOrd` implementation can be provided
137/// to offer a lightweight way for stable sorting. (The more heavyweight option
138/// is to sort via `ToStableHashKey`, but then sorting needs to have access to
139/// a stable hashing context and `ToStableHashKey` can also be expensive as in
140/// the case of `Symbol` where it has to allocate a `String`.)
141///
142/// See the documentation of [StableOrd] for how stable sort order is defined.
143/// The same definition applies here. Be careful when implementing this trait.
144pub trait StableCompare {
145    const CAN_USE_UNSTABLE_SORT: bool;
146
147    fn stable_cmp(&self, other: &Self) -> std::cmp::Ordering;
148}
149
150/// `StableOrd` denotes that the type's `Ord` implementation is stable, so
151/// we can implement `StableCompare` by just delegating to `Ord`.
152impl<T: StableOrd> StableCompare for T {
153    const CAN_USE_UNSTABLE_SORT: bool = T::CAN_USE_UNSTABLE_SORT;
154
155    fn stable_cmp(&self, other: &Self) -> std::cmp::Ordering {
156        self.cmp(other)
157    }
158}
159
160/// Implement StableHash by just calling `Hash::hash()`. Also implement `StableOrd` for the type
161/// since that has the same requirements.
162///
163/// **WARNING** This is only valid for types that *really* don't need any context for fingerprinting.
164/// But it is easy to misuse this macro (see [#96013](https://github.com/rust-lang/rust/issues/96013)
165/// for examples). Therefore this macro is not exported and should only be used in the limited cases
166/// here in this module.
167///
168/// Use `#[derive(StableHash)]` instead.
169macro_rules! impl_stable_traits_for_trivial_type {
170    ($t:ty) => {
171        impl $crate::stable_hash::StableHash for $t {
172            #[inline]
173            fn stable_hash<Hcx>(
174                &self,
175                _: &mut Hcx,
176                hasher: &mut $crate::stable_hash::StableHasher,
177            ) {
178                ::std::hash::Hash::hash(self, hasher);
179            }
180        }
181
182        impl $crate::stable_hash::StableOrd for $t {
183            const CAN_USE_UNSTABLE_SORT: bool = true;
184
185            // Encoding and decoding doesn't change the bytes of trivial types
186            // and `Ord::cmp` depends only on those bytes.
187            const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
188        }
189    };
190}
191
192pub(crate) use impl_stable_traits_for_trivial_type;
193
194impl crate::stable_hash::StableHash for i8 {
    #[inline]
    fn stable_hash<Hcx>(&self, _: &mut Hcx,
        hasher: &mut crate::stable_hash::StableHasher) {
        ::std::hash::Hash::hash(self, hasher);
    }
}
impl crate::stable_hash::StableOrd for i8 {
    const CAN_USE_UNSTABLE_SORT: bool = true;
    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_trivial_type!(i8);
195impl crate::stable_hash::StableHash for i16 {
    #[inline]
    fn stable_hash<Hcx>(&self, _: &mut Hcx,
        hasher: &mut crate::stable_hash::StableHasher) {
        ::std::hash::Hash::hash(self, hasher);
    }
}
impl crate::stable_hash::StableOrd for i16 {
    const CAN_USE_UNSTABLE_SORT: bool = true;
    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_trivial_type!(i16);
196impl crate::stable_hash::StableHash for i32 {
    #[inline]
    fn stable_hash<Hcx>(&self, _: &mut Hcx,
        hasher: &mut crate::stable_hash::StableHasher) {
        ::std::hash::Hash::hash(self, hasher);
    }
}
impl crate::stable_hash::StableOrd for i32 {
    const CAN_USE_UNSTABLE_SORT: bool = true;
    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_trivial_type!(i32);
197impl crate::stable_hash::StableHash for i64 {
    #[inline]
    fn stable_hash<Hcx>(&self, _: &mut Hcx,
        hasher: &mut crate::stable_hash::StableHasher) {
        ::std::hash::Hash::hash(self, hasher);
    }
}
impl crate::stable_hash::StableOrd for i64 {
    const CAN_USE_UNSTABLE_SORT: bool = true;
    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_trivial_type!(i64);
198impl crate::stable_hash::StableHash for isize {
    #[inline]
    fn stable_hash<Hcx>(&self, _: &mut Hcx,
        hasher: &mut crate::stable_hash::StableHasher) {
        ::std::hash::Hash::hash(self, hasher);
    }
}
impl crate::stable_hash::StableOrd for isize {
    const CAN_USE_UNSTABLE_SORT: bool = true;
    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_trivial_type!(isize);
199
200impl crate::stable_hash::StableHash for u8 {
    #[inline]
    fn stable_hash<Hcx>(&self, _: &mut Hcx,
        hasher: &mut crate::stable_hash::StableHasher) {
        ::std::hash::Hash::hash(self, hasher);
    }
}
impl crate::stable_hash::StableOrd for u8 {
    const CAN_USE_UNSTABLE_SORT: bool = true;
    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_trivial_type!(u8);
201impl crate::stable_hash::StableHash for u16 {
    #[inline]
    fn stable_hash<Hcx>(&self, _: &mut Hcx,
        hasher: &mut crate::stable_hash::StableHasher) {
        ::std::hash::Hash::hash(self, hasher);
    }
}
impl crate::stable_hash::StableOrd for u16 {
    const CAN_USE_UNSTABLE_SORT: bool = true;
    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_trivial_type!(u16);
202impl crate::stable_hash::StableHash for u32 {
    #[inline]
    fn stable_hash<Hcx>(&self, _: &mut Hcx,
        hasher: &mut crate::stable_hash::StableHasher) {
        ::std::hash::Hash::hash(self, hasher);
    }
}
impl crate::stable_hash::StableOrd for u32 {
    const CAN_USE_UNSTABLE_SORT: bool = true;
    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_trivial_type!(u32);
203impl crate::stable_hash::StableHash for u64 {
    #[inline]
    fn stable_hash<Hcx>(&self, _: &mut Hcx,
        hasher: &mut crate::stable_hash::StableHasher) {
        ::std::hash::Hash::hash(self, hasher);
    }
}
impl crate::stable_hash::StableOrd for u64 {
    const CAN_USE_UNSTABLE_SORT: bool = true;
    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_trivial_type!(u64);
204impl crate::stable_hash::StableHash for usize {
    #[inline]
    fn stable_hash<Hcx>(&self, _: &mut Hcx,
        hasher: &mut crate::stable_hash::StableHasher) {
        ::std::hash::Hash::hash(self, hasher);
    }
}
impl crate::stable_hash::StableOrd for usize {
    const CAN_USE_UNSTABLE_SORT: bool = true;
    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_trivial_type!(usize);
205
206impl crate::stable_hash::StableHash for u128 {
    #[inline]
    fn stable_hash<Hcx>(&self, _: &mut Hcx,
        hasher: &mut crate::stable_hash::StableHasher) {
        ::std::hash::Hash::hash(self, hasher);
    }
}
impl crate::stable_hash::StableOrd for u128 {
    const CAN_USE_UNSTABLE_SORT: bool = true;
    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_trivial_type!(u128);
207impl crate::stable_hash::StableHash for i128 {
    #[inline]
    fn stable_hash<Hcx>(&self, _: &mut Hcx,
        hasher: &mut crate::stable_hash::StableHasher) {
        ::std::hash::Hash::hash(self, hasher);
    }
}
impl crate::stable_hash::StableOrd for i128 {
    const CAN_USE_UNSTABLE_SORT: bool = true;
    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_trivial_type!(i128);
208
209impl crate::stable_hash::StableHash for char {
    #[inline]
    fn stable_hash<Hcx>(&self, _: &mut Hcx,
        hasher: &mut crate::stable_hash::StableHasher) {
        ::std::hash::Hash::hash(self, hasher);
    }
}
impl crate::stable_hash::StableOrd for char {
    const CAN_USE_UNSTABLE_SORT: bool = true;
    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_trivial_type!(char);
210impl crate::stable_hash::StableHash for () {
    #[inline]
    fn stable_hash<Hcx>(&self, _: &mut Hcx,
        hasher: &mut crate::stable_hash::StableHasher) {
        ::std::hash::Hash::hash(self, hasher);
    }
}
impl crate::stable_hash::StableOrd for () {
    const CAN_USE_UNSTABLE_SORT: bool = true;
    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_trivial_type!(());
211
212impl crate::stable_hash::StableHash for Hash64 {
    #[inline]
    fn stable_hash<Hcx>(&self, _: &mut Hcx,
        hasher: &mut crate::stable_hash::StableHasher) {
        ::std::hash::Hash::hash(self, hasher);
    }
}
impl crate::stable_hash::StableOrd for Hash64 {
    const CAN_USE_UNSTABLE_SORT: bool = true;
    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_trivial_type!(Hash64);
213
214// We need a custom impl as the default hash function will only hash half the bits. For stable
215// hashing we want to hash the full 128-bit hash.
216impl StableHash for Hash128 {
217    #[inline]
218    fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher) {
219        self.as_u128().hash(hasher);
220    }
221}
222
223impl StableOrd for Hash128 {
224    const CAN_USE_UNSTABLE_SORT: bool = true;
225
226    // Encoding and decoding doesn't change the bytes of `Hash128`
227    // and `Ord::cmp` depends only on those bytes.
228    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
229}
230
231impl StableHash for ! {
232    fn stable_hash<Hcx>(&self, _hcx: &mut Hcx, _hasher: &mut StableHasher) {
233        ::core::panicking::panic("internal error: entered unreachable code")unreachable!()
234    }
235}
236
237impl<T> StableHash for PhantomData<T> {
238    fn stable_hash<Hcx>(&self, _hcx: &mut Hcx, _hasher: &mut StableHasher) {}
239}
240
241impl<T: StableHash + std::num::ZeroablePrimitive> StableHash for NonZero<T> {
242    #[inline]
243    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
244        self.get().stable_hash(hcx, hasher)
245    }
246}
247
248impl StableHash for f32 {
249    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
250        let val: u32 = self.to_bits();
251        val.stable_hash(hcx, hasher);
252    }
253}
254
255impl StableHash for f64 {
256    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
257        let val: u64 = self.to_bits();
258        val.stable_hash(hcx, hasher);
259    }
260}
261
262impl StableHash for ::std::cmp::Ordering {
263    #[inline]
264    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
265        (*self as i8).stable_hash(hcx, hasher);
266    }
267}
268
269impl<T1: StableHash> StableHash for (T1,) {
270    #[inline]
271    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
272        let (ref _0,) = *self;
273        _0.stable_hash(hcx, hasher);
274    }
275}
276
277impl<T1: StableHash, T2: StableHash> StableHash for (T1, T2) {
278    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
279        let (ref _0, ref _1) = *self;
280        _0.stable_hash(hcx, hasher);
281        _1.stable_hash(hcx, hasher);
282    }
283}
284
285impl<T1: StableOrd, T2: StableOrd> StableOrd for (T1, T2) {
286    const CAN_USE_UNSTABLE_SORT: bool = T1::CAN_USE_UNSTABLE_SORT && T2::CAN_USE_UNSTABLE_SORT;
287
288    // Ordering of tuples is a pure function of their elements' ordering, and since
289    // the ordering of each element is stable so must be the ordering of the tuple.
290    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
291}
292
293impl<T1, T2, T3> StableHash for (T1, T2, T3)
294where
295    T1: StableHash,
296    T2: StableHash,
297    T3: StableHash,
298{
299    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
300        let (ref _0, ref _1, ref _2) = *self;
301        _0.stable_hash(hcx, hasher);
302        _1.stable_hash(hcx, hasher);
303        _2.stable_hash(hcx, hasher);
304    }
305}
306
307impl<T1: StableOrd, T2: StableOrd, T3: StableOrd> StableOrd for (T1, T2, T3) {
308    const CAN_USE_UNSTABLE_SORT: bool =
309        T1::CAN_USE_UNSTABLE_SORT && T2::CAN_USE_UNSTABLE_SORT && T3::CAN_USE_UNSTABLE_SORT;
310
311    // Ordering of tuples is a pure function of their elements' ordering, and since
312    // the ordering of each element is stable so must be the ordering of the tuple.
313    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
314}
315
316impl<T1, T2, T3, T4> StableHash for (T1, T2, T3, T4)
317where
318    T1: StableHash,
319    T2: StableHash,
320    T3: StableHash,
321    T4: StableHash,
322{
323    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
324        let (ref _0, ref _1, ref _2, ref _3) = *self;
325        _0.stable_hash(hcx, hasher);
326        _1.stable_hash(hcx, hasher);
327        _2.stable_hash(hcx, hasher);
328        _3.stable_hash(hcx, hasher);
329    }
330}
331
332impl<T1: StableOrd, T2: StableOrd, T3: StableOrd, T4: StableOrd> StableOrd for (T1, T2, T3, T4) {
333    const CAN_USE_UNSTABLE_SORT: bool = T1::CAN_USE_UNSTABLE_SORT
334        && T2::CAN_USE_UNSTABLE_SORT
335        && T3::CAN_USE_UNSTABLE_SORT
336        && T4::CAN_USE_UNSTABLE_SORT;
337
338    // Ordering of tuples is a pure function of their elements' ordering, and since
339    // the ordering of each element is stable so must be the ordering of the tuple.
340    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
341}
342
343impl<T: StableHash> StableHash for [T] {
344    default fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
345        self.len().stable_hash(hcx, hasher);
346        for item in self {
347            item.stable_hash(hcx, hasher);
348        }
349    }
350}
351
352impl StableHash for [u8] {
353    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
354        self.len().stable_hash(hcx, hasher);
355        hasher.write(self);
356    }
357}
358
359impl<T: StableHash> StableHash for Vec<T> {
360    #[inline]
361    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
362        self[..].stable_hash(hcx, hasher);
363    }
364}
365
366impl<K, V, R> StableHash for indexmap::IndexMap<K, V, R>
367where
368    K: StableHash + Eq + Hash,
369    V: StableHash,
370    R: BuildHasher,
371{
372    #[inline]
373    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
374        self.len().stable_hash(hcx, hasher);
375        for kv in self {
376            kv.stable_hash(hcx, hasher);
377        }
378    }
379}
380
381impl<K, R> StableHash for indexmap::IndexSet<K, R>
382where
383    K: StableHash + Eq + Hash,
384    R: BuildHasher,
385{
386    #[inline]
387    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
388        self.len().stable_hash(hcx, hasher);
389        for key in self {
390            key.stable_hash(hcx, hasher);
391        }
392    }
393}
394
395impl<A, const N: usize> StableHash for SmallVec<[A; N]>
396where
397    A: StableHash,
398{
399    #[inline]
400    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
401        self[..].stable_hash(hcx, hasher);
402    }
403}
404
405impl<T: StableHash> StableHash for ThinVec<T> {
406    #[inline]
407    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
408        self[..].stable_hash(hcx, hasher);
409    }
410}
411
412impl<T: ?Sized + StableHash> StableHash for Box<T> {
413    #[inline]
414    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
415        (**self).stable_hash(hcx, hasher);
416    }
417}
418
419impl<T: ?Sized + StableHash> StableHash for ::std::rc::Rc<T> {
420    #[inline]
421    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
422        (**self).stable_hash(hcx, hasher);
423    }
424}
425
426impl<T: ?Sized + StableHash> StableHash for ::std::sync::Arc<T> {
427    #[inline]
428    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
429        (**self).stable_hash(hcx, hasher);
430    }
431}
432
433impl StableHash for str {
434    #[inline]
435    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
436        self.as_bytes().stable_hash(hcx, hasher);
437    }
438}
439
440impl StableOrd for &str {
441    const CAN_USE_UNSTABLE_SORT: bool = true;
442
443    // Encoding and decoding doesn't change the bytes of string slices
444    // and `Ord::cmp` depends only on those bytes.
445    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
446}
447
448impl StableHash for String {
449    #[inline]
450    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
451        self[..].stable_hash(hcx, hasher);
452    }
453}
454
455impl StableOrd for String {
456    const CAN_USE_UNSTABLE_SORT: bool = true;
457
458    // String comparison only depends on their contents and the
459    // contents are not changed by (de-)serialization.
460    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
461}
462
463impl StableHash for bool {
464    #[inline]
465    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
466        (if *self { 1u8 } else { 0u8 }).stable_hash(hcx, hasher);
467    }
468}
469
470impl StableOrd for bool {
471    const CAN_USE_UNSTABLE_SORT: bool = true;
472
473    // sort order of bools is not changed by (de-)serialization.
474    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
475}
476
477impl<T> StableHash for Option<T>
478where
479    T: StableHash,
480{
481    #[inline]
482    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
483        if let Some(ref value) = *self {
484            1u8.stable_hash(hcx, hasher);
485            value.stable_hash(hcx, hasher);
486        } else {
487            0u8.stable_hash(hcx, hasher);
488        }
489    }
490}
491
492impl<T: StableOrd> StableOrd for Option<T> {
493    const CAN_USE_UNSTABLE_SORT: bool = T::CAN_USE_UNSTABLE_SORT;
494
495    // the Option wrapper does not add instability to comparison.
496    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
497}
498
499impl<T1, T2> StableHash for Result<T1, T2>
500where
501    T1: StableHash,
502    T2: StableHash,
503{
504    #[inline]
505    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
506        mem::discriminant(self).stable_hash(hcx, hasher);
507        match *self {
508            Ok(ref x) => x.stable_hash(hcx, hasher),
509            Err(ref x) => x.stable_hash(hcx, hasher),
510        }
511    }
512}
513
514impl<'a, T> StableHash for &'a T
515where
516    T: StableHash + ?Sized,
517{
518    #[inline]
519    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
520        (**self).stable_hash(hcx, hasher);
521    }
522}
523
524impl<T> StableHash for ::std::mem::Discriminant<T> {
525    #[inline]
526    fn stable_hash<Hcx: StableHashCtxt>(&self, _: &mut Hcx, hasher: &mut StableHasher) {
527        ::std::hash::Hash::hash(self, hasher);
528    }
529}
530
531impl<T> StableHash for ::std::range::RangeInclusive<T>
532where
533    T: StableHash,
534{
535    #[inline]
536    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
537        self.start.stable_hash(hcx, hasher);
538        self.last.stable_hash(hcx, hasher);
539    }
540}
541
542impl<I: Idx, T> StableHash for IndexSlice<I, T>
543where
544    T: StableHash,
545{
546    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
547        self.len().stable_hash(hcx, hasher);
548        for v in &self.raw {
549            v.stable_hash(hcx, hasher);
550        }
551    }
552}
553
554impl<I: Idx, T> StableHash for IndexVec<I, T>
555where
556    T: StableHash,
557{
558    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
559        self.len().stable_hash(hcx, hasher);
560        for v in &self.raw {
561            v.stable_hash(hcx, hasher);
562        }
563    }
564}
565
566impl<I: Idx> StableHash for DenseBitSet<I> {
567    fn stable_hash<Hcx: StableHashCtxt>(&self, _hcx: &mut Hcx, hasher: &mut StableHasher) {
568        ::std::hash::Hash::hash(self, hasher);
569    }
570}
571
572impl<R: Idx, C: Idx> StableHash for bit_set::BitMatrix<R, C> {
573    fn stable_hash<Hcx: StableHashCtxt>(&self, _hcx: &mut Hcx, hasher: &mut StableHasher) {
574        ::std::hash::Hash::hash(self, hasher);
575    }
576}
577
578impl crate::stable_hash::StableHash for ::std::ffi::OsStr {
    #[inline]
    fn stable_hash<Hcx>(&self, _: &mut Hcx,
        hasher: &mut crate::stable_hash::StableHasher) {
        ::std::hash::Hash::hash(self, hasher);
    }
}
impl crate::stable_hash::StableOrd for ::std::ffi::OsStr {
    const CAN_USE_UNSTABLE_SORT: bool = true;
    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_trivial_type!(::std::ffi::OsStr);
579
580impl crate::stable_hash::StableHash for ::std::path::Path {
    #[inline]
    fn stable_hash<Hcx>(&self, _: &mut Hcx,
        hasher: &mut crate::stable_hash::StableHasher) {
        ::std::hash::Hash::hash(self, hasher);
    }
}
impl crate::stable_hash::StableOrd for ::std::path::Path {
    const CAN_USE_UNSTABLE_SORT: bool = true;
    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_trivial_type!(::std::path::Path);
581impl crate::stable_hash::StableHash for ::std::path::PathBuf {
    #[inline]
    fn stable_hash<Hcx>(&self, _: &mut Hcx,
        hasher: &mut crate::stable_hash::StableHasher) {
        ::std::hash::Hash::hash(self, hasher);
    }
}
impl crate::stable_hash::StableOrd for ::std::path::PathBuf {
    const CAN_USE_UNSTABLE_SORT: bool = true;
    const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_trivial_type!(::std::path::PathBuf);
582
583// It is not safe to implement StableHash for HashSet, HashMap or any other collection type
584// with unstable but observable iteration order.
585// See https://github.com/rust-lang/compiler-team/issues/533 for further information.
586impl<V> !StableHash for std::collections::HashSet<V> {}
587impl<K, V> !StableHash for std::collections::HashMap<K, V> {}
588
589impl<K, V> StableHash for ::std::collections::BTreeMap<K, V>
590where
591    K: StableHash + StableOrd,
592    V: StableHash,
593{
594    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
595        self.len().stable_hash(hcx, hasher);
596        for entry in self.iter() {
597            entry.stable_hash(hcx, hasher);
598        }
599    }
600}
601
602impl<K> StableHash for ::std::collections::BTreeSet<K>
603where
604    K: StableHash + StableOrd,
605{
606    fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
607        self.len().stable_hash(hcx, hasher);
608        for entry in self.iter() {
609            entry.stable_hash(hcx, hasher);
610        }
611    }
612}
613
614/// Controls what data we do or do not hash.
615/// Whenever a `StableHash` implementation caches its
616/// result, it needs to include `StableHashControls` as part
617/// of the key, to ensure that it does not produce an incorrect
618/// result (for example, using a `Fingerprint` produced while
619/// hashing `Span`s when a `Fingerprint` without `Span`s is
620/// being requested)
621#[derive(#[automatically_derived]
impl ::core::clone::Clone for StableHashControls {
    #[inline]
    fn clone(&self) -> StableHashControls {
        let _: ::core::clone::AssertParamIsClone<bool>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for StableHashControls { }Copy, #[automatically_derived]
impl ::core::hash::Hash for StableHashControls {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.hash_spans, state)
    }
}Hash, #[automatically_derived]
impl ::core::cmp::Eq for StableHashControls {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<bool>;
    }
}Eq, #[automatically_derived]
impl ::core::cmp::PartialEq for StableHashControls {
    #[inline]
    fn eq(&self, other: &StableHashControls) -> bool {
        self.hash_spans == other.hash_spans
    }
}PartialEq, #[automatically_derived]
impl ::core::fmt::Debug for StableHashControls {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field1_finish(f,
            "StableHashControls", "hash_spans", &&self.hash_spans)
    }
}Debug)]
622pub struct StableHashControls {
623    pub hash_spans: bool,
624}