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::{ConstValue, Scalar, GlobalId};
14 use rustc_target::spec::abi;
15 use crate::hir as ast;
18 pub type RelateResult<'tcx, T> = Result<T, TypeError<'tcx>>;
20 #[derive(Clone, Debug)]
22 ExistentialRegionBound, // relating an existential region bound
25 pub trait TypeRelation<'gcx: 'tcx, 'tcx> : Sized {
26 fn tcx(&self) -> TyCtxt<'gcx, 'tcx>;
28 /// Returns a static string we can use for printouts.
29 fn tag(&self) -> &'static str;
31 /// Returns `true` if the value `a` is the "expected" type in the
32 /// relation. Just affects error messages.
33 fn a_is_expected(&self) -> bool;
35 fn with_cause<F,R>(&mut self, _cause: Cause, f: F) -> R
36 where F: FnOnce(&mut Self) -> R
41 /// Generic relation routine suitable for most anything.
42 fn relate<T: Relate<'tcx>>(&mut self, a: &T, b: &T) -> RelateResult<'tcx, T> {
43 Relate::relate(self, a, b)
46 /// Relate the two substitutions for the given item. The default
47 /// is to look up the variance for the item and proceed
49 fn relate_item_substs(&mut self,
51 a_subst: SubstsRef<'tcx>,
52 b_subst: SubstsRef<'tcx>)
53 -> RelateResult<'tcx, SubstsRef<'tcx>>
55 debug!("relate_item_substs(item_def_id={:?}, a_subst={:?}, b_subst={:?})",
60 let opt_variances = self.tcx().variances_of(item_def_id);
61 relate_substs(self, Some(opt_variances), a_subst, b_subst)
64 /// Switch variance for the purpose of relating `a` and `b`.
65 fn relate_with_variance<T: Relate<'tcx>>(&mut self,
66 variance: ty::Variance,
69 -> RelateResult<'tcx, T>;
71 // Overrideable relations. You shouldn't typically call these
72 // directly, instead call `relate()`, which in turn calls
73 // these. This is both more uniform but also allows us to add
74 // additional hooks for other types in the future if needed
75 // without making older code, which called `relate`, obsolete.
77 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>>;
83 ) -> RelateResult<'tcx, ty::Region<'tcx>>;
87 a: &'tcx ty::Const<'tcx>,
88 b: &'tcx ty::Const<'tcx>
89 ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>>;
91 fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
92 -> RelateResult<'tcx, ty::Binder<T>>
93 where T: Relate<'tcx>;
96 pub trait Relate<'tcx>: TypeFoldable<'tcx> {
97 fn relate<'a, 'gcx, R>(relation: &mut R, a: &Self, b: &Self)
98 -> RelateResult<'tcx, Self>
99 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a;
102 ///////////////////////////////////////////////////////////////////////////
105 impl<'tcx> Relate<'tcx> for ty::TypeAndMut<'tcx> {
106 fn relate<'a, 'gcx, R>(relation: &mut R,
107 a: &ty::TypeAndMut<'tcx>,
108 b: &ty::TypeAndMut<'tcx>)
109 -> RelateResult<'tcx, ty::TypeAndMut<'tcx>>
110 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
112 debug!("{}.mts({:?}, {:?})",
116 if a.mutbl != b.mutbl {
117 Err(TypeError::Mutability)
120 let variance = match mutbl {
121 ast::Mutability::MutImmutable => ty::Covariant,
122 ast::Mutability::MutMutable => ty::Invariant,
124 let ty = relation.relate_with_variance(variance, &a.ty, &b.ty)?;
125 Ok(ty::TypeAndMut { ty, mutbl })
130 pub fn relate_substs<'a, 'gcx, 'tcx, R>(relation: &mut R,
131 variances: Option<&[ty::Variance]>,
132 a_subst: SubstsRef<'tcx>,
133 b_subst: SubstsRef<'tcx>)
134 -> RelateResult<'tcx, SubstsRef<'tcx>>
135 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
137 let tcx = relation.tcx();
139 let params = a_subst.iter().zip(b_subst).enumerate().map(|(i, (a, b))| {
140 let variance = variances.map_or(ty::Invariant, |v| v[i]);
141 relation.relate_with_variance(variance, a, b)
144 Ok(tcx.mk_substs(params)?)
147 impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> {
148 fn relate<'a, 'gcx, R>(relation: &mut R,
151 -> RelateResult<'tcx, ty::FnSig<'tcx>>
152 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
154 let tcx = relation.tcx();
156 if a.c_variadic != b.c_variadic {
157 return Err(TypeError::VariadicMismatch(
158 expected_found(relation, &a.c_variadic, &b.c_variadic)));
160 let unsafety = relation.relate(&a.unsafety, &b.unsafety)?;
161 let abi = relation.relate(&a.abi, &b.abi)?;
163 if a.inputs().len() != b.inputs().len() {
164 return Err(TypeError::ArgCount);
167 let inputs_and_output = a.inputs().iter().cloned()
168 .zip(b.inputs().iter().cloned())
170 .chain(iter::once(((a.output(), b.output()), true)))
171 .map(|((a, b), is_output)| {
173 relation.relate(&a, &b)
175 relation.relate_with_variance(ty::Contravariant, &a, &b)
179 inputs_and_output: tcx.mk_type_list(inputs_and_output)?,
180 c_variadic: a.c_variadic,
187 impl<'tcx> Relate<'tcx> for ast::Unsafety {
188 fn relate<'a, 'gcx, R>(relation: &mut R,
191 -> RelateResult<'tcx, ast::Unsafety>
192 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
195 Err(TypeError::UnsafetyMismatch(expected_found(relation, a, b)))
202 impl<'tcx> Relate<'tcx> for abi::Abi {
203 fn relate<'a, 'gcx, R>(relation: &mut R,
206 -> RelateResult<'tcx, abi::Abi>
207 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
212 Err(TypeError::AbiMismatch(expected_found(relation, a, b)))
217 impl<'tcx> Relate<'tcx> for ty::ProjectionTy<'tcx> {
218 fn relate<'a, 'gcx, R>(relation: &mut R,
219 a: &ty::ProjectionTy<'tcx>,
220 b: &ty::ProjectionTy<'tcx>)
221 -> RelateResult<'tcx, ty::ProjectionTy<'tcx>>
222 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
224 if a.item_def_id != b.item_def_id {
225 Err(TypeError::ProjectionMismatched(
226 expected_found(relation, &a.item_def_id, &b.item_def_id)))
228 let substs = relation.relate(&a.substs, &b.substs)?;
229 Ok(ty::ProjectionTy {
230 item_def_id: a.item_def_id,
237 impl<'tcx> Relate<'tcx> for ty::ExistentialProjection<'tcx> {
238 fn relate<'a, 'gcx, R>(relation: &mut R,
239 a: &ty::ExistentialProjection<'tcx>,
240 b: &ty::ExistentialProjection<'tcx>)
241 -> RelateResult<'tcx, ty::ExistentialProjection<'tcx>>
242 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
244 if a.item_def_id != b.item_def_id {
245 Err(TypeError::ProjectionMismatched(
246 expected_found(relation, &a.item_def_id, &b.item_def_id)))
248 let ty = relation.relate(&a.ty, &b.ty)?;
249 let substs = relation.relate(&a.substs, &b.substs)?;
250 Ok(ty::ExistentialProjection {
251 item_def_id: a.item_def_id,
259 impl<'tcx> Relate<'tcx> for Vec<ty::PolyExistentialProjection<'tcx>> {
260 fn relate<'a, 'gcx, R>(relation: &mut R,
261 a: &Vec<ty::PolyExistentialProjection<'tcx>>,
262 b: &Vec<ty::PolyExistentialProjection<'tcx>>)
263 -> RelateResult<'tcx, Vec<ty::PolyExistentialProjection<'tcx>>>
264 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
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
271 if a.len() != b.len() {
272 Err(TypeError::ProjectionBoundsLength(expected_found(relation, &a.len(), &b.len())))
276 .map(|(a, b)| relation.relate(a, b))
282 impl<'tcx> Relate<'tcx> for ty::TraitRef<'tcx> {
283 fn relate<'a, 'gcx, R>(relation: &mut R,
284 a: &ty::TraitRef<'tcx>,
285 b: &ty::TraitRef<'tcx>)
286 -> RelateResult<'tcx, ty::TraitRef<'tcx>>
287 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
289 // Different traits cannot be related
290 if a.def_id != b.def_id {
291 Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
293 let substs = relate_substs(relation, None, a.substs, b.substs)?;
294 Ok(ty::TraitRef { def_id: a.def_id, substs: substs })
299 impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
300 fn relate<'a, 'gcx, R>(relation: &mut R,
301 a: &ty::ExistentialTraitRef<'tcx>,
302 b: &ty::ExistentialTraitRef<'tcx>)
303 -> RelateResult<'tcx, ty::ExistentialTraitRef<'tcx>>
304 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
306 // Different traits cannot be related
307 if a.def_id != b.def_id {
308 Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
310 let substs = relate_substs(relation, None, a.substs, b.substs)?;
311 Ok(ty::ExistentialTraitRef { def_id: a.def_id, substs: substs })
316 #[derive(Debug, Clone)]
317 struct GeneratorWitness<'tcx>(&'tcx ty::List<Ty<'tcx>>);
319 TupleStructTypeFoldableImpl! {
320 impl<'tcx> TypeFoldable<'tcx> for GeneratorWitness<'tcx> {
325 impl<'tcx> Relate<'tcx> for GeneratorWitness<'tcx> {
326 fn relate<'a, 'gcx, R>(relation: &mut R,
327 a: &GeneratorWitness<'tcx>,
328 b: &GeneratorWitness<'tcx>)
329 -> RelateResult<'tcx, GeneratorWitness<'tcx>>
330 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
332 assert_eq!(a.0.len(), b.0.len());
333 let tcx = relation.tcx();
334 let types = tcx.mk_type_list(a.0.iter().zip(b.0).map(|(a, b)| relation.relate(a, b)))?;
335 Ok(GeneratorWitness(types))
339 impl<'tcx> Relate<'tcx> for Ty<'tcx> {
340 fn relate<'a, 'gcx, R>(relation: &mut R,
343 -> RelateResult<'tcx, Ty<'tcx>>
344 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
350 /// The main "type relation" routine. Note that this does not handle
351 /// inference artifacts, so you should filter those out before calling
353 pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
356 -> RelateResult<'tcx, Ty<'tcx>>
357 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
359 let tcx = relation.tcx();
360 debug!("super_relate_tys: a={:?} b={:?}", a, b);
361 match (&a.sty, &b.sty) {
363 (_, &ty::Infer(_)) =>
365 // The caller should handle these cases!
366 bug!("var types encountered in super_relate_tys")
369 (ty::Bound(..), _) | (_, ty::Bound(..)) => {
370 bug!("bound types encountered in super_relate_tys")
373 (&ty::Error, _) | (_, &ty::Error) =>
390 (&ty::Param(ref a_p), &ty::Param(ref b_p))
391 if a_p.index == b_p.index =>
396 (ty::Placeholder(p1), ty::Placeholder(p2)) if p1 == p2 => {
400 (&ty::Adt(a_def, a_substs), &ty::Adt(b_def, b_substs))
403 let substs = relation.relate_item_substs(a_def.did, a_substs, b_substs)?;
404 Ok(tcx.mk_adt(a_def, substs))
407 (&ty::Foreign(a_id), &ty::Foreign(b_id))
410 Ok(tcx.mk_foreign(a_id))
413 (&ty::Dynamic(ref a_obj, ref a_region), &ty::Dynamic(ref b_obj, ref b_region)) => {
414 let region_bound = relation.with_cause(Cause::ExistentialRegionBound,
416 relation.relate_with_variance(
421 Ok(tcx.mk_dynamic(relation.relate(a_obj, b_obj)?, region_bound))
424 (&ty::Generator(a_id, a_substs, movability),
425 &ty::Generator(b_id, b_substs, _))
428 // All Generator types with the same id represent
429 // the (anonymous) type of the same generator expression. So
430 // all of their regions should be equated.
431 let substs = relation.relate(&a_substs, &b_substs)?;
432 Ok(tcx.mk_generator(a_id, substs, movability))
435 (&ty::GeneratorWitness(a_types), &ty::GeneratorWitness(b_types)) =>
437 // Wrap our types with a temporary GeneratorWitness struct
438 // inside the binder so we can related them
439 let a_types = a_types.map_bound(GeneratorWitness);
440 let b_types = b_types.map_bound(GeneratorWitness);
441 // Then remove the GeneratorWitness for the result
442 let types = relation.relate(&a_types, &b_types)?.map_bound(|witness| witness.0);
443 Ok(tcx.mk_generator_witness(types))
446 (&ty::Closure(a_id, a_substs),
447 &ty::Closure(b_id, b_substs))
450 // All Closure types with the same id represent
451 // the (anonymous) type of the same closure expression. So
452 // all of their regions should be equated.
453 let substs = relation.relate(&a_substs, &b_substs)?;
454 Ok(tcx.mk_closure(a_id, substs))
457 (&ty::RawPtr(ref a_mt), &ty::RawPtr(ref b_mt)) =>
459 let mt = relation.relate(a_mt, b_mt)?;
463 (&ty::Ref(a_r, a_ty, a_mutbl), &ty::Ref(b_r, b_ty, b_mutbl)) =>
465 let r = relation.relate_with_variance(ty::Contravariant, &a_r, &b_r)?;
466 let a_mt = ty::TypeAndMut { ty: a_ty, mutbl: a_mutbl };
467 let b_mt = ty::TypeAndMut { ty: b_ty, mutbl: b_mutbl };
468 let mt = relation.relate(&a_mt, &b_mt)?;
469 Ok(tcx.mk_ref(r, mt))
472 (&ty::Array(a_t, sz_a), &ty::Array(b_t, sz_b)) =>
474 let t = relation.relate(&a_t, &b_t)?;
475 match relation.relate(&sz_a, &sz_b) {
476 Ok(sz) => Ok(tcx.mk_ty(ty::Array(t, sz))),
478 // Check whether the lengths are both concrete/known values,
479 // but are unequal, for better diagnostics.
480 match (sz_a.assert_usize(tcx), sz_b.assert_usize(tcx)) {
481 (Some(sz_a_val), Some(sz_b_val)) => {
482 Err(TypeError::FixedArraySize(
483 expected_found(relation, &sz_a_val, &sz_b_val)
486 _ => return Err(err),
492 (&ty::Slice(a_t), &ty::Slice(b_t)) =>
494 let t = relation.relate(&a_t, &b_t)?;
498 (&ty::Tuple(as_), &ty::Tuple(bs)) =>
500 if as_.len() == bs.len() {
501 Ok(tcx.mk_tup(as_.iter().zip(bs).map(|(a, b)| {
502 relation.relate(&a.expect_ty(), &b.expect_ty())
504 } else if !(as_.is_empty() || bs.is_empty()) {
505 Err(TypeError::TupleSize(
506 expected_found(relation, &as_.len(), &bs.len())))
508 Err(TypeError::Sorts(expected_found(relation, &a, &b)))
512 (&ty::FnDef(a_def_id, a_substs), &ty::FnDef(b_def_id, b_substs))
513 if a_def_id == b_def_id =>
515 let substs = relation.relate_item_substs(a_def_id, a_substs, b_substs)?;
516 Ok(tcx.mk_fn_def(a_def_id, substs))
519 (&ty::FnPtr(a_fty), &ty::FnPtr(b_fty)) =>
521 let fty = relation.relate(&a_fty, &b_fty)?;
522 Ok(tcx.mk_fn_ptr(fty))
525 (ty::UnnormalizedProjection(a_data), ty::UnnormalizedProjection(b_data)) => {
526 let projection_ty = relation.relate(a_data, b_data)?;
527 Ok(tcx.mk_ty(ty::UnnormalizedProjection(projection_ty)))
530 // these two are already handled downstream in case of lazy normalization
531 (ty::Projection(a_data), ty::Projection(b_data)) => {
532 let projection_ty = relation.relate(a_data, b_data)?;
533 Ok(tcx.mk_projection(projection_ty.item_def_id, projection_ty.substs))
536 (&ty::Opaque(a_def_id, a_substs), &ty::Opaque(b_def_id, b_substs))
537 if a_def_id == b_def_id =>
539 let substs = relate_substs(relation, None, a_substs, b_substs)?;
540 Ok(tcx.mk_opaque(a_def_id, substs))
545 Err(TypeError::Sorts(expected_found(relation, &a, &b)))
550 /// The main "const relation" routine. Note that this does not handle
551 /// inference artifacts, so you should filter those out before calling
553 pub fn super_relate_consts<'a, 'gcx, 'tcx, R>(
555 a: &'tcx ty::Const<'tcx>,
556 b: &'tcx ty::Const<'tcx>
557 ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>>
559 R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
561 let tcx = relation.tcx();
563 let eagerly_eval = |x: &'tcx ty::Const<'tcx>| {
564 if let ConstValue::Unevaluated(def_id, substs) = x.val {
565 // FIXME(eddyb) get the right param_env.
566 let param_env = ty::ParamEnv::empty();
567 if let Some(substs) = tcx.lift_to_global(&substs) {
568 let instance = ty::Instance::resolve(
574 if let Some(instance) = instance {
579 if let Ok(ct) = tcx.const_eval(param_env.and(cid)) {
588 // Currently, the values that can be unified are those that
589 // implement both `PartialEq` and `Eq`, corresponding to
590 // `structural_match` types.
591 // FIXME(const_generics): check for `structural_match` synthetic attribute.
592 match (eagerly_eval(a), eagerly_eval(b)) {
593 (ConstValue::Infer(_), _) | (_, ConstValue::Infer(_)) => {
594 // The caller should handle these cases!
595 bug!("var types encountered in super_relate_consts: {:?} {:?}", a, b)
597 (ConstValue::Param(a_p), ConstValue::Param(b_p)) if a_p.index == b_p.index => {
600 (ConstValue::Placeholder(p1), ConstValue::Placeholder(p2)) if p1 == p2 => {
603 (a_val @ ConstValue::Scalar(Scalar::Raw { .. }), b_val @ _)
604 if a.ty == b.ty && a_val == b_val =>
606 Ok(tcx.mk_const(ty::Const {
611 (ConstValue::ByRef(..), _) => {
613 "non-Scalar ConstValue encountered in super_relate_consts {:?} {:?}",
619 // FIXME(const_generics): this is wrong, as it is a projection
620 (ConstValue::Unevaluated(a_def_id, a_substs),
621 ConstValue::Unevaluated(b_def_id, b_substs)) if a_def_id == b_def_id => {
623 relation.relate_with_variance(ty::Variance::Invariant, &a_substs, &b_substs)?;
624 Ok(tcx.mk_const(ty::Const {
625 val: ConstValue::Unevaluated(a_def_id, &substs),
630 _ => Err(TypeError::ConstMismatch(expected_found(relation, &a, &b))),
634 impl<'tcx> Relate<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
635 fn relate<'a, 'gcx, R>(relation: &mut R,
638 -> RelateResult<'tcx, Self>
639 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a {
641 if a.len() != b.len() {
642 return Err(TypeError::ExistentialMismatch(expected_found(relation, a, b)));
645 let tcx = relation.tcx();
646 let v = a.iter().zip(b.iter()).map(|(ep_a, ep_b)| {
647 use crate::ty::ExistentialPredicate::*;
648 match (*ep_a, *ep_b) {
649 (Trait(ref a), Trait(ref b)) => Ok(Trait(relation.relate(a, b)?)),
650 (Projection(ref a), Projection(ref b)) => Ok(Projection(relation.relate(a, b)?)),
651 (AutoTrait(ref a), AutoTrait(ref b)) if a == b => Ok(AutoTrait(*a)),
652 _ => Err(TypeError::ExistentialMismatch(expected_found(relation, a, b)))
655 Ok(tcx.mk_existential_predicates(v)?)
659 impl<'tcx> Relate<'tcx> for ty::ClosureSubsts<'tcx> {
660 fn relate<'a, 'gcx, R>(relation: &mut R,
661 a: &ty::ClosureSubsts<'tcx>,
662 b: &ty::ClosureSubsts<'tcx>)
663 -> RelateResult<'tcx, ty::ClosureSubsts<'tcx>>
664 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
666 let substs = relate_substs(relation, None, a.substs, b.substs)?;
667 Ok(ty::ClosureSubsts { substs })
671 impl<'tcx> Relate<'tcx> for ty::GeneratorSubsts<'tcx> {
672 fn relate<'a, 'gcx, R>(relation: &mut R,
673 a: &ty::GeneratorSubsts<'tcx>,
674 b: &ty::GeneratorSubsts<'tcx>)
675 -> RelateResult<'tcx, ty::GeneratorSubsts<'tcx>>
676 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
678 let substs = relate_substs(relation, None, a.substs, b.substs)?;
679 Ok(ty::GeneratorSubsts { substs })
683 impl<'tcx> Relate<'tcx> for SubstsRef<'tcx> {
684 fn relate<'a, 'gcx, R>(relation: &mut R,
687 -> RelateResult<'tcx, SubstsRef<'tcx>>
688 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
690 relate_substs(relation, None, a, b)
694 impl<'tcx> Relate<'tcx> for ty::Region<'tcx> {
695 fn relate<'a, 'gcx, R>(relation: &mut R,
696 a: &ty::Region<'tcx>,
697 b: &ty::Region<'tcx>)
698 -> RelateResult<'tcx, ty::Region<'tcx>>
699 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
701 relation.regions(*a, *b)
705 impl<'tcx> Relate<'tcx> for &'tcx ty::Const<'tcx> {
706 fn relate<'a, 'gcx, R>(relation: &mut R,
707 a: &&'tcx ty::Const<'tcx>,
708 b: &&'tcx ty::Const<'tcx>)
709 -> RelateResult<'tcx, &'tcx ty::Const<'tcx>>
710 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
712 relation.consts(*a, *b)
716 impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for ty::Binder<T> {
717 fn relate<'a, 'gcx, R>(relation: &mut R,
720 -> RelateResult<'tcx, ty::Binder<T>>
721 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
723 relation.binders(a, b)
727 impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for Rc<T> {
728 fn relate<'a, 'gcx, R>(relation: &mut R,
731 -> RelateResult<'tcx, Rc<T>>
732 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
736 Ok(Rc::new(relation.relate(a, b)?))
740 impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for Box<T> {
741 fn relate<'a, 'gcx, R>(relation: &mut R,
744 -> RelateResult<'tcx, Box<T>>
745 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
749 Ok(Box::new(relation.relate(a, b)?))
753 impl<'tcx> Relate<'tcx> for Kind<'tcx> {
754 fn relate<'a, 'gcx, R>(
758 ) -> RelateResult<'tcx, Kind<'tcx>>
760 R: TypeRelation<'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a,
762 match (a.unpack(), b.unpack()) {
763 (UnpackedKind::Lifetime(a_lt), UnpackedKind::Lifetime(b_lt)) => {
764 Ok(relation.relate(&a_lt, &b_lt)?.into())
766 (UnpackedKind::Type(a_ty), UnpackedKind::Type(b_ty)) => {
767 Ok(relation.relate(&a_ty, &b_ty)?.into())
769 (UnpackedKind::Const(a_ct), UnpackedKind::Const(b_ct)) => {
770 Ok(relation.relate(&a_ct, &b_ct)?.into())
772 (UnpackedKind::Lifetime(unpacked), x) => {
773 bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x)
775 (UnpackedKind::Type(unpacked), x) => {
776 bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x)
778 (UnpackedKind::Const(unpacked), x) => {
779 bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x)
785 impl<'tcx> Relate<'tcx> for ty::TraitPredicate<'tcx> {
786 fn relate<'a, 'gcx, R>(
788 a: &ty::TraitPredicate<'tcx>,
789 b: &ty::TraitPredicate<'tcx>
790 ) -> RelateResult<'tcx, ty::TraitPredicate<'tcx>>
791 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
793 Ok(ty::TraitPredicate {
794 trait_ref: relation.relate(&a.trait_ref, &b.trait_ref)?,
799 impl<'tcx> Relate<'tcx> for ty::ProjectionPredicate<'tcx> {
800 fn relate<'a, 'gcx, R>(
802 a: &ty::ProjectionPredicate<'tcx>,
803 b: &ty::ProjectionPredicate<'tcx>,
804 ) -> RelateResult<'tcx, ty::ProjectionPredicate<'tcx>>
805 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
807 Ok(ty::ProjectionPredicate {
808 projection_ty: relation.relate(&a.projection_ty, &b.projection_ty)?,
809 ty: relation.relate(&a.ty, &b.ty)?,
814 impl<'tcx> Relate<'tcx> for traits::WhereClause<'tcx> {
815 fn relate<'a, 'gcx, R>(
817 a: &traits::WhereClause<'tcx>,
818 b: &traits::WhereClause<'tcx>
819 ) -> RelateResult<'tcx, traits::WhereClause<'tcx>>
820 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
822 use crate::traits::WhereClause::*;
824 (Implemented(a_pred), Implemented(b_pred)) => {
825 Ok(Implemented(relation.relate(a_pred, b_pred)?))
828 (ProjectionEq(a_pred), ProjectionEq(b_pred)) => {
829 Ok(ProjectionEq(relation.relate(a_pred, b_pred)?))
832 (RegionOutlives(a_pred), RegionOutlives(b_pred)) => {
833 Ok(RegionOutlives(ty::OutlivesPredicate(
834 relation.relate(&a_pred.0, &b_pred.0)?,
835 relation.relate(&a_pred.1, &b_pred.1)?,
839 (TypeOutlives(a_pred), TypeOutlives(b_pred)) => {
840 Ok(TypeOutlives(ty::OutlivesPredicate(
841 relation.relate(&a_pred.0, &b_pred.0)?,
842 relation.relate(&a_pred.1, &b_pred.1)?,
846 _ => Err(TypeError::Mismatch),
851 impl<'tcx> Relate<'tcx> for traits::WellFormed<'tcx> {
852 fn relate<'a, 'gcx, R>(
854 a: &traits::WellFormed<'tcx>,
855 b: &traits::WellFormed<'tcx>
856 ) -> RelateResult<'tcx, traits::WellFormed<'tcx>>
857 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
859 use crate::traits::WellFormed::*;
861 (Trait(a_pred), Trait(b_pred)) => Ok(Trait(relation.relate(a_pred, b_pred)?)),
862 (Ty(a_ty), Ty(b_ty)) => Ok(Ty(relation.relate(a_ty, b_ty)?)),
863 _ => Err(TypeError::Mismatch),
868 impl<'tcx> Relate<'tcx> for traits::FromEnv<'tcx> {
869 fn relate<'a, 'gcx, R>(
871 a: &traits::FromEnv<'tcx>,
872 b: &traits::FromEnv<'tcx>
873 ) -> RelateResult<'tcx, traits::FromEnv<'tcx>>
874 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
876 use crate::traits::FromEnv::*;
878 (Trait(a_pred), Trait(b_pred)) => Ok(Trait(relation.relate(a_pred, b_pred)?)),
879 (Ty(a_ty), Ty(b_ty)) => Ok(Ty(relation.relate(a_ty, b_ty)?)),
880 _ => Err(TypeError::Mismatch),
885 impl<'tcx> Relate<'tcx> for traits::DomainGoal<'tcx> {
886 fn relate<'a, 'gcx, R>(
888 a: &traits::DomainGoal<'tcx>,
889 b: &traits::DomainGoal<'tcx>
890 ) -> RelateResult<'tcx, traits::DomainGoal<'tcx>>
891 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
893 use crate::traits::DomainGoal::*;
895 (Holds(a_wc), Holds(b_wc)) => Ok(Holds(relation.relate(a_wc, b_wc)?)),
896 (WellFormed(a_wf), WellFormed(b_wf)) => Ok(WellFormed(relation.relate(a_wf, b_wf)?)),
897 (FromEnv(a_fe), FromEnv(b_fe)) => Ok(FromEnv(relation.relate(a_fe, b_fe)?)),
899 (Normalize(a_pred), Normalize(b_pred)) => {
900 Ok(Normalize(relation.relate(a_pred, b_pred)?))
903 _ => Err(TypeError::Mismatch),
908 impl<'tcx> Relate<'tcx> for traits::Goal<'tcx> {
909 fn relate<'a, 'gcx, R>(
911 a: &traits::Goal<'tcx>,
912 b: &traits::Goal<'tcx>
913 ) -> RelateResult<'tcx, traits::Goal<'tcx>>
914 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
916 use crate::traits::GoalKind::*;
918 (Implies(a_clauses, a_goal), Implies(b_clauses, b_goal)) => {
919 let clauses = relation.relate(a_clauses, b_clauses)?;
920 let goal = relation.relate(a_goal, b_goal)?;
921 Ok(relation.tcx().mk_goal(Implies(clauses, goal)))
924 (And(a_left, a_right), And(b_left, b_right)) => {
925 let left = relation.relate(a_left, b_left)?;
926 let right = relation.relate(a_right, b_right)?;
927 Ok(relation.tcx().mk_goal(And(left, right)))
930 (Not(a_goal), Not(b_goal)) => {
931 let goal = relation.relate(a_goal, b_goal)?;
932 Ok(relation.tcx().mk_goal(Not(goal)))
935 (DomainGoal(a_goal), DomainGoal(b_goal)) => {
936 let goal = relation.relate(a_goal, b_goal)?;
937 Ok(relation.tcx().mk_goal(DomainGoal(goal)))
940 (Quantified(a_qkind, a_goal), Quantified(b_qkind, b_goal))
941 if a_qkind == b_qkind =>
943 let goal = relation.relate(a_goal, b_goal)?;
944 Ok(relation.tcx().mk_goal(Quantified(*a_qkind, goal)))
947 (CannotProve, CannotProve) => Ok(*a),
949 _ => Err(TypeError::Mismatch),
954 impl<'tcx> Relate<'tcx> for traits::Goals<'tcx> {
955 fn relate<'a, 'gcx, R>(
957 a: &traits::Goals<'tcx>,
958 b: &traits::Goals<'tcx>
959 ) -> RelateResult<'tcx, traits::Goals<'tcx>>
960 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
962 if a.len() != b.len() {
963 return Err(TypeError::Mismatch);
966 let tcx = relation.tcx();
967 let goals = a.iter().zip(b.iter()).map(|(a, b)| relation.relate(a, b));
968 Ok(tcx.mk_goals(goals)?)
972 impl<'tcx> Relate<'tcx> for traits::Clause<'tcx> {
973 fn relate<'a, 'gcx, R>(
975 a: &traits::Clause<'tcx>,
976 b: &traits::Clause<'tcx>
977 ) -> RelateResult<'tcx, traits::Clause<'tcx>>
978 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
980 use crate::traits::Clause::*;
982 (Implies(a_clause), Implies(b_clause)) => {
983 let clause = relation.relate(a_clause, b_clause)?;
987 (ForAll(a_clause), ForAll(b_clause)) => {
988 let clause = relation.relate(a_clause, b_clause)?;
992 _ => Err(TypeError::Mismatch),
997 impl<'tcx> Relate<'tcx> for traits::Clauses<'tcx> {
998 fn relate<'a, 'gcx, R>(
1000 a: &traits::Clauses<'tcx>,
1001 b: &traits::Clauses<'tcx>
1002 ) -> RelateResult<'tcx, traits::Clauses<'tcx>>
1003 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
1005 if a.len() != b.len() {
1006 return Err(TypeError::Mismatch);
1009 let tcx = relation.tcx();
1010 let clauses = a.iter().zip(b.iter()).map(|(a, b)| relation.relate(a, b));
1011 Ok(tcx.mk_clauses(clauses)?)
1015 impl<'tcx> Relate<'tcx> for traits::ProgramClause<'tcx> {
1016 fn relate<'a, 'gcx, R>(
1018 a: &traits::ProgramClause<'tcx>,
1019 b: &traits::ProgramClause<'tcx>
1020 ) -> RelateResult<'tcx, traits::ProgramClause<'tcx>>
1021 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
1023 Ok(traits::ProgramClause {
1024 goal: relation.relate(&a.goal, &b.goal)?,
1025 hypotheses: relation.relate(&a.hypotheses, &b.hypotheses)?,
1026 category: traits::ProgramClauseCategory::Other,
1031 impl<'tcx> Relate<'tcx> for traits::Environment<'tcx> {
1032 fn relate<'a, 'gcx, R>(
1034 a: &traits::Environment<'tcx>,
1035 b: &traits::Environment<'tcx>
1036 ) -> RelateResult<'tcx, traits::Environment<'tcx>>
1037 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
1039 Ok(traits::Environment {
1040 clauses: relation.relate(&a.clauses, &b.clauses)?,
1045 impl<'tcx, G> Relate<'tcx> for traits::InEnvironment<'tcx, G>
1046 where G: Relate<'tcx>
1048 fn relate<'a, 'gcx, R>(
1050 a: &traits::InEnvironment<'tcx, G>,
1051 b: &traits::InEnvironment<'tcx, G>
1052 ) -> RelateResult<'tcx, traits::InEnvironment<'tcx, G>>
1053 where R: TypeRelation<'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
1055 Ok(traits::InEnvironment {
1056 environment: relation.relate(&a.environment, &b.environment)?,
1057 goal: relation.relate(&a.goal, &b.goal)?,
1062 ///////////////////////////////////////////////////////////////////////////
1065 pub fn expected_found<'a, 'gcx, 'tcx, R, T>(relation: &mut R,
1069 where R: TypeRelation<'gcx, 'tcx>, T: Clone, 'gcx: 'a+'tcx, 'tcx: 'a
1071 expected_found_bool(relation.a_is_expected(), a, b)
1074 pub fn expected_found_bool<T>(a_is_expected: bool,
1083 ExpectedFound {expected: a, found: b}
1085 ExpectedFound {expected: b, found: a}