1 use crate::mir::Mutability;
2 use crate::ty::subst::GenericArgKind;
3 use crate::ty::{self, SubstsRef, Ty, TyCtxt, TypeVisitableExt};
4 use rustc_hir::def_id::DefId;
5 use std::fmt::Debug;
6 use std::hash::Hash;
7 use std::iter;
8
9 use self::SimplifiedType::*;
10
11 /// See `simplify_type`.
12 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable)]
13 pub enum SimplifiedType {
14 BoolSimplifiedType,
15 CharSimplifiedType,
16 IntSimplifiedType(ty::IntTy),
17 UintSimplifiedType(ty::UintTy),
18 FloatSimplifiedType(ty::FloatTy),
19 AdtSimplifiedType(DefId),
20 ForeignSimplifiedType(DefId),
21 StrSimplifiedType,
22 ArraySimplifiedType,
23 SliceSimplifiedType,
24 RefSimplifiedType(Mutability),
25 PtrSimplifiedType(Mutability),
26 NeverSimplifiedType,
27 TupleSimplifiedType(usize),
28 /// A trait object, all of whose components are markers
29 /// (e.g., `dyn Send + Sync`).
30 MarkerTraitObjectSimplifiedType,
31 TraitSimplifiedType(DefId),
32 ClosureSimplifiedType(DefId),
33 GeneratorSimplifiedType(DefId),
34 GeneratorWitnessSimplifiedType(usize),
35 GeneratorWitnessMIRSimplifiedType(DefId),
36 FunctionSimplifiedType(usize),
37 PlaceholderSimplifiedType,
38 }
39
40 /// Generic parameters are pretty much just bound variables, e.g.
41 /// the type of `fn foo<'a, T>(x: &'a T) -> u32 { ... }` can be thought of as
42 /// `for<'a, T> fn(&'a T) -> u32`.
43 ///
44 /// Typecheck of `foo` has to succeed for all possible generic arguments, so
45 /// during typeck, we have to treat its generic parameters as if they
46 /// were placeholders.
47 ///
48 /// But when calling `foo` we only have to provide a specific generic argument.
49 /// In that case the generic parameters are instantiated with inference variables.
50 /// As we use `simplify_type` before that instantiation happens, we just treat
51 /// generic parameters as if they were inference variables in that case.
52 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
53 pub enum TreatParams {
54 /// Treat parameters as infer vars. This is the correct mode for caching
55 /// an impl's type for lookup.
56 AsCandidateKey,
57 /// Treat parameters as placeholders in the given environment. This is the
58 /// correct mode for *lookup*, as during candidate selection.
59 ///
60 /// This also treats projections with inference variables as infer vars
61 /// since they could be further normalized.
62 ForLookup,
63 /// Treat parameters as placeholders in the given environment. This is the
64 /// correct mode for *lookup*, as during candidate selection.
65 ///
66 /// N.B. during deep rejection, this acts identically to `ForLookup`.
67 NextSolverLookup,
68 }
69
70 /// During fast-rejection, we have the choice of treating projection types
71 /// as either simplifiable or not, depending on whether we expect the projection
72 /// to be normalized/rigid.
73 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
74 pub enum TreatProjections {
75 /// In the old solver we don't try to normalize projections
76 /// when looking up impls and only access them by using the
77 /// current self type. This means that if the self type is
78 /// a projection which could later be normalized, we must not
79 /// treat it as rigid.
80 ForLookup,
81 /// We can treat projections in the self type as opaque as
82 /// we separately look up impls for the normalized self type.
83 NextSolverLookup,
84 }
85
86 /// Tries to simplify a type by only returning the outermost injective¹ layer, if one exists.
87 ///
88 /// **This function should only be used if you need to store or retrieve the type from some
89 /// hashmap. If you want to quickly decide whether two types may unify, use the [DeepRejectCtxt]
90 /// instead.**
91 ///
92 /// The idea is to get something simple that we can use to quickly decide if two types could unify,
93 /// for example during method lookup. If this function returns `Some(x)` it can only unify with
94 /// types for which this method returns either `Some(x)` as well or `None`.
95 ///
96 /// A special case here are parameters and projections, which are only injective
97 /// if they are treated as placeholders.
98 ///
99 /// For example when storing impls based on their simplified self type, we treat
100 /// generic parameters as if they were inference variables. We must not simplify them here,
101 /// as they can unify with any other type.
102 ///
103 /// With projections we have to be even more careful, as treating them as placeholders
104 /// is only correct if they are fully normalized.
105 ///
106 /// ¹ meaning that if the outermost layers are different, then the whole types are also different.
simplify_type<'tcx>( tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, treat_params: TreatParams, ) -> Option<SimplifiedType>107 pub fn simplify_type<'tcx>(
108 tcx: TyCtxt<'tcx>,
109 ty: Ty<'tcx>,
110 treat_params: TreatParams,
111 ) -> Option<SimplifiedType> {
112 match *ty.kind() {
113 ty::Bool => Some(BoolSimplifiedType),
114 ty::Char => Some(CharSimplifiedType),
115 ty::Int(int_type) => Some(IntSimplifiedType(int_type)),
116 ty::Uint(uint_type) => Some(UintSimplifiedType(uint_type)),
117 ty::Float(float_type) => Some(FloatSimplifiedType(float_type)),
118 ty::Adt(def, _) => Some(AdtSimplifiedType(def.did())),
119 ty::Str => Some(StrSimplifiedType),
120 ty::Array(..) => Some(ArraySimplifiedType),
121 ty::Slice(..) => Some(SliceSimplifiedType),
122 ty::RawPtr(ptr) => Some(PtrSimplifiedType(ptr.mutbl)),
123 ty::Dynamic(trait_info, ..) => match trait_info.principal_def_id() {
124 Some(principal_def_id) if !tcx.trait_is_auto(principal_def_id) => {
125 Some(TraitSimplifiedType(principal_def_id))
126 }
127 _ => Some(MarkerTraitObjectSimplifiedType),
128 },
129 ty::Ref(_, _, mutbl) => Some(RefSimplifiedType(mutbl)),
130 ty::FnDef(def_id, _) | ty::Closure(def_id, _) => Some(ClosureSimplifiedType(def_id)),
131 ty::Generator(def_id, _, _) => Some(GeneratorSimplifiedType(def_id)),
132 ty::GeneratorWitness(tys) => Some(GeneratorWitnessSimplifiedType(tys.skip_binder().len())),
133 ty::GeneratorWitnessMIR(def_id, _) => Some(GeneratorWitnessMIRSimplifiedType(def_id)),
134 ty::Never => Some(NeverSimplifiedType),
135 ty::Tuple(tys) => Some(TupleSimplifiedType(tys.len())),
136 ty::FnPtr(f) => Some(FunctionSimplifiedType(f.skip_binder().inputs().len())),
137 ty::Placeholder(..) => Some(PlaceholderSimplifiedType),
138 ty::Param(_) => match treat_params {
139 TreatParams::ForLookup | TreatParams::NextSolverLookup => {
140 Some(PlaceholderSimplifiedType)
141 }
142 TreatParams::AsCandidateKey => None,
143 },
144 ty::Alias(..) => match treat_params {
145 // When treating `ty::Param` as a placeholder, projections also
146 // don't unify with anything else as long as they are fully normalized.
147 //
148 // We will have to be careful with lazy normalization here.
149 // FIXME(lazy_normalization): This is probably not right...
150 TreatParams::ForLookup if !ty.has_non_region_infer() => Some(PlaceholderSimplifiedType),
151 TreatParams::NextSolverLookup => Some(PlaceholderSimplifiedType),
152 TreatParams::ForLookup | TreatParams::AsCandidateKey => None,
153 },
154 ty::Foreign(def_id) => Some(ForeignSimplifiedType(def_id)),
155 ty::Bound(..) | ty::Infer(_) | ty::Error(_) => None,
156 }
157 }
158
159 impl SimplifiedType {
def(self) -> Option<DefId>160 pub fn def(self) -> Option<DefId> {
161 match self {
162 AdtSimplifiedType(d)
163 | ForeignSimplifiedType(d)
164 | TraitSimplifiedType(d)
165 | ClosureSimplifiedType(d)
166 | GeneratorSimplifiedType(d)
167 | GeneratorWitnessMIRSimplifiedType(d) => Some(d),
168 _ => None,
169 }
170 }
171 }
172
173 /// Given generic arguments from an obligation and an impl,
174 /// could these two be unified after replacing parameters in the
175 /// the impl with inference variables.
176 ///
177 /// For obligations, parameters won't be replaced by inference
178 /// variables and only unify with themselves. We treat them
179 /// the same way we treat placeholders.
180 ///
181 /// We also use this function during coherence. For coherence the
182 /// impls only have to overlap for some value, so we treat parameters
183 /// on both sides like inference variables. This behavior is toggled
184 /// using the `treat_obligation_params` field.
185 #[derive(Debug, Clone, Copy)]
186 pub struct DeepRejectCtxt {
187 pub treat_obligation_params: TreatParams,
188 }
189
190 impl DeepRejectCtxt {
substs_refs_may_unify<'tcx>( self, obligation_substs: SubstsRef<'tcx>, impl_substs: SubstsRef<'tcx>, ) -> bool191 pub fn substs_refs_may_unify<'tcx>(
192 self,
193 obligation_substs: SubstsRef<'tcx>,
194 impl_substs: SubstsRef<'tcx>,
195 ) -> bool {
196 iter::zip(obligation_substs, impl_substs).all(|(obl, imp)| {
197 match (obl.unpack(), imp.unpack()) {
198 // We don't fast reject based on regions for now.
199 (GenericArgKind::Lifetime(_), GenericArgKind::Lifetime(_)) => true,
200 (GenericArgKind::Type(obl), GenericArgKind::Type(imp)) => {
201 self.types_may_unify(obl, imp)
202 }
203 (GenericArgKind::Const(obl), GenericArgKind::Const(imp)) => {
204 self.consts_may_unify(obl, imp)
205 }
206 _ => bug!("kind mismatch: {obl} {imp}"),
207 }
208 })
209 }
210
types_may_unify<'tcx>(self, obligation_ty: Ty<'tcx>, impl_ty: Ty<'tcx>) -> bool211 pub fn types_may_unify<'tcx>(self, obligation_ty: Ty<'tcx>, impl_ty: Ty<'tcx>) -> bool {
212 match impl_ty.kind() {
213 // Start by checking whether the type in the impl may unify with
214 // pretty much everything. Just return `true` in that case.
215 ty::Param(_) | ty::Error(_) | ty::Alias(..) => return true,
216 // These types only unify with inference variables or their own
217 // variant.
218 ty::Bool
219 | ty::Char
220 | ty::Int(_)
221 | ty::Uint(_)
222 | ty::Float(_)
223 | ty::Adt(..)
224 | ty::Str
225 | ty::Array(..)
226 | ty::Slice(..)
227 | ty::RawPtr(..)
228 | ty::Dynamic(..)
229 | ty::Ref(..)
230 | ty::Never
231 | ty::Tuple(..)
232 | ty::FnPtr(..)
233 | ty::Foreign(..) => {}
234 ty::FnDef(..)
235 | ty::Closure(..)
236 | ty::Generator(..)
237 | ty::GeneratorWitness(..)
238 | ty::GeneratorWitnessMIR(..)
239 | ty::Placeholder(..)
240 | ty::Bound(..)
241 | ty::Infer(_) => bug!("unexpected impl_ty: {impl_ty}"),
242 }
243
244 let k = impl_ty.kind();
245 match *obligation_ty.kind() {
246 // Purely rigid types, use structural equivalence.
247 ty::Bool
248 | ty::Char
249 | ty::Int(_)
250 | ty::Uint(_)
251 | ty::Float(_)
252 | ty::Str
253 | ty::Never
254 | ty::Foreign(_) => obligation_ty == impl_ty,
255 ty::Ref(_, obl_ty, obl_mutbl) => match k {
256 &ty::Ref(_, impl_ty, impl_mutbl) => {
257 obl_mutbl == impl_mutbl && self.types_may_unify(obl_ty, impl_ty)
258 }
259 _ => false,
260 },
261 ty::Adt(obl_def, obl_substs) => match k {
262 &ty::Adt(impl_def, impl_substs) => {
263 obl_def == impl_def && self.substs_refs_may_unify(obl_substs, impl_substs)
264 }
265 _ => false,
266 },
267 ty::Slice(obl_ty) => {
268 matches!(k, &ty::Slice(impl_ty) if self.types_may_unify(obl_ty, impl_ty))
269 }
270 ty::Array(obl_ty, obl_len) => match k {
271 &ty::Array(impl_ty, impl_len) => {
272 self.types_may_unify(obl_ty, impl_ty)
273 && self.consts_may_unify(obl_len, impl_len)
274 }
275 _ => false,
276 },
277 ty::Tuple(obl) => match k {
278 &ty::Tuple(imp) => {
279 obl.len() == imp.len()
280 && iter::zip(obl, imp).all(|(obl, imp)| self.types_may_unify(obl, imp))
281 }
282 _ => false,
283 },
284 ty::RawPtr(obl) => match k {
285 ty::RawPtr(imp) => obl.mutbl == imp.mutbl && self.types_may_unify(obl.ty, imp.ty),
286 _ => false,
287 },
288 ty::Dynamic(obl_preds, ..) => {
289 // Ideally we would walk the existential predicates here or at least
290 // compare their length. But considering that the relevant `Relate` impl
291 // actually sorts and deduplicates these, that doesn't work.
292 matches!(k, ty::Dynamic(impl_preds, ..) if
293 obl_preds.principal_def_id() == impl_preds.principal_def_id()
294 )
295 }
296 ty::FnPtr(obl_sig) => match k {
297 ty::FnPtr(impl_sig) => {
298 let ty::FnSig { inputs_and_output, c_variadic, unsafety, abi } =
299 obl_sig.skip_binder();
300 let impl_sig = impl_sig.skip_binder();
301
302 abi == impl_sig.abi
303 && c_variadic == impl_sig.c_variadic
304 && unsafety == impl_sig.unsafety
305 && inputs_and_output.len() == impl_sig.inputs_and_output.len()
306 && iter::zip(inputs_and_output, impl_sig.inputs_and_output)
307 .all(|(obl, imp)| self.types_may_unify(obl, imp))
308 }
309 _ => false,
310 },
311
312 // Impls cannot contain these types as these cannot be named directly.
313 ty::FnDef(..) | ty::Closure(..) | ty::Generator(..) => false,
314
315 // Placeholder types don't unify with anything on their own
316 ty::Placeholder(..) | ty::Bound(..) => false,
317
318 // Depending on the value of `treat_obligation_params`, we either
319 // treat generic parameters like placeholders or like inference variables.
320 ty::Param(_) => match self.treat_obligation_params {
321 TreatParams::ForLookup | TreatParams::NextSolverLookup => false,
322 TreatParams::AsCandidateKey => true,
323 },
324
325 ty::Infer(ty::IntVar(_)) => impl_ty.is_integral(),
326
327 ty::Infer(ty::FloatVar(_)) => impl_ty.is_floating_point(),
328
329 ty::Infer(_) => true,
330
331 // As we're walking the whole type, it may encounter projections
332 // inside of binders and what not, so we're just going to assume that
333 // projections can unify with other stuff.
334 //
335 // Looking forward to lazy normalization this is the safer strategy anyways.
336 ty::Alias(..) => true,
337
338 ty::Error(_) => true,
339
340 ty::GeneratorWitness(..) | ty::GeneratorWitnessMIR(..) => {
341 bug!("unexpected obligation type: {:?}", obligation_ty)
342 }
343 }
344 }
345
consts_may_unify(self, obligation_ct: ty::Const<'_>, impl_ct: ty::Const<'_>) -> bool346 pub fn consts_may_unify(self, obligation_ct: ty::Const<'_>, impl_ct: ty::Const<'_>) -> bool {
347 match impl_ct.kind() {
348 ty::ConstKind::Expr(_)
349 | ty::ConstKind::Param(_)
350 | ty::ConstKind::Unevaluated(_)
351 | ty::ConstKind::Error(_) => {
352 return true;
353 }
354 ty::ConstKind::Value(_) => {}
355 ty::ConstKind::Infer(_) | ty::ConstKind::Bound(..) | ty::ConstKind::Placeholder(_) => {
356 bug!("unexpected impl arg: {:?}", impl_ct)
357 }
358 }
359
360 let k = impl_ct.kind();
361 match obligation_ct.kind() {
362 ty::ConstKind::Param(_) => match self.treat_obligation_params {
363 TreatParams::ForLookup | TreatParams::NextSolverLookup => false,
364 TreatParams::AsCandidateKey => true,
365 },
366
367 // Placeholder consts don't unify with anything on their own
368 ty::ConstKind::Placeholder(_) => false,
369
370 // As we don't necessarily eagerly evaluate constants,
371 // they might unify with any value.
372 ty::ConstKind::Expr(_) | ty::ConstKind::Unevaluated(_) | ty::ConstKind::Error(_) => {
373 true
374 }
375 ty::ConstKind::Value(obl) => match k {
376 ty::ConstKind::Value(imp) => obl == imp,
377 _ => true,
378 },
379
380 ty::ConstKind::Infer(_) => true,
381
382 ty::ConstKind::Bound(..) => {
383 bug!("unexpected obl const: {:?}", obligation_ct)
384 }
385 }
386 }
387 }
388