rustc_type_ir/
ty_info.rs

1use std::cmp::Ordering;
2use std::hash::{Hash, Hasher};
3use std::ops::Deref;
4
5#[cfg(feature = "nightly")]
6use rustc_data_structures::fingerprint::Fingerprint;
7#[cfg(feature = "nightly")]
8use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
9
10use crate::{DebruijnIndex, TypeFlags};
11
12/// A helper type that you can wrap round your own type in order to automatically
13/// cache the stable hash, type flags and debruijn index on creation and
14/// not recompute it whenever the information is needed.
15/// This is only done in incremental mode. You can also opt out of caching by using
16/// StableHash::ZERO for the hash, in which case the hash gets computed each time.
17/// This is useful if you have values that you intern but never (can?) use for stable
18/// hashing.
19#[derive(Copy, Clone)]
20pub struct WithCachedTypeInfo<T> {
21    pub internee: T,
22
23    #[cfg(feature = "nightly")]
24    pub stable_hash: Fingerprint,
25
26    /// This field provides fast access to information that is also contained
27    /// in `kind`.
28    ///
29    /// This field shouldn't be used directly and may be removed in the future.
30    /// Use `Ty::flags()` instead.
31    pub flags: TypeFlags,
32
33    /// This field provides fast access to information that is also contained
34    /// in `kind`.
35    ///
36    /// This is a kind of confusing thing: it stores the smallest
37    /// binder such that
38    ///
39    /// (a) the binder itself captures nothing but
40    /// (b) all the late-bound things within the type are captured
41    ///     by some sub-binder.
42    ///
43    /// So, for a type without any late-bound things, like `u32`, this
44    /// will be *innermost*, because that is the innermost binder that
45    /// captures nothing. But for a type `&'D u32`, where `'D` is a
46    /// late-bound region with De Bruijn index `D`, this would be `D + 1`
47    /// -- the binder itself does not capture `D`, but `D` is captured
48    /// by an inner binder.
49    ///
50    /// We call this concept an "exclusive" binder `D` because all
51    /// De Bruijn indices within the type are contained within `0..D`
52    /// (exclusive).
53    pub outer_exclusive_binder: DebruijnIndex,
54}
55
56impl<T: PartialEq> PartialEq for WithCachedTypeInfo<T> {
57    #[inline]
58    fn eq(&self, other: &Self) -> bool {
59        self.internee.eq(&other.internee)
60    }
61}
62
63impl<T: Eq> Eq for WithCachedTypeInfo<T> {}
64
65impl<T: Ord> PartialOrd for WithCachedTypeInfo<T> {
66    fn partial_cmp(&self, other: &WithCachedTypeInfo<T>) -> Option<Ordering> {
67        Some(self.internee.cmp(&other.internee))
68    }
69}
70
71impl<T: Ord> Ord for WithCachedTypeInfo<T> {
72    fn cmp(&self, other: &WithCachedTypeInfo<T>) -> Ordering {
73        self.internee.cmp(&other.internee)
74    }
75}
76
77impl<T> Deref for WithCachedTypeInfo<T> {
78    type Target = T;
79
80    #[inline]
81    fn deref(&self) -> &T {
82        &self.internee
83    }
84}
85
86impl<T: Hash> Hash for WithCachedTypeInfo<T> {
87    #[inline]
88    fn hash<H: Hasher>(&self, s: &mut H) {
89        #[cfg(feature = "nightly")]
90        if self.stable_hash != Fingerprint::ZERO {
91            return self.stable_hash.hash(s);
92        }
93
94        self.internee.hash(s)
95    }
96}
97
98#[cfg(feature = "nightly")]
99impl<T: HashStable<CTX>, CTX> HashStable<CTX> for WithCachedTypeInfo<T> {
100    fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
101        if self.stable_hash == Fingerprint::ZERO || cfg!(debug_assertions) {
102            // No cached hash available. This can only mean that incremental is disabled.
103            // We don't cache stable hashes in non-incremental mode, because they are used
104            // so rarely that the performance actually suffers.
105
106            // We need to build the hash as if we cached it and then hash that hash, as
107            // otherwise the hashes will differ between cached and non-cached mode.
108            let stable_hash: Fingerprint = {
109                let mut hasher = StableHasher::new();
110                self.internee.hash_stable(hcx, &mut hasher);
111                hasher.finish()
112            };
113            if cfg!(debug_assertions) && self.stable_hash != Fingerprint::ZERO {
114                assert_eq!(
115                    stable_hash, self.stable_hash,
116                    "cached stable hash does not match freshly computed stable hash"
117                );
118            }
119            stable_hash.hash_stable(hcx, hasher);
120        } else {
121            self.stable_hash.hash_stable(hcx, hasher);
122        }
123    }
124}