1 //! Generalized type relating mechanism.
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.
7 use crate::hir::def_id::DefId;
8 use crate::ty::subst::{Kind, UnpackedKind, SubstsRef};
9 use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
10 use crate::ty::error::{ExpectedFound, TypeError};
11 use crate::mir::interpret::GlobalId;
12 use crate::util::common::ErrorReported;
13 use syntax_pos::DUMMY_SP;
16 use rustc_target::spec::abi;
17 use crate::hir as ast;
20 pub type RelateResult<'tcx, T> = Result<T, TypeError<'tcx>>;
22 #[derive(Clone, Debug)]
24 ExistentialRegionBound, // relating an existential region bound
27 pub trait TypeRelation<'a, 'gcx: 'a+'tcx, 'tcx: 'a> : Sized {
28 fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx>;
30 /// Returns a static string we can use for printouts.
31 fn tag(&self) -> &'static str;
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;
37 fn with_cause<F,R>(&mut self, _cause: Cause, f: F) -> R
38 where F: FnOnce(&mut Self) -> R
43 /// Generic relation routine suitable for most anything.
44 fn relate<T: Relate<'tcx>>(&mut self, a: &T, b: &T) -> RelateResult<'tcx, T> {
45 Relate::relate(self, a, b)
48 /// Relate the two substitutions for the given item. The default
49 /// is to look up the variance for the item and proceed
51 fn relate_item_substs(&mut self,
53 a_subst: SubstsRef<'tcx>,
54 b_subst: SubstsRef<'tcx>)
55 -> RelateResult<'tcx, SubstsRef<'tcx>>
57 debug!("relate_item_substs(item_def_id={:?}, a_subst={:?}, b_subst={:?})",
62 let opt_variances = self.tcx().variances_of(item_def_id);
63 relate_substs(self, Some(&opt_variances), a_subst, b_subst)
66 /// Switch variance for the purpose of relating `a` and `b`.
67 fn relate_with_variance<T: Relate<'tcx>>(&mut self,
68 variance: ty::Variance,
71 -> RelateResult<'tcx, T>;
73 // Overrideable relations. You shouldn't typically call these
74 // directly, instead call `relate()`, which in turn calls
75 // these. This is both more uniform but also allows us to add
76 // additional hooks for other types in the future if needed
77 // without making older code, which called `relate`, obsolete.
79 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>)
80 -> RelateResult<'tcx, Ty<'tcx>>;
82 fn regions(&mut self, a: ty::Region<'tcx>, b: ty::Region<'tcx>)
83 -> RelateResult<'tcx, ty::Region<'tcx>>;
85 fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
86 -> RelateResult<'tcx, ty::Binder<T>>
87 where T: Relate<'tcx>;
90 pub trait Relate<'tcx>: TypeFoldable<'tcx> {
91 fn relate<'a, 'gcx, R>(relation: &mut R, a: &Self, b: &Self)
92 -> RelateResult<'tcx, Self>
93 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a;
96 ///////////////////////////////////////////////////////////////////////////
99 impl<'tcx> Relate<'tcx> for ty::TypeAndMut<'tcx> {
100 fn relate<'a, 'gcx, R>(relation: &mut R,
101 a: &ty::TypeAndMut<'tcx>,
102 b: &ty::TypeAndMut<'tcx>)
103 -> RelateResult<'tcx, ty::TypeAndMut<'tcx>>
104 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
106 debug!("{}.mts({:?}, {:?})",
110 if a.mutbl != b.mutbl {
111 Err(TypeError::Mutability)
114 let variance = match mutbl {
115 ast::Mutability::MutImmutable => ty::Covariant,
116 ast::Mutability::MutMutable => ty::Invariant,
118 let ty = relation.relate_with_variance(variance, &a.ty, &b.ty)?;
119 Ok(ty::TypeAndMut {ty: ty, mutbl: mutbl})
124 pub fn relate_substs<'a, 'gcx, 'tcx, R>(relation: &mut R,
125 variances: Option<&Vec<ty::Variance>>,
126 a_subst: SubstsRef<'tcx>,
127 b_subst: SubstsRef<'tcx>)
128 -> RelateResult<'tcx, SubstsRef<'tcx>>
129 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
131 let tcx = relation.tcx();
133 let params = a_subst.iter().zip(b_subst).enumerate().map(|(i, (a, b))| {
134 let variance = variances.map_or(ty::Invariant, |v| v[i]);
135 relation.relate_with_variance(variance, a, b)
138 Ok(tcx.mk_substs(params)?)
141 impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> {
142 fn relate<'a, 'gcx, R>(relation: &mut R,
145 -> RelateResult<'tcx, ty::FnSig<'tcx>>
146 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
148 let tcx = relation.tcx();
150 if a.variadic != b.variadic {
151 return Err(TypeError::VariadicMismatch(
152 expected_found(relation, &a.variadic, &b.variadic)));
154 let unsafety = relation.relate(&a.unsafety, &b.unsafety)?;
155 let abi = relation.relate(&a.abi, &b.abi)?;
157 if a.inputs().len() != b.inputs().len() {
158 return Err(TypeError::ArgCount);
161 let inputs_and_output = a.inputs().iter().cloned()
162 .zip(b.inputs().iter().cloned())
164 .chain(iter::once(((a.output(), b.output()), true)))
165 .map(|((a, b), is_output)| {
167 relation.relate(&a, &b)
169 relation.relate_with_variance(ty::Contravariant, &a, &b)
173 inputs_and_output: tcx.mk_type_list(inputs_and_output)?,
174 variadic: a.variadic,
181 impl<'tcx> Relate<'tcx> for ast::Unsafety {
182 fn relate<'a, 'gcx, R>(relation: &mut R,
185 -> RelateResult<'tcx, ast::Unsafety>
186 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
189 Err(TypeError::UnsafetyMismatch(expected_found(relation, a, b)))
196 impl<'tcx> Relate<'tcx> for abi::Abi {
197 fn relate<'a, 'gcx, R>(relation: &mut R,
200 -> RelateResult<'tcx, abi::Abi>
201 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
206 Err(TypeError::AbiMismatch(expected_found(relation, a, b)))
211 impl<'tcx> Relate<'tcx> for ty::ProjectionTy<'tcx> {
212 fn relate<'a, 'gcx, R>(relation: &mut R,
213 a: &ty::ProjectionTy<'tcx>,
214 b: &ty::ProjectionTy<'tcx>)
215 -> RelateResult<'tcx, ty::ProjectionTy<'tcx>>
216 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
218 if a.item_def_id != b.item_def_id {
219 Err(TypeError::ProjectionMismatched(
220 expected_found(relation, &a.item_def_id, &b.item_def_id)))
222 let substs = relation.relate(&a.substs, &b.substs)?;
223 Ok(ty::ProjectionTy {
224 item_def_id: a.item_def_id,
231 impl<'tcx> Relate<'tcx> for ty::ExistentialProjection<'tcx> {
232 fn relate<'a, 'gcx, R>(relation: &mut R,
233 a: &ty::ExistentialProjection<'tcx>,
234 b: &ty::ExistentialProjection<'tcx>)
235 -> RelateResult<'tcx, ty::ExistentialProjection<'tcx>>
236 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
238 if a.item_def_id != b.item_def_id {
239 Err(TypeError::ProjectionMismatched(
240 expected_found(relation, &a.item_def_id, &b.item_def_id)))
242 let ty = relation.relate(&a.ty, &b.ty)?;
243 let substs = relation.relate(&a.substs, &b.substs)?;
244 Ok(ty::ExistentialProjection {
245 item_def_id: a.item_def_id,
253 impl<'tcx> Relate<'tcx> for Vec<ty::PolyExistentialProjection<'tcx>> {
254 fn relate<'a, 'gcx, R>(relation: &mut R,
255 a: &Vec<ty::PolyExistentialProjection<'tcx>>,
256 b: &Vec<ty::PolyExistentialProjection<'tcx>>)
257 -> RelateResult<'tcx, Vec<ty::PolyExistentialProjection<'tcx>>>
258 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
260 // To be compatible, `a` and `b` must be for precisely the
261 // same set of traits and item names. We always require that
262 // projection bounds lists are sorted by trait-def-id and item-name,
263 // so we can just iterate through the lists pairwise, so long as they are the
265 if a.len() != b.len() {
266 Err(TypeError::ProjectionBoundsLength(expected_found(relation, &a.len(), &b.len())))
270 .map(|(a, b)| relation.relate(a, b))
276 impl<'tcx> Relate<'tcx> for ty::TraitRef<'tcx> {
277 fn relate<'a, 'gcx, R>(relation: &mut R,
278 a: &ty::TraitRef<'tcx>,
279 b: &ty::TraitRef<'tcx>)
280 -> RelateResult<'tcx, ty::TraitRef<'tcx>>
281 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
283 // Different traits cannot be related
284 if a.def_id != b.def_id {
285 Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
287 let substs = relate_substs(relation, None, a.substs, b.substs)?;
288 Ok(ty::TraitRef { def_id: a.def_id, substs: substs })
293 impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
294 fn relate<'a, 'gcx, R>(relation: &mut R,
295 a: &ty::ExistentialTraitRef<'tcx>,
296 b: &ty::ExistentialTraitRef<'tcx>)
297 -> RelateResult<'tcx, ty::ExistentialTraitRef<'tcx>>
298 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
300 // Different traits cannot be related
301 if a.def_id != b.def_id {
302 Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
304 let substs = relate_substs(relation, None, a.substs, b.substs)?;
305 Ok(ty::ExistentialTraitRef { def_id: a.def_id, substs: substs })
310 #[derive(Debug, Clone)]
311 struct GeneratorWitness<'tcx>(&'tcx ty::List<Ty<'tcx>>);
313 TupleStructTypeFoldableImpl! {
314 impl<'tcx> TypeFoldable<'tcx> for GeneratorWitness<'tcx> {
319 impl<'tcx> Relate<'tcx> for GeneratorWitness<'tcx> {
320 fn relate<'a, 'gcx, R>(relation: &mut R,
321 a: &GeneratorWitness<'tcx>,
322 b: &GeneratorWitness<'tcx>)
323 -> RelateResult<'tcx, GeneratorWitness<'tcx>>
324 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
326 assert_eq!(a.0.len(), b.0.len());
327 let tcx = relation.tcx();
328 let types = tcx.mk_type_list(a.0.iter().zip(b.0).map(|(a, b)| relation.relate(a, b)))?;
329 Ok(GeneratorWitness(types))
333 impl<'tcx> Relate<'tcx> for Ty<'tcx> {
334 fn relate<'a, 'gcx, R>(relation: &mut R,
337 -> RelateResult<'tcx, Ty<'tcx>>
338 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
344 /// The main "type relation" routine. Note that this does not handle
345 /// inference artifacts, so you should filter those out before calling
347 pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
350 -> RelateResult<'tcx, Ty<'tcx>>
351 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
353 let tcx = relation.tcx();
356 debug!("super_relate_tys: a_sty={:?} b_sty={:?}", a_sty, b_sty);
357 match (a_sty, b_sty) {
359 (_, &ty::Infer(_)) =>
361 // The caller should handle these cases!
362 bug!("var types encountered in super_relate_tys")
365 (ty::Bound(..), _) | (_, ty::Bound(..)) => {
366 bug!("bound types encountered in super_relate_tys")
369 (&ty::Error, _) | (_, &ty::Error) =>
386 (&ty::Param(ref a_p), &ty::Param(ref b_p))
387 if a_p.idx == b_p.idx =>
392 (ty::Placeholder(p1), ty::Placeholder(p2)) if p1 == p2 => {
396 (&ty::Adt(a_def, a_substs), &ty::Adt(b_def, b_substs))
399 let substs = relation.relate_item_substs(a_def.did, a_substs, b_substs)?;
400 Ok(tcx.mk_adt(a_def, substs))
403 (&ty::Foreign(a_id), &ty::Foreign(b_id))
406 Ok(tcx.mk_foreign(a_id))
409 (&ty::Dynamic(ref a_obj, ref a_region), &ty::Dynamic(ref b_obj, ref b_region)) => {
410 let region_bound = relation.with_cause(Cause::ExistentialRegionBound,
412 relation.relate_with_variance(
417 Ok(tcx.mk_dynamic(relation.relate(a_obj, b_obj)?, region_bound))
420 (&ty::Generator(a_id, a_substs, movability),
421 &ty::Generator(b_id, b_substs, _))
424 // All Generator types with the same id represent
425 // the (anonymous) type of the same generator expression. So
426 // all of their regions should be equated.
427 let substs = relation.relate(&a_substs, &b_substs)?;
428 Ok(tcx.mk_generator(a_id, substs, movability))
431 (&ty::GeneratorWitness(a_types), &ty::GeneratorWitness(b_types)) =>
433 // Wrap our types with a temporary GeneratorWitness struct
434 // inside the binder so we can related them
435 let a_types = a_types.map_bound(GeneratorWitness);
436 let b_types = b_types.map_bound(GeneratorWitness);
437 // Then remove the GeneratorWitness for the result
438 let types = relation.relate(&a_types, &b_types)?.map_bound(|witness| witness.0);
439 Ok(tcx.mk_generator_witness(types))
442 (&ty::Closure(a_id, a_substs),
443 &ty::Closure(b_id, b_substs))
446 // All Closure types with the same id represent
447 // the (anonymous) type of the same closure expression. So
448 // all of their regions should be equated.
449 let substs = relation.relate(&a_substs, &b_substs)?;
450 Ok(tcx.mk_closure(a_id, substs))
453 (&ty::RawPtr(ref a_mt), &ty::RawPtr(ref b_mt)) =>
455 let mt = relation.relate(a_mt, b_mt)?;
459 (&ty::Ref(a_r, a_ty, a_mutbl), &ty::Ref(b_r, b_ty, b_mutbl)) =>
461 let r = relation.relate_with_variance(ty::Contravariant, &a_r, &b_r)?;
462 let a_mt = ty::TypeAndMut { ty: a_ty, mutbl: a_mutbl };
463 let b_mt = ty::TypeAndMut { ty: b_ty, mutbl: b_mutbl };
464 let mt = relation.relate(&a_mt, &b_mt)?;
465 Ok(tcx.mk_ref(r, mt))
468 (&ty::Array(a_t, sz_a), &ty::Array(b_t, sz_b)) =>
470 let t = relation.relate(&a_t, &b_t)?;
471 let to_u64 = |x: ty::LazyConst<'tcx>| -> Result<u64, ErrorReported> {
473 ty::LazyConst::Unevaluated(def_id, substs) => {
474 // FIXME(eddyb) get the right param_env.
475 let param_env = ty::ParamEnv::empty();
476 if let Some(substs) = tcx.lift_to_global(&substs) {
477 let instance = ty::Instance::resolve(
483 if let Some(instance) = instance {
488 if let Some(s) = tcx.const_eval(param_env.and(cid))
490 .map(|c| c.unwrap_usize(tcx)) {
495 tcx.sess.delay_span_bug(tcx.def_span(def_id),
496 "array length could not be evaluated");
499 ty::LazyConst::Evaluated(c) => c.assert_usize(tcx).ok_or_else(|| {
500 tcx.sess.delay_span_bug(DUMMY_SP,
501 "array length could not be evaluated");
506 match (to_u64(*sz_a), to_u64(*sz_b)) {
507 (Ok(sz_a_u64), Ok(sz_b_u64)) => {
508 if sz_a_u64 == sz_b_u64 {
509 Ok(tcx.mk_ty(ty::Array(t, sz_a)))
511 Err(TypeError::FixedArraySize(
512 expected_found(relation, &sz_a_u64, &sz_b_u64)))
515 // We reported an error or will ICE, so we can return Error.
516 (Err(ErrorReported), _) | (_, Err(ErrorReported)) => {
522 (&ty::Slice(a_t), &ty::Slice(b_t)) =>
524 let t = relation.relate(&a_t, &b_t)?;
528 (&ty::Tuple(as_), &ty::Tuple(bs)) =>
530 if as_.len() == bs.len() {
531 Ok(tcx.mk_tup(as_.iter().zip(bs).map(|(a, b)| relation.relate(a, b)))?)
532 } else if !(as_.is_empty() || bs.is_empty()) {
533 Err(TypeError::TupleSize(
534 expected_found(relation, &as_.len(), &bs.len())))
536 Err(TypeError::Sorts(expected_found(relation, &a, &b)))
540 (&ty::FnDef(a_def_id, a_substs), &ty::FnDef(b_def_id, b_substs))
541 if a_def_id == b_def_id =>
543 let substs = relation.relate_item_substs(a_def_id, a_substs, b_substs)?;
544 Ok(tcx.mk_fn_def(a_def_id, substs))
547 (&ty::FnPtr(a_fty), &ty::FnPtr(b_fty)) =>
549 let fty = relation.relate(&a_fty, &b_fty)?;
550 Ok(tcx.mk_fn_ptr(fty))
553 (ty::UnnormalizedProjection(a_data), ty::UnnormalizedProjection(b_data)) => {
554 let projection_ty = relation.relate(a_data, b_data)?;
555 Ok(tcx.mk_ty(ty::UnnormalizedProjection(projection_ty)))
558 // these two are already handled downstream in case of lazy normalization
559 (ty::Projection(a_data), ty::Projection(b_data)) => {
560 let projection_ty = relation.relate(a_data, b_data)?;
561 Ok(tcx.mk_projection(projection_ty.item_def_id, projection_ty.substs))
564 (&ty::Opaque(a_def_id, a_substs), &ty::Opaque(b_def_id, b_substs))
565 if a_def_id == b_def_id =>
567 let substs = relate_substs(relation, None, a_substs, b_substs)?;
568 Ok(tcx.mk_opaque(a_def_id, substs))
573 Err(TypeError::Sorts(expected_found(relation, &a, &b)))
578 impl<'tcx> Relate<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
579 fn relate<'a, 'gcx, R>(relation: &mut R,
582 -> RelateResult<'tcx, Self>
583 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a {
585 if a.len() != b.len() {
586 return Err(TypeError::ExistentialMismatch(expected_found(relation, a, b)));
589 let tcx = relation.tcx();
590 let v = a.iter().zip(b.iter()).map(|(ep_a, ep_b)| {
591 use crate::ty::ExistentialPredicate::*;
592 match (*ep_a, *ep_b) {
593 (Trait(ref a), Trait(ref b)) => Ok(Trait(relation.relate(a, b)?)),
594 (Projection(ref a), Projection(ref b)) => Ok(Projection(relation.relate(a, b)?)),
595 (AutoTrait(ref a), AutoTrait(ref b)) if a == b => Ok(AutoTrait(*a)),
596 _ => Err(TypeError::ExistentialMismatch(expected_found(relation, a, b)))
599 Ok(tcx.mk_existential_predicates(v)?)
603 impl<'tcx> Relate<'tcx> for ty::ClosureSubsts<'tcx> {
604 fn relate<'a, 'gcx, R>(relation: &mut R,
605 a: &ty::ClosureSubsts<'tcx>,
606 b: &ty::ClosureSubsts<'tcx>)
607 -> RelateResult<'tcx, ty::ClosureSubsts<'tcx>>
608 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
610 let substs = relate_substs(relation, None, a.substs, b.substs)?;
611 Ok(ty::ClosureSubsts { substs })
615 impl<'tcx> Relate<'tcx> for ty::GeneratorSubsts<'tcx> {
616 fn relate<'a, 'gcx, R>(relation: &mut R,
617 a: &ty::GeneratorSubsts<'tcx>,
618 b: &ty::GeneratorSubsts<'tcx>)
619 -> RelateResult<'tcx, ty::GeneratorSubsts<'tcx>>
620 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
622 let substs = relate_substs(relation, None, a.substs, b.substs)?;
623 Ok(ty::GeneratorSubsts { substs })
627 impl<'tcx> Relate<'tcx> for SubstsRef<'tcx> {
628 fn relate<'a, 'gcx, R>(relation: &mut R,
631 -> RelateResult<'tcx, SubstsRef<'tcx>>
632 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
634 relate_substs(relation, None, a, b)
638 impl<'tcx> Relate<'tcx> for ty::Region<'tcx> {
639 fn relate<'a, 'gcx, R>(relation: &mut R,
640 a: &ty::Region<'tcx>,
641 b: &ty::Region<'tcx>)
642 -> RelateResult<'tcx, ty::Region<'tcx>>
643 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
645 relation.regions(*a, *b)
649 impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for ty::Binder<T> {
650 fn relate<'a, 'gcx, R>(relation: &mut R,
653 -> RelateResult<'tcx, ty::Binder<T>>
654 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
656 relation.binders(a, b)
660 impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for Rc<T> {
661 fn relate<'a, 'gcx, R>(relation: &mut R,
664 -> RelateResult<'tcx, Rc<T>>
665 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
669 Ok(Rc::new(relation.relate(a, b)?))
673 impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for Box<T> {
674 fn relate<'a, 'gcx, R>(relation: &mut R,
677 -> RelateResult<'tcx, Box<T>>
678 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
682 Ok(Box::new(relation.relate(a, b)?))
686 impl<'tcx> Relate<'tcx> for Kind<'tcx> {
687 fn relate<'a, 'gcx, R>(
691 ) -> RelateResult<'tcx, Kind<'tcx>>
693 R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a,
695 match (a.unpack(), b.unpack()) {
696 (UnpackedKind::Lifetime(a_lt), UnpackedKind::Lifetime(b_lt)) => {
697 Ok(relation.relate(&a_lt, &b_lt)?.into())
699 (UnpackedKind::Type(a_ty), UnpackedKind::Type(b_ty)) => {
700 Ok(relation.relate(&a_ty, &b_ty)?.into())
702 (UnpackedKind::Lifetime(unpacked), x) => {
703 bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x)
705 (UnpackedKind::Type(unpacked), x) => {
706 bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x)
712 impl<'tcx> Relate<'tcx> for ty::TraitPredicate<'tcx> {
713 fn relate<'a, 'gcx, R>(
715 a: &ty::TraitPredicate<'tcx>,
716 b: &ty::TraitPredicate<'tcx>
717 ) -> RelateResult<'tcx, ty::TraitPredicate<'tcx>>
718 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
720 Ok(ty::TraitPredicate {
721 trait_ref: relation.relate(&a.trait_ref, &b.trait_ref)?,
726 impl<'tcx> Relate<'tcx> for ty::ProjectionPredicate<'tcx> {
727 fn relate<'a, 'gcx, R>(
729 a: &ty::ProjectionPredicate<'tcx>,
730 b: &ty::ProjectionPredicate<'tcx>,
731 ) -> RelateResult<'tcx, ty::ProjectionPredicate<'tcx>>
732 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
734 Ok(ty::ProjectionPredicate {
735 projection_ty: relation.relate(&a.projection_ty, &b.projection_ty)?,
736 ty: relation.relate(&a.ty, &b.ty)?,
741 impl<'tcx> Relate<'tcx> for traits::WhereClause<'tcx> {
742 fn relate<'a, 'gcx, R>(
744 a: &traits::WhereClause<'tcx>,
745 b: &traits::WhereClause<'tcx>
746 ) -> RelateResult<'tcx, traits::WhereClause<'tcx>>
747 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
749 use crate::traits::WhereClause::*;
751 (Implemented(a_pred), Implemented(b_pred)) => {
752 Ok(Implemented(relation.relate(a_pred, b_pred)?))
755 (ProjectionEq(a_pred), ProjectionEq(b_pred)) => {
756 Ok(ProjectionEq(relation.relate(a_pred, b_pred)?))
759 (RegionOutlives(a_pred), RegionOutlives(b_pred)) => {
760 Ok(RegionOutlives(ty::OutlivesPredicate(
761 relation.relate(&a_pred.0, &b_pred.0)?,
762 relation.relate(&a_pred.1, &b_pred.1)?,
766 (TypeOutlives(a_pred), TypeOutlives(b_pred)) => {
767 Ok(TypeOutlives(ty::OutlivesPredicate(
768 relation.relate(&a_pred.0, &b_pred.0)?,
769 relation.relate(&a_pred.1, &b_pred.1)?,
773 _ => Err(TypeError::Mismatch),
778 impl<'tcx> Relate<'tcx> for traits::WellFormed<'tcx> {
779 fn relate<'a, 'gcx, R>(
781 a: &traits::WellFormed<'tcx>,
782 b: &traits::WellFormed<'tcx>
783 ) -> RelateResult<'tcx, traits::WellFormed<'tcx>>
784 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
786 use crate::traits::WellFormed::*;
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),
795 impl<'tcx> Relate<'tcx> for traits::FromEnv<'tcx> {
796 fn relate<'a, 'gcx, R>(
798 a: &traits::FromEnv<'tcx>,
799 b: &traits::FromEnv<'tcx>
800 ) -> RelateResult<'tcx, traits::FromEnv<'tcx>>
801 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
803 use crate::traits::FromEnv::*;
805 (Trait(a_pred), Trait(b_pred)) => Ok(Trait(relation.relate(a_pred, b_pred)?)),
806 (Ty(a_ty), Ty(b_ty)) => Ok(Ty(relation.relate(a_ty, b_ty)?)),
807 _ => Err(TypeError::Mismatch),
812 impl<'tcx> Relate<'tcx> for traits::DomainGoal<'tcx> {
813 fn relate<'a, 'gcx, R>(
815 a: &traits::DomainGoal<'tcx>,
816 b: &traits::DomainGoal<'tcx>
817 ) -> RelateResult<'tcx, traits::DomainGoal<'tcx>>
818 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
820 use crate::traits::DomainGoal::*;
822 (Holds(a_wc), Holds(b_wc)) => Ok(Holds(relation.relate(a_wc, b_wc)?)),
823 (WellFormed(a_wf), WellFormed(b_wf)) => Ok(WellFormed(relation.relate(a_wf, b_wf)?)),
824 (FromEnv(a_fe), FromEnv(b_fe)) => Ok(FromEnv(relation.relate(a_fe, b_fe)?)),
826 (Normalize(a_pred), Normalize(b_pred)) => {
827 Ok(Normalize(relation.relate(a_pred, b_pred)?))
830 _ => Err(TypeError::Mismatch),
835 impl<'tcx> Relate<'tcx> for traits::Goal<'tcx> {
836 fn relate<'a, 'gcx, R>(
838 a: &traits::Goal<'tcx>,
839 b: &traits::Goal<'tcx>
840 ) -> RelateResult<'tcx, traits::Goal<'tcx>>
841 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
843 use crate::traits::GoalKind::*;
845 (Implies(a_clauses, a_goal), Implies(b_clauses, b_goal)) => {
846 let clauses = relation.relate(a_clauses, b_clauses)?;
847 let goal = relation.relate(a_goal, b_goal)?;
848 Ok(relation.tcx().mk_goal(Implies(clauses, goal)))
851 (And(a_left, a_right), And(b_left, b_right)) => {
852 let left = relation.relate(a_left, b_left)?;
853 let right = relation.relate(a_right, b_right)?;
854 Ok(relation.tcx().mk_goal(And(left, right)))
857 (Not(a_goal), Not(b_goal)) => {
858 let goal = relation.relate(a_goal, b_goal)?;
859 Ok(relation.tcx().mk_goal(Not(goal)))
862 (DomainGoal(a_goal), DomainGoal(b_goal)) => {
863 let goal = relation.relate(a_goal, b_goal)?;
864 Ok(relation.tcx().mk_goal(DomainGoal(goal)))
867 (Quantified(a_qkind, a_goal), Quantified(b_qkind, b_goal))
868 if a_qkind == b_qkind =>
870 let goal = relation.relate(a_goal, b_goal)?;
871 Ok(relation.tcx().mk_goal(Quantified(*a_qkind, goal)))
874 (CannotProve, CannotProve) => Ok(*a),
876 _ => Err(TypeError::Mismatch),
881 impl<'tcx> Relate<'tcx> for traits::Goals<'tcx> {
882 fn relate<'a, 'gcx, R>(
884 a: &traits::Goals<'tcx>,
885 b: &traits::Goals<'tcx>
886 ) -> RelateResult<'tcx, traits::Goals<'tcx>>
887 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
889 if a.len() != b.len() {
890 return Err(TypeError::Mismatch);
893 let tcx = relation.tcx();
894 let goals = a.iter().zip(b.iter()).map(|(a, b)| relation.relate(a, b));
895 Ok(tcx.mk_goals(goals)?)
899 impl<'tcx> Relate<'tcx> for traits::Clause<'tcx> {
900 fn relate<'a, 'gcx, R>(
902 a: &traits::Clause<'tcx>,
903 b: &traits::Clause<'tcx>
904 ) -> RelateResult<'tcx, traits::Clause<'tcx>>
905 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
907 use crate::traits::Clause::*;
909 (Implies(a_clause), Implies(b_clause)) => {
910 let clause = relation.relate(a_clause, b_clause)?;
914 (ForAll(a_clause), ForAll(b_clause)) => {
915 let clause = relation.relate(a_clause, b_clause)?;
919 _ => Err(TypeError::Mismatch),
924 impl<'tcx> Relate<'tcx> for traits::Clauses<'tcx> {
925 fn relate<'a, 'gcx, R>(
927 a: &traits::Clauses<'tcx>,
928 b: &traits::Clauses<'tcx>
929 ) -> RelateResult<'tcx, traits::Clauses<'tcx>>
930 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
932 if a.len() != b.len() {
933 return Err(TypeError::Mismatch);
936 let tcx = relation.tcx();
937 let clauses = a.iter().zip(b.iter()).map(|(a, b)| relation.relate(a, b));
938 Ok(tcx.mk_clauses(clauses)?)
942 impl<'tcx> Relate<'tcx> for traits::ProgramClause<'tcx> {
943 fn relate<'a, 'gcx, R>(
945 a: &traits::ProgramClause<'tcx>,
946 b: &traits::ProgramClause<'tcx>
947 ) -> RelateResult<'tcx, traits::ProgramClause<'tcx>>
948 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
950 Ok(traits::ProgramClause {
951 goal: relation.relate(&a.goal, &b.goal)?,
952 hypotheses: relation.relate(&a.hypotheses, &b.hypotheses)?,
953 category: traits::ProgramClauseCategory::Other,
958 impl<'tcx> Relate<'tcx> for traits::Environment<'tcx> {
959 fn relate<'a, 'gcx, R>(
961 a: &traits::Environment<'tcx>,
962 b: &traits::Environment<'tcx>
963 ) -> RelateResult<'tcx, traits::Environment<'tcx>>
964 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
966 Ok(traits::Environment {
967 clauses: relation.relate(&a.clauses, &b.clauses)?,
972 impl<'tcx, G> Relate<'tcx> for traits::InEnvironment<'tcx, G>
973 where G: Relate<'tcx>
975 fn relate<'a, 'gcx, R>(
977 a: &traits::InEnvironment<'tcx, G>,
978 b: &traits::InEnvironment<'tcx, G>
979 ) -> RelateResult<'tcx, traits::InEnvironment<'tcx, G>>
980 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
982 Ok(traits::InEnvironment {
983 environment: relation.relate(&a.environment, &b.environment)?,
984 goal: relation.relate(&a.goal, &b.goal)?,
989 ///////////////////////////////////////////////////////////////////////////
992 pub fn expected_found<'a, 'gcx, 'tcx, R, T>(relation: &mut R,
996 where R: TypeRelation<'a, 'gcx, 'tcx>, T: Clone, 'gcx: 'a+'tcx, 'tcx: 'a
998 expected_found_bool(relation.a_is_expected(), a, b)
1001 pub fn expected_found_bool<T>(a_is_expected: bool,
1010 ExpectedFound {expected: a, found: b}
1012 ExpectedFound {expected: b, found: a}