]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/relate.rs
Rollup merge of #68500 - Mark-Simulacrum:fix-bootstrap-clearing, r=alexcrichton
[rust.git] / src / librustc / ty / relate.rs
1 //! Generalized type relating mechanism.
2 //!
3 //! A type relation `R` relates a pair of values `(A, B)`. `A and B` are usually
4 //! types or regions but can be other things. Examples of type relations are
5 //! subtyping, type equality, etc.
6
7 use crate::mir::interpret::{get_slice_bytes, ConstValue};
8 use crate::traits;
9 use crate::ty::error::{ExpectedFound, TypeError};
10 use crate::ty::subst::{GenericArg, GenericArgKind, SubstsRef};
11 use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
12 use rustc_hir as ast;
13 use rustc_hir::def_id::DefId;
14 use rustc_target::spec::abi;
15 use std::iter;
16 use std::rc::Rc;
17
18 pub type RelateResult<'tcx, T> = Result<T, TypeError<'tcx>>;
19
20 #[derive(Clone, Debug)]
21 pub enum Cause {
22     ExistentialRegionBound, // relating an existential region bound
23 }
24
25 pub trait TypeRelation<'tcx>: Sized {
26     fn tcx(&self) -> TyCtxt<'tcx>;
27
28     fn param_env(&self) -> ty::ParamEnv<'tcx>;
29
30     /// Returns a static string we can use for printouts.
31     fn tag(&self) -> &'static str;
32
33     /// Returns `true` if the value `a` is the "expected" type in the
34     /// relation. Just affects error messages.
35     fn a_is_expected(&self) -> bool;
36
37     fn with_cause<F, R>(&mut self, _cause: Cause, f: F) -> R
38     where
39         F: FnOnce(&mut Self) -> R,
40     {
41         f(self)
42     }
43
44     /// Generic relation routine suitable for most anything.
45     fn relate<T: Relate<'tcx>>(&mut self, a: &T, b: &T) -> RelateResult<'tcx, T> {
46         Relate::relate(self, a, b)
47     }
48
49     /// Relate the two substitutions for the given item. The default
50     /// is to look up the variance for the item and proceed
51     /// accordingly.
52     fn relate_item_substs(
53         &mut self,
54         item_def_id: DefId,
55         a_subst: SubstsRef<'tcx>,
56         b_subst: SubstsRef<'tcx>,
57     ) -> RelateResult<'tcx, SubstsRef<'tcx>> {
58         debug!(
59             "relate_item_substs(item_def_id={:?}, a_subst={:?}, b_subst={:?})",
60             item_def_id, a_subst, b_subst
61         );
62
63         let opt_variances = self.tcx().variances_of(item_def_id);
64         relate_substs(self, Some(opt_variances), a_subst, b_subst)
65     }
66
67     /// Switch variance for the purpose of relating `a` and `b`.
68     fn relate_with_variance<T: Relate<'tcx>>(
69         &mut self,
70         variance: ty::Variance,
71         a: &T,
72         b: &T,
73     ) -> RelateResult<'tcx, T>;
74
75     // Overrideable relations. You shouldn't typically call these
76     // directly, instead call `relate()`, which in turn calls
77     // these. This is both more uniform but also allows us to add
78     // additional hooks for other types in the future if needed
79     // without making older code, which called `relate`, obsolete.
80
81     fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>>;
82
83     fn regions(
84         &mut self,
85         a: ty::Region<'tcx>,
86         b: ty::Region<'tcx>,
87     ) -> RelateResult<'tcx, ty::Region<'tcx>>;
88
89     fn consts(
90         &mut self,
91         a: &'tcx ty::Const<'tcx>,
92         b: &'tcx ty::Const<'tcx>,
93     ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>>;
94
95     fn binders<T>(
96         &mut self,
97         a: &ty::Binder<T>,
98         b: &ty::Binder<T>,
99     ) -> RelateResult<'tcx, ty::Binder<T>>
100     where
101         T: Relate<'tcx>;
102 }
103
104 pub trait Relate<'tcx>: TypeFoldable<'tcx> {
105     fn relate<R: TypeRelation<'tcx>>(
106         relation: &mut R,
107         a: &Self,
108         b: &Self,
109     ) -> RelateResult<'tcx, Self>;
110 }
111
112 ///////////////////////////////////////////////////////////////////////////
113 // Relate impls
114
115 impl<'tcx> Relate<'tcx> for ty::TypeAndMut<'tcx> {
116     fn relate<R: TypeRelation<'tcx>>(
117         relation: &mut R,
118         a: &ty::TypeAndMut<'tcx>,
119         b: &ty::TypeAndMut<'tcx>,
120     ) -> RelateResult<'tcx, ty::TypeAndMut<'tcx>> {
121         debug!("{}.mts({:?}, {:?})", relation.tag(), a, b);
122         if a.mutbl != b.mutbl {
123             Err(TypeError::Mutability)
124         } else {
125             let mutbl = a.mutbl;
126             let variance = match mutbl {
127                 ast::Mutability::Not => ty::Covariant,
128                 ast::Mutability::Mut => ty::Invariant,
129             };
130             let ty = relation.relate_with_variance(variance, &a.ty, &b.ty)?;
131             Ok(ty::TypeAndMut { ty, mutbl })
132         }
133     }
134 }
135
136 pub fn relate_substs<R: TypeRelation<'tcx>>(
137     relation: &mut R,
138     variances: Option<&[ty::Variance]>,
139     a_subst: SubstsRef<'tcx>,
140     b_subst: SubstsRef<'tcx>,
141 ) -> RelateResult<'tcx, SubstsRef<'tcx>> {
142     let tcx = relation.tcx();
143
144     let params = a_subst.iter().zip(b_subst).enumerate().map(|(i, (a, b))| {
145         let variance = variances.map_or(ty::Invariant, |v| v[i]);
146         relation.relate_with_variance(variance, a, b)
147     });
148
149     Ok(tcx.mk_substs(params)?)
150 }
151
152 impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> {
153     fn relate<R: TypeRelation<'tcx>>(
154         relation: &mut R,
155         a: &ty::FnSig<'tcx>,
156         b: &ty::FnSig<'tcx>,
157     ) -> RelateResult<'tcx, ty::FnSig<'tcx>> {
158         let tcx = relation.tcx();
159
160         if a.c_variadic != b.c_variadic {
161             return Err(TypeError::VariadicMismatch(expected_found(
162                 relation,
163                 &a.c_variadic,
164                 &b.c_variadic,
165             )));
166         }
167         let unsafety = relation.relate(&a.unsafety, &b.unsafety)?;
168         let abi = relation.relate(&a.abi, &b.abi)?;
169
170         if a.inputs().len() != b.inputs().len() {
171             return Err(TypeError::ArgCount);
172         }
173
174         let inputs_and_output = a
175             .inputs()
176             .iter()
177             .cloned()
178             .zip(b.inputs().iter().cloned())
179             .map(|x| (x, false))
180             .chain(iter::once(((a.output(), b.output()), true)))
181             .map(|((a, b), is_output)| {
182                 if is_output {
183                     relation.relate(&a, &b)
184                 } else {
185                     relation.relate_with_variance(ty::Contravariant, &a, &b)
186                 }
187             });
188         Ok(ty::FnSig {
189             inputs_and_output: tcx.mk_type_list(inputs_and_output)?,
190             c_variadic: a.c_variadic,
191             unsafety,
192             abi,
193         })
194     }
195 }
196
197 impl<'tcx> Relate<'tcx> for ast::Unsafety {
198     fn relate<R: TypeRelation<'tcx>>(
199         relation: &mut R,
200         a: &ast::Unsafety,
201         b: &ast::Unsafety,
202     ) -> RelateResult<'tcx, ast::Unsafety> {
203         if a != b {
204             Err(TypeError::UnsafetyMismatch(expected_found(relation, a, b)))
205         } else {
206             Ok(*a)
207         }
208     }
209 }
210
211 impl<'tcx> Relate<'tcx> for abi::Abi {
212     fn relate<R: TypeRelation<'tcx>>(
213         relation: &mut R,
214         a: &abi::Abi,
215         b: &abi::Abi,
216     ) -> RelateResult<'tcx, abi::Abi> {
217         if a == b { Ok(*a) } else { Err(TypeError::AbiMismatch(expected_found(relation, a, b))) }
218     }
219 }
220
221 impl<'tcx> Relate<'tcx> for ty::ProjectionTy<'tcx> {
222     fn relate<R: TypeRelation<'tcx>>(
223         relation: &mut R,
224         a: &ty::ProjectionTy<'tcx>,
225         b: &ty::ProjectionTy<'tcx>,
226     ) -> RelateResult<'tcx, ty::ProjectionTy<'tcx>> {
227         if a.item_def_id != b.item_def_id {
228             Err(TypeError::ProjectionMismatched(expected_found(
229                 relation,
230                 &a.item_def_id,
231                 &b.item_def_id,
232             )))
233         } else {
234             let substs = relation.relate(&a.substs, &b.substs)?;
235             Ok(ty::ProjectionTy { item_def_id: a.item_def_id, substs: &substs })
236         }
237     }
238 }
239
240 impl<'tcx> Relate<'tcx> for ty::ExistentialProjection<'tcx> {
241     fn relate<R: TypeRelation<'tcx>>(
242         relation: &mut R,
243         a: &ty::ExistentialProjection<'tcx>,
244         b: &ty::ExistentialProjection<'tcx>,
245     ) -> RelateResult<'tcx, ty::ExistentialProjection<'tcx>> {
246         if a.item_def_id != b.item_def_id {
247             Err(TypeError::ProjectionMismatched(expected_found(
248                 relation,
249                 &a.item_def_id,
250                 &b.item_def_id,
251             )))
252         } else {
253             let ty = relation.relate(&a.ty, &b.ty)?;
254             let substs = relation.relate(&a.substs, &b.substs)?;
255             Ok(ty::ExistentialProjection { item_def_id: a.item_def_id, substs, ty })
256         }
257     }
258 }
259
260 impl<'tcx> Relate<'tcx> for Vec<ty::PolyExistentialProjection<'tcx>> {
261     fn relate<R: TypeRelation<'tcx>>(
262         relation: &mut R,
263         a: &Vec<ty::PolyExistentialProjection<'tcx>>,
264         b: &Vec<ty::PolyExistentialProjection<'tcx>>,
265     ) -> RelateResult<'tcx, Vec<ty::PolyExistentialProjection<'tcx>>> {
266         // To be compatible, `a` and `b` must be for precisely the
267         // same set of traits and item names. We always require that
268         // projection bounds lists are sorted by trait-def-id and item-name,
269         // so we can just iterate through the lists pairwise, so long as they are the
270         // same length.
271         if a.len() != b.len() {
272             Err(TypeError::ProjectionBoundsLength(expected_found(relation, &a.len(), &b.len())))
273         } else {
274             a.iter().zip(b).map(|(a, b)| relation.relate(a, b)).collect()
275         }
276     }
277 }
278
279 impl<'tcx> Relate<'tcx> for ty::TraitRef<'tcx> {
280     fn relate<R: TypeRelation<'tcx>>(
281         relation: &mut R,
282         a: &ty::TraitRef<'tcx>,
283         b: &ty::TraitRef<'tcx>,
284     ) -> RelateResult<'tcx, ty::TraitRef<'tcx>> {
285         // Different traits cannot be related.
286         if a.def_id != b.def_id {
287             Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
288         } else {
289             let substs = relate_substs(relation, None, a.substs, b.substs)?;
290             Ok(ty::TraitRef { def_id: a.def_id, substs: substs })
291         }
292     }
293 }
294
295 impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
296     fn relate<R: TypeRelation<'tcx>>(
297         relation: &mut R,
298         a: &ty::ExistentialTraitRef<'tcx>,
299         b: &ty::ExistentialTraitRef<'tcx>,
300     ) -> RelateResult<'tcx, ty::ExistentialTraitRef<'tcx>> {
301         // Different traits cannot be related.
302         if a.def_id != b.def_id {
303             Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
304         } else {
305             let substs = relate_substs(relation, None, a.substs, b.substs)?;
306             Ok(ty::ExistentialTraitRef { def_id: a.def_id, substs: substs })
307         }
308     }
309 }
310
311 #[derive(Debug, Clone, TypeFoldable)]
312 struct GeneratorWitness<'tcx>(&'tcx ty::List<Ty<'tcx>>);
313
314 impl<'tcx> Relate<'tcx> for GeneratorWitness<'tcx> {
315     fn relate<R: TypeRelation<'tcx>>(
316         relation: &mut R,
317         a: &GeneratorWitness<'tcx>,
318         b: &GeneratorWitness<'tcx>,
319     ) -> RelateResult<'tcx, GeneratorWitness<'tcx>> {
320         assert_eq!(a.0.len(), b.0.len());
321         let tcx = relation.tcx();
322         let types = tcx.mk_type_list(a.0.iter().zip(b.0).map(|(a, b)| relation.relate(a, b)))?;
323         Ok(GeneratorWitness(types))
324     }
325 }
326
327 impl<'tcx> Relate<'tcx> for Ty<'tcx> {
328     fn relate<R: TypeRelation<'tcx>>(
329         relation: &mut R,
330         a: &Ty<'tcx>,
331         b: &Ty<'tcx>,
332     ) -> RelateResult<'tcx, Ty<'tcx>> {
333         relation.tys(a, b)
334     }
335 }
336
337 /// The main "type relation" routine. Note that this does not handle
338 /// inference artifacts, so you should filter those out before calling
339 /// it.
340 pub fn super_relate_tys<R: TypeRelation<'tcx>>(
341     relation: &mut R,
342     a: Ty<'tcx>,
343     b: Ty<'tcx>,
344 ) -> RelateResult<'tcx, Ty<'tcx>> {
345     let tcx = relation.tcx();
346     debug!("super_relate_tys: a={:?} b={:?}", a, b);
347     match (&a.kind, &b.kind) {
348         (&ty::Infer(_), _) | (_, &ty::Infer(_)) => {
349             // The caller should handle these cases!
350             bug!("var types encountered in super_relate_tys")
351         }
352
353         (ty::Bound(..), _) | (_, ty::Bound(..)) => {
354             bug!("bound types encountered in super_relate_tys")
355         }
356
357         (&ty::Error, _) | (_, &ty::Error) => Ok(tcx.types.err),
358
359         (&ty::Never, _)
360         | (&ty::Char, _)
361         | (&ty::Bool, _)
362         | (&ty::Int(_), _)
363         | (&ty::Uint(_), _)
364         | (&ty::Float(_), _)
365         | (&ty::Str, _)
366             if a == b =>
367         {
368             Ok(a)
369         }
370
371         (&ty::Param(ref a_p), &ty::Param(ref b_p)) if a_p.index == b_p.index => Ok(a),
372
373         (ty::Placeholder(p1), ty::Placeholder(p2)) if p1 == p2 => Ok(a),
374
375         (&ty::Adt(a_def, a_substs), &ty::Adt(b_def, b_substs)) if a_def == b_def => {
376             let substs = relation.relate_item_substs(a_def.did, a_substs, b_substs)?;
377             Ok(tcx.mk_adt(a_def, substs))
378         }
379
380         (&ty::Foreign(a_id), &ty::Foreign(b_id)) if a_id == b_id => Ok(tcx.mk_foreign(a_id)),
381
382         (&ty::Dynamic(ref a_obj, ref a_region), &ty::Dynamic(ref b_obj, ref b_region)) => {
383             let region_bound = relation.with_cause(Cause::ExistentialRegionBound, |relation| {
384                 relation.relate_with_variance(ty::Contravariant, a_region, b_region)
385             })?;
386             Ok(tcx.mk_dynamic(relation.relate(a_obj, b_obj)?, region_bound))
387         }
388
389         (&ty::Generator(a_id, a_substs, movability), &ty::Generator(b_id, b_substs, _))
390             if a_id == b_id =>
391         {
392             // All Generator types with the same id represent
393             // the (anonymous) type of the same generator expression. So
394             // all of their regions should be equated.
395             let substs = relation.relate(&a_substs, &b_substs)?;
396             Ok(tcx.mk_generator(a_id, substs, movability))
397         }
398
399         (&ty::GeneratorWitness(a_types), &ty::GeneratorWitness(b_types)) => {
400             // Wrap our types with a temporary GeneratorWitness struct
401             // inside the binder so we can related them
402             let a_types = a_types.map_bound(GeneratorWitness);
403             let b_types = b_types.map_bound(GeneratorWitness);
404             // Then remove the GeneratorWitness for the result
405             let types = relation.relate(&a_types, &b_types)?.map_bound(|witness| witness.0);
406             Ok(tcx.mk_generator_witness(types))
407         }
408
409         (&ty::Closure(a_id, a_substs), &ty::Closure(b_id, b_substs)) if a_id == b_id => {
410             // All Closure types with the same id represent
411             // the (anonymous) type of the same closure expression. So
412             // all of their regions should be equated.
413             let substs = relation.relate(&a_substs, &b_substs)?;
414             Ok(tcx.mk_closure(a_id, &substs))
415         }
416
417         (&ty::RawPtr(ref a_mt), &ty::RawPtr(ref b_mt)) => {
418             let mt = relation.relate(a_mt, b_mt)?;
419             Ok(tcx.mk_ptr(mt))
420         }
421
422         (&ty::Ref(a_r, a_ty, a_mutbl), &ty::Ref(b_r, b_ty, b_mutbl)) => {
423             let r = relation.relate_with_variance(ty::Contravariant, &a_r, &b_r)?;
424             let a_mt = ty::TypeAndMut { ty: a_ty, mutbl: a_mutbl };
425             let b_mt = ty::TypeAndMut { ty: b_ty, mutbl: b_mutbl };
426             let mt = relation.relate(&a_mt, &b_mt)?;
427             Ok(tcx.mk_ref(r, mt))
428         }
429
430         (&ty::Array(a_t, sz_a), &ty::Array(b_t, sz_b)) => {
431             let t = relation.relate(&a_t, &b_t)?;
432             match relation.relate(&sz_a, &sz_b) {
433                 Ok(sz) => Ok(tcx.mk_ty(ty::Array(t, sz))),
434                 Err(err) => {
435                     // Check whether the lengths are both concrete/known values,
436                     // but are unequal, for better diagnostics.
437                     let sz_a = sz_a.try_eval_usize(tcx, relation.param_env());
438                     let sz_b = sz_b.try_eval_usize(tcx, relation.param_env());
439                     match (sz_a, sz_b) {
440                         (Some(sz_a_val), Some(sz_b_val)) => Err(TypeError::FixedArraySize(
441                             expected_found(relation, &sz_a_val, &sz_b_val),
442                         )),
443                         _ => return Err(err),
444                     }
445                 }
446             }
447         }
448
449         (&ty::Slice(a_t), &ty::Slice(b_t)) => {
450             let t = relation.relate(&a_t, &b_t)?;
451             Ok(tcx.mk_slice(t))
452         }
453
454         (&ty::Tuple(as_), &ty::Tuple(bs)) => {
455             if as_.len() == bs.len() {
456                 Ok(tcx.mk_tup(
457                     as_.iter()
458                         .zip(bs)
459                         .map(|(a, b)| relation.relate(&a.expect_ty(), &b.expect_ty())),
460                 )?)
461             } else if !(as_.is_empty() || bs.is_empty()) {
462                 Err(TypeError::TupleSize(expected_found(relation, &as_.len(), &bs.len())))
463             } else {
464                 Err(TypeError::Sorts(expected_found(relation, &a, &b)))
465             }
466         }
467
468         (&ty::FnDef(a_def_id, a_substs), &ty::FnDef(b_def_id, b_substs))
469             if a_def_id == b_def_id =>
470         {
471             let substs = relation.relate_item_substs(a_def_id, a_substs, b_substs)?;
472             Ok(tcx.mk_fn_def(a_def_id, substs))
473         }
474
475         (&ty::FnPtr(a_fty), &ty::FnPtr(b_fty)) => {
476             let fty = relation.relate(&a_fty, &b_fty)?;
477             Ok(tcx.mk_fn_ptr(fty))
478         }
479
480         (ty::UnnormalizedProjection(a_data), ty::UnnormalizedProjection(b_data)) => {
481             let projection_ty = relation.relate(a_data, b_data)?;
482             Ok(tcx.mk_ty(ty::UnnormalizedProjection(projection_ty)))
483         }
484
485         // these two are already handled downstream in case of lazy normalization
486         (ty::Projection(a_data), ty::Projection(b_data)) => {
487             let projection_ty = relation.relate(a_data, b_data)?;
488             Ok(tcx.mk_projection(projection_ty.item_def_id, projection_ty.substs))
489         }
490
491         (&ty::Opaque(a_def_id, a_substs), &ty::Opaque(b_def_id, b_substs))
492             if a_def_id == b_def_id =>
493         {
494             let substs = relate_substs(relation, None, a_substs, b_substs)?;
495             Ok(tcx.mk_opaque(a_def_id, substs))
496         }
497
498         _ => Err(TypeError::Sorts(expected_found(relation, &a, &b))),
499     }
500 }
501
502 /// The main "const relation" routine. Note that this does not handle
503 /// inference artifacts, so you should filter those out before calling
504 /// it.
505 pub fn super_relate_consts<R: TypeRelation<'tcx>>(
506     relation: &mut R,
507     a: &'tcx ty::Const<'tcx>,
508     b: &'tcx ty::Const<'tcx>,
509 ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
510     let tcx = relation.tcx();
511
512     let eagerly_eval = |x: &'tcx ty::Const<'tcx>| {
513         if !x.val.has_local_value() {
514             return x.eval(tcx, relation.param_env()).val;
515         }
516         x.val
517     };
518
519     // Currently, the values that can be unified are primitive types,
520     // and those that derive both `PartialEq` and `Eq`, corresponding
521     // to `structural_match` types.
522     let new_const_val = match (eagerly_eval(a), eagerly_eval(b)) {
523         (ty::ConstKind::Infer(_), _) | (_, ty::ConstKind::Infer(_)) => {
524             // The caller should handle these cases!
525             bug!("var types encountered in super_relate_consts: {:?} {:?}", a, b)
526         }
527         (ty::ConstKind::Param(a_p), ty::ConstKind::Param(b_p)) if a_p.index == b_p.index => {
528             return Ok(a);
529         }
530         (ty::ConstKind::Placeholder(p1), ty::ConstKind::Placeholder(p2)) if p1 == p2 => {
531             return Ok(a);
532         }
533         (ty::ConstKind::Value(a_val), ty::ConstKind::Value(b_val)) => {
534             let new_val = match (a_val, b_val) {
535                 (ConstValue::Scalar(a_val), ConstValue::Scalar(b_val)) if a.ty == b.ty => {
536                     if a_val == b_val {
537                         Ok(ConstValue::Scalar(a_val))
538                     } else if let ty::FnPtr(_) = a.ty.kind {
539                         let alloc_map = tcx.alloc_map.lock();
540                         let a_instance = alloc_map.unwrap_fn(a_val.assert_ptr().alloc_id);
541                         let b_instance = alloc_map.unwrap_fn(b_val.assert_ptr().alloc_id);
542                         if a_instance == b_instance {
543                             Ok(ConstValue::Scalar(a_val))
544                         } else {
545                             Err(TypeError::ConstMismatch(expected_found(relation, &a, &b)))
546                         }
547                     } else {
548                         Err(TypeError::ConstMismatch(expected_found(relation, &a, &b)))
549                     }
550                 }
551
552                 (a_val @ ConstValue::Slice { .. }, b_val @ ConstValue::Slice { .. }) => {
553                     let a_bytes = get_slice_bytes(&tcx, a_val);
554                     let b_bytes = get_slice_bytes(&tcx, b_val);
555                     if a_bytes == b_bytes {
556                         Ok(a_val)
557                     } else {
558                         Err(TypeError::ConstMismatch(expected_found(relation, &a, &b)))
559                     }
560                 }
561
562                 // FIXME(const_generics): handle `ConstValue::ByRef`.
563                 _ => Err(TypeError::ConstMismatch(expected_found(relation, &a, &b))),
564             };
565
566             new_val.map(ty::ConstKind::Value)
567         }
568
569         // FIXME(const_generics): this is wrong, as it is a projection
570         (
571             ty::ConstKind::Unevaluated(a_def_id, a_substs, a_promoted),
572             ty::ConstKind::Unevaluated(b_def_id, b_substs, b_promoted),
573         ) if a_def_id == b_def_id && a_promoted == b_promoted => {
574             let substs =
575                 relation.relate_with_variance(ty::Variance::Invariant, &a_substs, &b_substs)?;
576             Ok(ty::ConstKind::Unevaluated(a_def_id, &substs, a_promoted))
577         }
578         _ => Err(TypeError::ConstMismatch(expected_found(relation, &a, &b))),
579     };
580     new_const_val.map(|val| tcx.mk_const(ty::Const { val, ty: a.ty }))
581 }
582
583 impl<'tcx> Relate<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
584     fn relate<R: TypeRelation<'tcx>>(
585         relation: &mut R,
586         a: &Self,
587         b: &Self,
588     ) -> RelateResult<'tcx, Self> {
589         if a.len() != b.len() {
590             return Err(TypeError::ExistentialMismatch(expected_found(relation, a, b)));
591         }
592
593         let tcx = relation.tcx();
594         let v = a.iter().zip(b.iter()).map(|(ep_a, ep_b)| {
595             use crate::ty::ExistentialPredicate::*;
596             match (*ep_a, *ep_b) {
597                 (Trait(ref a), Trait(ref b)) => Ok(Trait(relation.relate(a, b)?)),
598                 (Projection(ref a), Projection(ref b)) => Ok(Projection(relation.relate(a, b)?)),
599                 (AutoTrait(ref a), AutoTrait(ref b)) if a == b => Ok(AutoTrait(*a)),
600                 _ => Err(TypeError::ExistentialMismatch(expected_found(relation, a, b))),
601             }
602         });
603         Ok(tcx.mk_existential_predicates(v)?)
604     }
605 }
606
607 impl<'tcx> Relate<'tcx> for ty::ClosureSubsts<'tcx> {
608     fn relate<R: TypeRelation<'tcx>>(
609         relation: &mut R,
610         a: &ty::ClosureSubsts<'tcx>,
611         b: &ty::ClosureSubsts<'tcx>,
612     ) -> RelateResult<'tcx, ty::ClosureSubsts<'tcx>> {
613         let substs = relate_substs(relation, None, a.substs, b.substs)?;
614         Ok(ty::ClosureSubsts { substs })
615     }
616 }
617
618 impl<'tcx> Relate<'tcx> for ty::GeneratorSubsts<'tcx> {
619     fn relate<R: TypeRelation<'tcx>>(
620         relation: &mut R,
621         a: &ty::GeneratorSubsts<'tcx>,
622         b: &ty::GeneratorSubsts<'tcx>,
623     ) -> RelateResult<'tcx, ty::GeneratorSubsts<'tcx>> {
624         let substs = relate_substs(relation, None, a.substs, b.substs)?;
625         Ok(ty::GeneratorSubsts { substs })
626     }
627 }
628
629 impl<'tcx> Relate<'tcx> for SubstsRef<'tcx> {
630     fn relate<R: TypeRelation<'tcx>>(
631         relation: &mut R,
632         a: &SubstsRef<'tcx>,
633         b: &SubstsRef<'tcx>,
634     ) -> RelateResult<'tcx, SubstsRef<'tcx>> {
635         relate_substs(relation, None, a, b)
636     }
637 }
638
639 impl<'tcx> Relate<'tcx> for ty::Region<'tcx> {
640     fn relate<R: TypeRelation<'tcx>>(
641         relation: &mut R,
642         a: &ty::Region<'tcx>,
643         b: &ty::Region<'tcx>,
644     ) -> RelateResult<'tcx, ty::Region<'tcx>> {
645         relation.regions(*a, *b)
646     }
647 }
648
649 impl<'tcx> Relate<'tcx> for &'tcx ty::Const<'tcx> {
650     fn relate<R: TypeRelation<'tcx>>(
651         relation: &mut R,
652         a: &&'tcx ty::Const<'tcx>,
653         b: &&'tcx ty::Const<'tcx>,
654     ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
655         relation.consts(*a, *b)
656     }
657 }
658
659 impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for ty::Binder<T> {
660     fn relate<R: TypeRelation<'tcx>>(
661         relation: &mut R,
662         a: &ty::Binder<T>,
663         b: &ty::Binder<T>,
664     ) -> RelateResult<'tcx, ty::Binder<T>> {
665         relation.binders(a, b)
666     }
667 }
668
669 impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for Rc<T> {
670     fn relate<R: TypeRelation<'tcx>>(
671         relation: &mut R,
672         a: &Rc<T>,
673         b: &Rc<T>,
674     ) -> RelateResult<'tcx, Rc<T>> {
675         let a: &T = a;
676         let b: &T = b;
677         Ok(Rc::new(relation.relate(a, b)?))
678     }
679 }
680
681 impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for Box<T> {
682     fn relate<R: TypeRelation<'tcx>>(
683         relation: &mut R,
684         a: &Box<T>,
685         b: &Box<T>,
686     ) -> RelateResult<'tcx, Box<T>> {
687         let a: &T = a;
688         let b: &T = b;
689         Ok(Box::new(relation.relate(a, b)?))
690     }
691 }
692
693 impl<'tcx> Relate<'tcx> for GenericArg<'tcx> {
694     fn relate<R: TypeRelation<'tcx>>(
695         relation: &mut R,
696         a: &GenericArg<'tcx>,
697         b: &GenericArg<'tcx>,
698     ) -> RelateResult<'tcx, GenericArg<'tcx>> {
699         match (a.unpack(), b.unpack()) {
700             (GenericArgKind::Lifetime(a_lt), GenericArgKind::Lifetime(b_lt)) => {
701                 Ok(relation.relate(&a_lt, &b_lt)?.into())
702             }
703             (GenericArgKind::Type(a_ty), GenericArgKind::Type(b_ty)) => {
704                 Ok(relation.relate(&a_ty, &b_ty)?.into())
705             }
706             (GenericArgKind::Const(a_ct), GenericArgKind::Const(b_ct)) => {
707                 Ok(relation.relate(&a_ct, &b_ct)?.into())
708             }
709             (GenericArgKind::Lifetime(unpacked), x) => {
710                 bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x)
711             }
712             (GenericArgKind::Type(unpacked), x) => {
713                 bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x)
714             }
715             (GenericArgKind::Const(unpacked), x) => {
716                 bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x)
717             }
718         }
719     }
720 }
721
722 impl<'tcx> Relate<'tcx> for ty::TraitPredicate<'tcx> {
723     fn relate<R: TypeRelation<'tcx>>(
724         relation: &mut R,
725         a: &ty::TraitPredicate<'tcx>,
726         b: &ty::TraitPredicate<'tcx>,
727     ) -> RelateResult<'tcx, ty::TraitPredicate<'tcx>> {
728         Ok(ty::TraitPredicate { trait_ref: relation.relate(&a.trait_ref, &b.trait_ref)? })
729     }
730 }
731
732 impl<'tcx> Relate<'tcx> for ty::ProjectionPredicate<'tcx> {
733     fn relate<R: TypeRelation<'tcx>>(
734         relation: &mut R,
735         a: &ty::ProjectionPredicate<'tcx>,
736         b: &ty::ProjectionPredicate<'tcx>,
737     ) -> RelateResult<'tcx, ty::ProjectionPredicate<'tcx>> {
738         Ok(ty::ProjectionPredicate {
739             projection_ty: relation.relate(&a.projection_ty, &b.projection_ty)?,
740             ty: relation.relate(&a.ty, &b.ty)?,
741         })
742     }
743 }
744
745 impl<'tcx> Relate<'tcx> for traits::WhereClause<'tcx> {
746     fn relate<R: TypeRelation<'tcx>>(
747         relation: &mut R,
748         a: &traits::WhereClause<'tcx>,
749         b: &traits::WhereClause<'tcx>,
750     ) -> RelateResult<'tcx, traits::WhereClause<'tcx>> {
751         use crate::traits::WhereClause::*;
752         match (a, b) {
753             (Implemented(a_pred), Implemented(b_pred)) => {
754                 Ok(Implemented(relation.relate(a_pred, b_pred)?))
755             }
756
757             (ProjectionEq(a_pred), ProjectionEq(b_pred)) => {
758                 Ok(ProjectionEq(relation.relate(a_pred, b_pred)?))
759             }
760
761             (RegionOutlives(a_pred), RegionOutlives(b_pred)) => {
762                 Ok(RegionOutlives(ty::OutlivesPredicate(
763                     relation.relate(&a_pred.0, &b_pred.0)?,
764                     relation.relate(&a_pred.1, &b_pred.1)?,
765                 )))
766             }
767
768             (TypeOutlives(a_pred), TypeOutlives(b_pred)) => {
769                 Ok(TypeOutlives(ty::OutlivesPredicate(
770                     relation.relate(&a_pred.0, &b_pred.0)?,
771                     relation.relate(&a_pred.1, &b_pred.1)?,
772                 )))
773             }
774
775             _ => Err(TypeError::Mismatch),
776         }
777     }
778 }
779
780 impl<'tcx> Relate<'tcx> for traits::WellFormed<'tcx> {
781     fn relate<R: TypeRelation<'tcx>>(
782         relation: &mut R,
783         a: &traits::WellFormed<'tcx>,
784         b: &traits::WellFormed<'tcx>,
785     ) -> RelateResult<'tcx, traits::WellFormed<'tcx>> {
786         use crate::traits::WellFormed::*;
787         match (a, b) {
788             (Trait(a_pred), Trait(b_pred)) => Ok(Trait(relation.relate(a_pred, b_pred)?)),
789             (Ty(a_ty), Ty(b_ty)) => Ok(Ty(relation.relate(a_ty, b_ty)?)),
790             _ => Err(TypeError::Mismatch),
791         }
792     }
793 }
794
795 impl<'tcx> Relate<'tcx> for traits::FromEnv<'tcx> {
796     fn relate<R: TypeRelation<'tcx>>(
797         relation: &mut R,
798         a: &traits::FromEnv<'tcx>,
799         b: &traits::FromEnv<'tcx>,
800     ) -> RelateResult<'tcx, traits::FromEnv<'tcx>> {
801         use crate::traits::FromEnv::*;
802         match (a, b) {
803             (Trait(a_pred), Trait(b_pred)) => Ok(Trait(relation.relate(a_pred, b_pred)?)),
804             (Ty(a_ty), Ty(b_ty)) => Ok(Ty(relation.relate(a_ty, b_ty)?)),
805             _ => Err(TypeError::Mismatch),
806         }
807     }
808 }
809
810 impl<'tcx> Relate<'tcx> for traits::DomainGoal<'tcx> {
811     fn relate<R: TypeRelation<'tcx>>(
812         relation: &mut R,
813         a: &traits::DomainGoal<'tcx>,
814         b: &traits::DomainGoal<'tcx>,
815     ) -> RelateResult<'tcx, traits::DomainGoal<'tcx>> {
816         use crate::traits::DomainGoal::*;
817         match (a, b) {
818             (Holds(a_wc), Holds(b_wc)) => Ok(Holds(relation.relate(a_wc, b_wc)?)),
819             (WellFormed(a_wf), WellFormed(b_wf)) => Ok(WellFormed(relation.relate(a_wf, b_wf)?)),
820             (FromEnv(a_fe), FromEnv(b_fe)) => Ok(FromEnv(relation.relate(a_fe, b_fe)?)),
821
822             (Normalize(a_pred), Normalize(b_pred)) => {
823                 Ok(Normalize(relation.relate(a_pred, b_pred)?))
824             }
825
826             _ => Err(TypeError::Mismatch),
827         }
828     }
829 }
830
831 impl<'tcx> Relate<'tcx> for traits::Goal<'tcx> {
832     fn relate<R: TypeRelation<'tcx>>(
833         relation: &mut R,
834         a: &traits::Goal<'tcx>,
835         b: &traits::Goal<'tcx>,
836     ) -> RelateResult<'tcx, traits::Goal<'tcx>> {
837         use crate::traits::GoalKind::*;
838         match (a, b) {
839             (Implies(a_clauses, a_goal), Implies(b_clauses, b_goal)) => {
840                 let clauses = relation.relate(a_clauses, b_clauses)?;
841                 let goal = relation.relate(a_goal, b_goal)?;
842                 Ok(relation.tcx().mk_goal(Implies(clauses, goal)))
843             }
844
845             (And(a_left, a_right), And(b_left, b_right)) => {
846                 let left = relation.relate(a_left, b_left)?;
847                 let right = relation.relate(a_right, b_right)?;
848                 Ok(relation.tcx().mk_goal(And(left, right)))
849             }
850
851             (Not(a_goal), Not(b_goal)) => {
852                 let goal = relation.relate(a_goal, b_goal)?;
853                 Ok(relation.tcx().mk_goal(Not(goal)))
854             }
855
856             (DomainGoal(a_goal), DomainGoal(b_goal)) => {
857                 let goal = relation.relate(a_goal, b_goal)?;
858                 Ok(relation.tcx().mk_goal(DomainGoal(goal)))
859             }
860
861             (Quantified(a_qkind, a_goal), Quantified(b_qkind, b_goal)) if a_qkind == b_qkind => {
862                 let goal = relation.relate(a_goal, b_goal)?;
863                 Ok(relation.tcx().mk_goal(Quantified(*a_qkind, goal)))
864             }
865
866             (CannotProve, CannotProve) => Ok(*a),
867
868             _ => Err(TypeError::Mismatch),
869         }
870     }
871 }
872
873 impl<'tcx> Relate<'tcx> for traits::Goals<'tcx> {
874     fn relate<R: TypeRelation<'tcx>>(
875         relation: &mut R,
876         a: &traits::Goals<'tcx>,
877         b: &traits::Goals<'tcx>,
878     ) -> RelateResult<'tcx, traits::Goals<'tcx>> {
879         if a.len() != b.len() {
880             return Err(TypeError::Mismatch);
881         }
882
883         let tcx = relation.tcx();
884         let goals = a.iter().zip(b.iter()).map(|(a, b)| relation.relate(a, b));
885         Ok(tcx.mk_goals(goals)?)
886     }
887 }
888
889 impl<'tcx> Relate<'tcx> for traits::Clause<'tcx> {
890     fn relate<R: TypeRelation<'tcx>>(
891         relation: &mut R,
892         a: &traits::Clause<'tcx>,
893         b: &traits::Clause<'tcx>,
894     ) -> RelateResult<'tcx, traits::Clause<'tcx>> {
895         use crate::traits::Clause::*;
896         match (a, b) {
897             (Implies(a_clause), Implies(b_clause)) => {
898                 let clause = relation.relate(a_clause, b_clause)?;
899                 Ok(Implies(clause))
900             }
901
902             (ForAll(a_clause), ForAll(b_clause)) => {
903                 let clause = relation.relate(a_clause, b_clause)?;
904                 Ok(ForAll(clause))
905             }
906
907             _ => Err(TypeError::Mismatch),
908         }
909     }
910 }
911
912 impl<'tcx> Relate<'tcx> for traits::Clauses<'tcx> {
913     fn relate<R: TypeRelation<'tcx>>(
914         relation: &mut R,
915         a: &traits::Clauses<'tcx>,
916         b: &traits::Clauses<'tcx>,
917     ) -> RelateResult<'tcx, traits::Clauses<'tcx>> {
918         if a.len() != b.len() {
919             return Err(TypeError::Mismatch);
920         }
921
922         let tcx = relation.tcx();
923         let clauses = a.iter().zip(b.iter()).map(|(a, b)| relation.relate(a, b));
924         Ok(tcx.mk_clauses(clauses)?)
925     }
926 }
927
928 impl<'tcx> Relate<'tcx> for traits::ProgramClause<'tcx> {
929     fn relate<R: TypeRelation<'tcx>>(
930         relation: &mut R,
931         a: &traits::ProgramClause<'tcx>,
932         b: &traits::ProgramClause<'tcx>,
933     ) -> RelateResult<'tcx, traits::ProgramClause<'tcx>> {
934         Ok(traits::ProgramClause {
935             goal: relation.relate(&a.goal, &b.goal)?,
936             hypotheses: relation.relate(&a.hypotheses, &b.hypotheses)?,
937             category: traits::ProgramClauseCategory::Other,
938         })
939     }
940 }
941
942 impl<'tcx> Relate<'tcx> for traits::Environment<'tcx> {
943     fn relate<R: TypeRelation<'tcx>>(
944         relation: &mut R,
945         a: &traits::Environment<'tcx>,
946         b: &traits::Environment<'tcx>,
947     ) -> RelateResult<'tcx, traits::Environment<'tcx>> {
948         Ok(traits::Environment { clauses: relation.relate(&a.clauses, &b.clauses)? })
949     }
950 }
951
952 impl<'tcx, G> Relate<'tcx> for traits::InEnvironment<'tcx, G>
953 where
954     G: Relate<'tcx>,
955 {
956     fn relate<R: TypeRelation<'tcx>>(
957         relation: &mut R,
958         a: &traits::InEnvironment<'tcx, G>,
959         b: &traits::InEnvironment<'tcx, G>,
960     ) -> RelateResult<'tcx, traits::InEnvironment<'tcx, G>> {
961         Ok(traits::InEnvironment {
962             environment: relation.relate(&a.environment, &b.environment)?,
963             goal: relation.relate(&a.goal, &b.goal)?,
964         })
965     }
966 }
967
968 ///////////////////////////////////////////////////////////////////////////
969 // Error handling
970
971 pub fn expected_found<R, T>(relation: &mut R, a: &T, b: &T) -> ExpectedFound<T>
972 where
973     R: TypeRelation<'tcx>,
974     T: Clone,
975 {
976     expected_found_bool(relation.a_is_expected(), a, b)
977 }
978
979 pub fn expected_found_bool<T>(a_is_expected: bool, a: &T, b: &T) -> ExpectedFound<T>
980 where
981     T: Clone,
982 {
983     let a = a.clone();
984     let b = b.clone();
985     if a_is_expected {
986         ExpectedFound { expected: a, found: b }
987     } else {
988         ExpectedFound { expected: b, found: a }
989     }
990 }