]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_type_ir/src/ty_info.rs
Move FFI attribute validation to `check_attr`
[rust.git] / compiler / rustc_type_ir / src / ty_info.rs
1 use std::{
2     cmp::Ordering,
3     hash::{Hash, Hasher},
4     ops::Deref,
5 };
6
7 use rustc_data_structures::{
8     fingerprint::Fingerprint,
9     stable_hasher::{HashStable, StableHasher},
10 };
11
12 use crate::{DebruijnIndex, TypeFlags};
13
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
20 /// hashing.
21 #[derive(Copy, Clone)]
22 pub struct WithCachedTypeInfo<T> {
23     pub internee: T,
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
56 impl<T: PartialEq> PartialEq for WithCachedTypeInfo<T> {
57     #[inline]
58     fn eq(&self, other: &Self) -> bool {
59         self.internee.eq(&other.internee)
60     }
61 }
62
63 impl<T: Eq> Eq for WithCachedTypeInfo<T> {}
64
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))
68     }
69 }
70
71 impl<T: Ord> Ord for WithCachedTypeInfo<T> {
72     fn cmp(&self, other: &WithCachedTypeInfo<T>) -> Ordering {
73         self.internee.cmp(&other.internee)
74     }
75 }
76
77 impl<T> Deref for WithCachedTypeInfo<T> {
78     type Target = T;
79
80     #[inline]
81     fn deref(&self) -> &T {
82         &self.internee
83     }
84 }
85
86 impl<T: Hash> Hash for WithCachedTypeInfo<T> {
87     #[inline]
88     fn hash<H: Hasher>(&self, s: &mut H) {
89         if self.stable_hash != Fingerprint::ZERO {
90             self.stable_hash.hash(s)
91         } else {
92             self.internee.hash(s)
93         }
94     }
95 }
96
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.
103
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);
109                 hasher.finish()
110             };
111             if cfg!(debug_assertions) && self.stable_hash != Fingerprint::ZERO {
112                 assert_eq!(
113                     stable_hash, self.stable_hash,
114                     "cached stable hash does not match freshly computed stable hash"
115                 );
116             }
117             stable_hash.hash_stable(hcx, hasher);
118         } else {
119             self.stable_hash.hash_stable(hcx, hasher);
120         }
121     }
122 }