]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/fast_reject.rs
Rollup merge of #94566 - yanganto:show-ignore-message, r=m-ou-se
[rust.git] / compiler / rustc_middle / src / ty / fast_reject.rs
1 use crate::mir::Mutability;
2 use crate::ty::{self, Ty, TyCtxt};
3 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4 use rustc_hir::def_id::DefId;
5 use rustc_query_system::ich::StableHashingContext;
6 use std::fmt::Debug;
7 use std::hash::Hash;
8 use std::mem;
9
10 use self::SimplifiedTypeGen::*;
11
12 pub type SimplifiedType = SimplifiedTypeGen<DefId>;
13
14 /// See `simplify_type`
15 ///
16 /// Note that we keep this type generic over the type of identifier it uses
17 /// because we sometimes need to use SimplifiedTypeGen values as stable sorting
18 /// keys (in which case we use a DefPathHash as id-type) but in the general case
19 /// the non-stable but fast to construct DefId-version is the better choice.
20 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
21 pub enum SimplifiedTypeGen<D>
22 where
23     D: Copy + Debug + Eq,
24 {
25     BoolSimplifiedType,
26     CharSimplifiedType,
27     IntSimplifiedType(ty::IntTy),
28     UintSimplifiedType(ty::UintTy),
29     FloatSimplifiedType(ty::FloatTy),
30     AdtSimplifiedType(D),
31     ForeignSimplifiedType(D),
32     StrSimplifiedType,
33     ArraySimplifiedType,
34     SliceSimplifiedType,
35     RefSimplifiedType(Mutability),
36     PtrSimplifiedType(Mutability),
37     NeverSimplifiedType,
38     TupleSimplifiedType(usize),
39     /// A trait object, all of whose components are markers
40     /// (e.g., `dyn Send + Sync`).
41     MarkerTraitObjectSimplifiedType,
42     TraitSimplifiedType(D),
43     ClosureSimplifiedType(D),
44     GeneratorSimplifiedType(D),
45     GeneratorWitnessSimplifiedType(usize),
46     OpaqueSimplifiedType(D),
47     FunctionSimplifiedType(usize),
48     ParameterSimplifiedType,
49 }
50
51 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
52 pub enum TreatParams {
53     /// Treat parameters as bound types in the given environment.
54     ///
55     /// For this to be correct the input has to be fully normalized
56     /// in its param env as it may otherwise cause us to ignore
57     /// potentially applying impls.
58     AsBoundTypes,
59     AsPlaceholders,
60 }
61
62 /// Tries to simplify a type by only returning the outermost injective¹ layer, if one exists.
63 ///
64 /// The idea is to get something simple that we can use to quickly decide if two types could unify,
65 /// for example during method lookup.
66 ///
67 /// A special case here are parameters and projections, which are only injective
68 /// if they are treated as bound types.
69 ///
70 /// For example when storing impls based on their simplified self type, we treat
71 /// generic parameters as placeholders. We must not simplify them here,
72 /// as they can unify with any other type.
73 ///
74 /// With projections we have to be even more careful, as even when treating them as bound types
75 /// this is still only correct if they are fully normalized.
76 ///
77 /// ¹ meaning that if the outermost layers are different, then the whole types are also different.
78 pub fn simplify_type<'tcx>(
79     tcx: TyCtxt<'tcx>,
80     ty: Ty<'tcx>,
81     treat_params: TreatParams,
82 ) -> Option<SimplifiedType> {
83     match *ty.kind() {
84         ty::Bool => Some(BoolSimplifiedType),
85         ty::Char => Some(CharSimplifiedType),
86         ty::Int(int_type) => Some(IntSimplifiedType(int_type)),
87         ty::Uint(uint_type) => Some(UintSimplifiedType(uint_type)),
88         ty::Float(float_type) => Some(FloatSimplifiedType(float_type)),
89         ty::Adt(def, _) => Some(AdtSimplifiedType(def.did())),
90         ty::Str => Some(StrSimplifiedType),
91         ty::Array(..) => Some(ArraySimplifiedType),
92         ty::Slice(..) => Some(SliceSimplifiedType),
93         ty::RawPtr(ptr) => Some(PtrSimplifiedType(ptr.mutbl)),
94         ty::Dynamic(trait_info, ..) => match trait_info.principal_def_id() {
95             Some(principal_def_id) if !tcx.trait_is_auto(principal_def_id) => {
96                 Some(TraitSimplifiedType(principal_def_id))
97             }
98             _ => Some(MarkerTraitObjectSimplifiedType),
99         },
100         ty::Ref(_, _, mutbl) => Some(RefSimplifiedType(mutbl)),
101         ty::FnDef(def_id, _) | ty::Closure(def_id, _) => Some(ClosureSimplifiedType(def_id)),
102         ty::Generator(def_id, _, _) => Some(GeneratorSimplifiedType(def_id)),
103         ty::GeneratorWitness(tys) => Some(GeneratorWitnessSimplifiedType(tys.skip_binder().len())),
104         ty::Never => Some(NeverSimplifiedType),
105         ty::Tuple(tys) => Some(TupleSimplifiedType(tys.len())),
106         ty::FnPtr(f) => Some(FunctionSimplifiedType(f.skip_binder().inputs().len())),
107         ty::Param(_) | ty::Projection(_) => match treat_params {
108             // When treated as bound types, projections don't unify with
109             // anything as long as they are fully normalized.
110             //
111             // We will have to be careful with lazy normalization here.
112             TreatParams::AsBoundTypes => {
113                 debug!("treating `{}` as a bound type", ty);
114                 Some(ParameterSimplifiedType)
115             }
116             TreatParams::AsPlaceholders => None,
117         },
118         ty::Opaque(def_id, _) => Some(OpaqueSimplifiedType(def_id)),
119         ty::Foreign(def_id) => Some(ForeignSimplifiedType(def_id)),
120         ty::Placeholder(..) | ty::Bound(..) | ty::Infer(_) | ty::Error(_) => None,
121     }
122 }
123
124 impl<D: Copy + Debug + Eq> SimplifiedTypeGen<D> {
125     pub fn def(self) -> Option<D> {
126         match self {
127             AdtSimplifiedType(d)
128             | ForeignSimplifiedType(d)
129             | TraitSimplifiedType(d)
130             | ClosureSimplifiedType(d)
131             | GeneratorSimplifiedType(d)
132             | OpaqueSimplifiedType(d) => Some(d),
133             _ => None,
134         }
135     }
136
137     pub fn map_def<U, F>(self, map: F) -> SimplifiedTypeGen<U>
138     where
139         F: Fn(D) -> U,
140         U: Copy + Debug + Eq,
141     {
142         match self {
143             BoolSimplifiedType => BoolSimplifiedType,
144             CharSimplifiedType => CharSimplifiedType,
145             IntSimplifiedType(t) => IntSimplifiedType(t),
146             UintSimplifiedType(t) => UintSimplifiedType(t),
147             FloatSimplifiedType(t) => FloatSimplifiedType(t),
148             AdtSimplifiedType(d) => AdtSimplifiedType(map(d)),
149             ForeignSimplifiedType(d) => ForeignSimplifiedType(map(d)),
150             StrSimplifiedType => StrSimplifiedType,
151             ArraySimplifiedType => ArraySimplifiedType,
152             SliceSimplifiedType => SliceSimplifiedType,
153             RefSimplifiedType(m) => RefSimplifiedType(m),
154             PtrSimplifiedType(m) => PtrSimplifiedType(m),
155             NeverSimplifiedType => NeverSimplifiedType,
156             MarkerTraitObjectSimplifiedType => MarkerTraitObjectSimplifiedType,
157             TupleSimplifiedType(n) => TupleSimplifiedType(n),
158             TraitSimplifiedType(d) => TraitSimplifiedType(map(d)),
159             ClosureSimplifiedType(d) => ClosureSimplifiedType(map(d)),
160             GeneratorSimplifiedType(d) => GeneratorSimplifiedType(map(d)),
161             GeneratorWitnessSimplifiedType(n) => GeneratorWitnessSimplifiedType(n),
162             OpaqueSimplifiedType(d) => OpaqueSimplifiedType(map(d)),
163             FunctionSimplifiedType(n) => FunctionSimplifiedType(n),
164             ParameterSimplifiedType => ParameterSimplifiedType,
165         }
166     }
167 }
168
169 impl<'a, D> HashStable<StableHashingContext<'a>> for SimplifiedTypeGen<D>
170 where
171     D: Copy + Debug + Eq + HashStable<StableHashingContext<'a>>,
172 {
173     fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
174         mem::discriminant(self).hash_stable(hcx, hasher);
175         match *self {
176             BoolSimplifiedType
177             | CharSimplifiedType
178             | StrSimplifiedType
179             | ArraySimplifiedType
180             | SliceSimplifiedType
181             | NeverSimplifiedType
182             | ParameterSimplifiedType
183             | MarkerTraitObjectSimplifiedType => {
184                 // nothing to do
185             }
186             RefSimplifiedType(m) | PtrSimplifiedType(m) => m.hash_stable(hcx, hasher),
187             IntSimplifiedType(t) => t.hash_stable(hcx, hasher),
188             UintSimplifiedType(t) => t.hash_stable(hcx, hasher),
189             FloatSimplifiedType(t) => t.hash_stable(hcx, hasher),
190             AdtSimplifiedType(d) => d.hash_stable(hcx, hasher),
191             TupleSimplifiedType(n) => n.hash_stable(hcx, hasher),
192             TraitSimplifiedType(d) => d.hash_stable(hcx, hasher),
193             ClosureSimplifiedType(d) => d.hash_stable(hcx, hasher),
194             GeneratorSimplifiedType(d) => d.hash_stable(hcx, hasher),
195             GeneratorWitnessSimplifiedType(n) => n.hash_stable(hcx, hasher),
196             OpaqueSimplifiedType(d) => d.hash_stable(hcx, hasher),
197             FunctionSimplifiedType(n) => n.hash_stable(hcx, hasher),
198             ForeignSimplifiedType(d) => d.hash_stable(hcx, hasher),
199         }
200     }
201 }