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;
10 use self::SimplifiedTypeGen::*;
12 pub type SimplifiedType = SimplifiedTypeGen<DefId>;
14 /// See `simplify_type`
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>
27 IntSimplifiedType(ty::IntTy),
28 UintSimplifiedType(ty::UintTy),
29 FloatSimplifiedType(ty::FloatTy),
31 ForeignSimplifiedType(D),
35 RefSimplifiedType(Mutability),
36 PtrSimplifiedType(Mutability),
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,
51 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
52 pub enum TreatParams {
53 /// Treat parameters as bound types in the given environment.
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.
62 /// Tries to simplify a type by only returning the outermost injective¹ layer, if one exists.
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.
67 /// A special case here are parameters and projections, which are only injective
68 /// if they are treated as bound types.
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.
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.
77 /// ¹ meaning that if the outermost layers are different, then the whole types are also different.
78 pub fn simplify_type<'tcx>(
81 treat_params: TreatParams,
82 ) -> Option<SimplifiedType> {
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))
98 _ => Some(MarkerTraitObjectSimplifiedType),
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.
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)
116 TreatParams::AsPlaceholders => None,
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,
124 impl<D: Copy + Debug + Eq> SimplifiedTypeGen<D> {
125 pub fn def(self) -> Option<D> {
128 | ForeignSimplifiedType(d)
129 | TraitSimplifiedType(d)
130 | ClosureSimplifiedType(d)
131 | GeneratorSimplifiedType(d)
132 | OpaqueSimplifiedType(d) => Some(d),
137 pub fn map_def<U, F>(self, map: F) -> SimplifiedTypeGen<U>
140 U: Copy + Debug + Eq,
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,
169 impl<'a, D> HashStable<StableHashingContext<'a>> for SimplifiedTypeGen<D>
171 D: Copy + Debug + Eq + HashStable<StableHashingContext<'a>>,
173 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
174 mem::discriminant(self).hash_stable(hcx, hasher);
179 | ArraySimplifiedType
180 | SliceSimplifiedType
181 | NeverSimplifiedType
182 | ParameterSimplifiedType
183 | MarkerTraitObjectSimplifiedType => {
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),