7 use rustc_data_structures::{
8 fingerprint::Fingerprint,
9 stable_hasher::{HashStable, StableHasher},
12 use crate::{DebruijnIndex, TypeFlags};
14 /// A helper type that you can wrap round your own type in order to automatically
15 /// cache the stable hash, type flags and debruijn index on creation and
16 /// not recompute it whenever the information is needed.
17 /// This is only done in incremental mode. You can also opt out of caching by using
18 /// StableHash::ZERO for the hash, in which case the hash gets computed each time.
19 /// This is useful if you have values that you intern but never (can?) use for stable
21 #[derive(Copy, Clone)]
22 pub struct WithCachedTypeInfo<T> {
24 pub stable_hash: Fingerprint,
26 /// This field provides fast access to information that is also contained
29 /// This field shouldn't be used directly and may be removed in the future.
30 /// Use `Ty::flags()` instead.
33 /// This field provides fast access to information that is also contained
36 /// This is a kind of confusing thing: it stores the smallest
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.
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.
50 /// We call this concept an "exclusive" binder `D` because all
51 /// De Bruijn indices within the type are contained within `0..D`
53 pub outer_exclusive_binder: DebruijnIndex,
56 impl<T: PartialEq> PartialEq for WithCachedTypeInfo<T> {
58 fn eq(&self, other: &Self) -> bool {
59 self.internee.eq(&other.internee)
63 impl<T: Eq> Eq for WithCachedTypeInfo<T> {}
65 impl<T: Ord> PartialOrd for WithCachedTypeInfo<T> {
66 fn partial_cmp(&self, other: &WithCachedTypeInfo<T>) -> Option<Ordering> {
67 Some(self.internee.cmp(&other.internee))
71 impl<T: Ord> Ord for WithCachedTypeInfo<T> {
72 fn cmp(&self, other: &WithCachedTypeInfo<T>) -> Ordering {
73 self.internee.cmp(&other.internee)
77 impl<T> Deref for WithCachedTypeInfo<T> {
81 fn deref(&self) -> &T {
86 impl<T: Hash> Hash for WithCachedTypeInfo<T> {
88 fn hash<H: Hasher>(&self, s: &mut H) {
89 if self.stable_hash != Fingerprint::ZERO {
90 self.stable_hash.hash(s)
97 impl<T: HashStable<CTX>, CTX> HashStable<CTX> for WithCachedTypeInfo<T> {
98 fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
99 if self.stable_hash == Fingerprint::ZERO || cfg!(debug_assertions) {
100 // No cached hash available. This can only mean that incremental is disabled.
101 // We don't cache stable hashes in non-incremental mode, because they are used
102 // so rarely that the performance actually suffers.
104 // We need to build the hash as if we cached it and then hash that hash, as
105 // otherwise the hashes will differ between cached and non-cached mode.
106 let stable_hash: Fingerprint = {
107 let mut hasher = StableHasher::new();
108 self.internee.hash_stable(hcx, &mut hasher);
111 if cfg!(debug_assertions) && self.stable_hash != Fingerprint::ZERO {
113 stable_hash, self.stable_hash,
114 "cached stable hash does not match freshly computed stable hash"
117 stable_hash.hash_stable(hcx, hasher);
119 self.stable_hash.hash_stable(hcx, hasher);