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::mir::interpret::{get_slice_bytes, ConstValue, GlobalAlloc, Scalar};
8 use crate::ty::error::{ExpectedFound, TypeError};
9 use crate::ty::subst::{GenericArg, GenericArgKind, SubstsRef};
10 use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
12 use rustc_hir::def_id::DefId;
13 use rustc_span::DUMMY_SP;
14 use rustc_target::spec::abi;
17 pub type RelateResult<'tcx, T> = Result<T, TypeError<'tcx>>;
19 #[derive(Clone, Debug)]
21 ExistentialRegionBound, // relating an existential region bound
24 pub trait TypeRelation<'tcx>: Sized {
25 fn tcx(&self) -> TyCtxt<'tcx>;
27 fn param_env(&self) -> ty::ParamEnv<'tcx>;
29 /// Returns a static string we can use for printouts.
30 fn tag(&self) -> &'static str;
32 /// Returns `true` if the value `a` is the "expected" type in the
33 /// relation. Just affects error messages.
34 fn a_is_expected(&self) -> bool;
36 fn with_cause<F, R>(&mut self, _cause: Cause, f: F) -> R
38 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(
54 a_subst: SubstsRef<'tcx>,
55 b_subst: SubstsRef<'tcx>,
56 ) -> RelateResult<'tcx, SubstsRef<'tcx>> {
58 "relate_item_substs(item_def_id={:?}, a_subst={:?}, b_subst={:?})",
59 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>>(
69 variance: ty::Variance,
70 info: ty::VarianceDiagInfo<'tcx>,
73 ) -> RelateResult<'tcx, T>;
75 // Overridable 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.
81 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>>;
87 ) -> RelateResult<'tcx, ty::Region<'tcx>>;
91 a: &'tcx ty::Const<'tcx>,
92 b: &'tcx ty::Const<'tcx>,
93 ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>>;
97 a: ty::Binder<'tcx, T>,
98 b: ty::Binder<'tcx, T>,
99 ) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
104 pub trait Relate<'tcx>: TypeFoldable<'tcx> + Copy {
105 fn relate<R: TypeRelation<'tcx>>(
109 ) -> RelateResult<'tcx, Self>;
112 ///////////////////////////////////////////////////////////////////////////
115 pub fn relate_type_and_mut<'tcx, R: TypeRelation<'tcx>>(
117 a: ty::TypeAndMut<'tcx>,
118 b: ty::TypeAndMut<'tcx>,
119 kind: ty::VarianceDiagMutKind,
120 ) -> RelateResult<'tcx, ty::TypeAndMut<'tcx>> {
121 debug!("{}.mts({:?}, {:?})", relation.tag(), a, b);
122 if a.mutbl != b.mutbl {
123 Err(TypeError::Mutability)
126 let (variance, info) = match mutbl {
127 ast::Mutability::Not => (ty::Covariant, ty::VarianceDiagInfo::None),
128 ast::Mutability::Mut => (ty::Invariant, ty::VarianceDiagInfo::Mut { kind, ty: a.ty }),
130 let ty = relation.relate_with_variance(variance, info, a.ty, b.ty)?;
131 Ok(ty::TypeAndMut { ty, mutbl })
135 pub fn relate_substs<R: TypeRelation<'tcx>>(
137 variances: Option<&[ty::Variance]>,
138 a_subst: SubstsRef<'tcx>,
139 b_subst: SubstsRef<'tcx>,
140 ) -> RelateResult<'tcx, SubstsRef<'tcx>> {
141 let tcx = relation.tcx();
143 let params = iter::zip(a_subst, b_subst).enumerate().map(|(i, (a, b))| {
144 let variance = variances.map_or(ty::Invariant, |v| v[i]);
145 relation.relate_with_variance(variance, ty::VarianceDiagInfo::default(), a, b)
148 tcx.mk_substs(params)
151 impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> {
152 fn relate<R: TypeRelation<'tcx>>(
156 ) -> RelateResult<'tcx, ty::FnSig<'tcx>> {
157 let tcx = relation.tcx();
159 if a.c_variadic != b.c_variadic {
160 return Err(TypeError::VariadicMismatch(expected_found(
166 let unsafety = relation.relate(a.unsafety, b.unsafety)?;
167 let abi = relation.relate(a.abi, b.abi)?;
169 if a.inputs().len() != b.inputs().len() {
170 return Err(TypeError::ArgCount);
173 let inputs_and_output = iter::zip(a.inputs(), b.inputs())
174 .map(|(&a, &b)| ((a, b), false))
175 .chain(iter::once(((a.output(), b.output()), true)))
176 .map(|((a, b), is_output)| {
178 relation.relate(a, b)
180 relation.relate_with_variance(
182 ty::VarianceDiagInfo::default(),
189 .map(|(i, r)| match r {
190 Err(TypeError::Sorts(exp_found)) => Err(TypeError::ArgumentSorts(exp_found, i)),
191 Err(TypeError::Mutability) => Err(TypeError::ArgumentMutability(i)),
195 inputs_and_output: tcx.mk_type_list(inputs_and_output)?,
196 c_variadic: a.c_variadic,
203 impl<'tcx> Relate<'tcx> for ast::Unsafety {
204 fn relate<R: TypeRelation<'tcx>>(
208 ) -> RelateResult<'tcx, ast::Unsafety> {
210 Err(TypeError::UnsafetyMismatch(expected_found(relation, a, b)))
217 impl<'tcx> Relate<'tcx> for abi::Abi {
218 fn relate<R: TypeRelation<'tcx>>(
222 ) -> RelateResult<'tcx, abi::Abi> {
223 if a == b { Ok(a) } else { Err(TypeError::AbiMismatch(expected_found(relation, a, b))) }
227 impl<'tcx> Relate<'tcx> for ty::ProjectionTy<'tcx> {
228 fn relate<R: TypeRelation<'tcx>>(
230 a: ty::ProjectionTy<'tcx>,
231 b: ty::ProjectionTy<'tcx>,
232 ) -> RelateResult<'tcx, ty::ProjectionTy<'tcx>> {
233 if a.item_def_id != b.item_def_id {
234 Err(TypeError::ProjectionMismatched(expected_found(
240 let substs = relation.relate(a.substs, b.substs)?;
241 Ok(ty::ProjectionTy { item_def_id: a.item_def_id, substs: &substs })
246 impl<'tcx> Relate<'tcx> for ty::ExistentialProjection<'tcx> {
247 fn relate<R: TypeRelation<'tcx>>(
249 a: ty::ExistentialProjection<'tcx>,
250 b: ty::ExistentialProjection<'tcx>,
251 ) -> RelateResult<'tcx, ty::ExistentialProjection<'tcx>> {
252 if a.item_def_id != b.item_def_id {
253 Err(TypeError::ProjectionMismatched(expected_found(
259 let ty = relation.relate_with_variance(
261 ty::VarianceDiagInfo::default(),
265 let substs = relation.relate_with_variance(
267 ty::VarianceDiagInfo::default(),
271 Ok(ty::ExistentialProjection { item_def_id: a.item_def_id, substs, ty })
276 impl<'tcx> Relate<'tcx> for ty::TraitRef<'tcx> {
277 fn relate<R: TypeRelation<'tcx>>(
279 a: ty::TraitRef<'tcx>,
280 b: ty::TraitRef<'tcx>,
281 ) -> RelateResult<'tcx, ty::TraitRef<'tcx>> {
282 // Different traits cannot be related.
283 if a.def_id != b.def_id {
284 Err(TypeError::Traits(expected_found(relation, a.def_id, b.def_id)))
286 let substs = relate_substs(relation, None, a.substs, b.substs)?;
287 Ok(ty::TraitRef { def_id: a.def_id, substs })
292 impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
293 fn relate<R: TypeRelation<'tcx>>(
295 a: ty::ExistentialTraitRef<'tcx>,
296 b: ty::ExistentialTraitRef<'tcx>,
297 ) -> RelateResult<'tcx, ty::ExistentialTraitRef<'tcx>> {
298 // Different traits cannot be related.
299 if a.def_id != b.def_id {
300 Err(TypeError::Traits(expected_found(relation, a.def_id, b.def_id)))
302 let substs = relate_substs(relation, None, a.substs, b.substs)?;
303 Ok(ty::ExistentialTraitRef { def_id: a.def_id, substs })
308 #[derive(Copy, Debug, Clone, TypeFoldable)]
309 struct GeneratorWitness<'tcx>(&'tcx ty::List<Ty<'tcx>>);
311 impl<'tcx> Relate<'tcx> for GeneratorWitness<'tcx> {
312 fn relate<R: TypeRelation<'tcx>>(
314 a: GeneratorWitness<'tcx>,
315 b: GeneratorWitness<'tcx>,
316 ) -> RelateResult<'tcx, GeneratorWitness<'tcx>> {
317 assert_eq!(a.0.len(), b.0.len());
318 let tcx = relation.tcx();
319 let types = tcx.mk_type_list(iter::zip(a.0, b.0).map(|(a, b)| relation.relate(a, b)))?;
320 Ok(GeneratorWitness(types))
324 impl<'tcx> Relate<'tcx> for Ty<'tcx> {
326 fn relate<R: TypeRelation<'tcx>>(
330 ) -> RelateResult<'tcx, Ty<'tcx>> {
335 /// The main "type relation" routine. Note that this does not handle
336 /// inference artifacts, so you should filter those out before calling
338 pub fn super_relate_tys<R: TypeRelation<'tcx>>(
342 ) -> RelateResult<'tcx, Ty<'tcx>> {
343 let tcx = relation.tcx();
344 debug!("super_relate_tys: a={:?} b={:?}", a, b);
345 match (a.kind(), b.kind()) {
346 (&ty::Infer(_), _) | (_, &ty::Infer(_)) => {
347 // The caller should handle these cases!
348 bug!("var types encountered in super_relate_tys")
351 (ty::Bound(..), _) | (_, ty::Bound(..)) => {
352 bug!("bound types encountered in super_relate_tys")
355 (&ty::Error(_), _) | (_, &ty::Error(_)) => Ok(tcx.ty_error()),
369 (&ty::Param(ref a_p), &ty::Param(ref b_p)) if a_p.index == b_p.index => Ok(a),
371 (ty::Placeholder(p1), ty::Placeholder(p2)) if p1 == p2 => Ok(a),
373 (&ty::Adt(a_def, a_substs), &ty::Adt(b_def, b_substs)) if a_def == b_def => {
374 let substs = relation.relate_item_substs(a_def.did, a_substs, b_substs)?;
375 Ok(tcx.mk_adt(a_def, substs))
378 (&ty::Foreign(a_id), &ty::Foreign(b_id)) if a_id == b_id => Ok(tcx.mk_foreign(a_id)),
380 (&ty::Dynamic(a_obj, a_region), &ty::Dynamic(b_obj, b_region)) => {
381 let region_bound = relation.with_cause(Cause::ExistentialRegionBound, |relation| {
382 relation.relate_with_variance(
384 ty::VarianceDiagInfo::default(),
389 Ok(tcx.mk_dynamic(relation.relate(a_obj, b_obj)?, region_bound))
392 (&ty::Generator(a_id, a_substs, movability), &ty::Generator(b_id, b_substs, _))
395 // All Generator types with the same id represent
396 // the (anonymous) type of the same generator expression. So
397 // all of their regions should be equated.
398 let substs = relation.relate(a_substs, b_substs)?;
399 Ok(tcx.mk_generator(a_id, substs, movability))
402 (&ty::GeneratorWitness(a_types), &ty::GeneratorWitness(b_types)) => {
403 // Wrap our types with a temporary GeneratorWitness struct
404 // inside the binder so we can related them
405 let a_types = a_types.map_bound(GeneratorWitness);
406 let b_types = b_types.map_bound(GeneratorWitness);
407 // Then remove the GeneratorWitness for the result
408 let types = relation.relate(a_types, b_types)?.map_bound(|witness| witness.0);
409 Ok(tcx.mk_generator_witness(types))
412 (&ty::Closure(a_id, a_substs), &ty::Closure(b_id, b_substs)) if a_id == b_id => {
413 // All Closure types with the same id represent
414 // the (anonymous) type of the same closure expression. So
415 // all of their regions should be equated.
416 let substs = relation.relate(a_substs, b_substs)?;
417 Ok(tcx.mk_closure(a_id, &substs))
420 (&ty::RawPtr(a_mt), &ty::RawPtr(b_mt)) => {
421 let mt = relate_type_and_mut(relation, a_mt, b_mt, ty::VarianceDiagMutKind::RawPtr)?;
425 (&ty::Ref(a_r, a_ty, a_mutbl), &ty::Ref(b_r, b_ty, b_mutbl)) => {
426 let r = relation.relate_with_variance(
428 ty::VarianceDiagInfo::default(),
432 let a_mt = ty::TypeAndMut { ty: a_ty, mutbl: a_mutbl };
433 let b_mt = ty::TypeAndMut { ty: b_ty, mutbl: b_mutbl };
434 let mt = relate_type_and_mut(relation, a_mt, b_mt, ty::VarianceDiagMutKind::Ref)?;
435 Ok(tcx.mk_ref(r, mt))
438 (&ty::Array(a_t, sz_a), &ty::Array(b_t, sz_b)) => {
439 let t = relation.relate(a_t, b_t)?;
440 match relation.relate(sz_a, sz_b) {
441 Ok(sz) => Ok(tcx.mk_ty(ty::Array(t, sz))),
443 // Check whether the lengths are both concrete/known values,
444 // but are unequal, for better diagnostics.
446 // It might seem dubious to eagerly evaluate these constants here,
447 // we however cannot end up with errors in `Relate` during both
448 // `type_of` and `predicates_of`. This means that evaluating the
449 // constants should not cause cycle errors here.
450 let sz_a = sz_a.try_eval_usize(tcx, relation.param_env());
451 let sz_b = sz_b.try_eval_usize(tcx, relation.param_env());
453 (Some(sz_a_val), Some(sz_b_val)) if sz_a_val != sz_b_val => Err(
454 TypeError::FixedArraySize(expected_found(relation, sz_a_val, sz_b_val)),
462 (&ty::Slice(a_t), &ty::Slice(b_t)) => {
463 let t = relation.relate(a_t, b_t)?;
467 (&ty::Tuple(as_), &ty::Tuple(bs)) => {
468 if as_.len() == bs.len() {
470 iter::zip(as_, bs).map(|(a, b)| relation.relate(a.expect_ty(), b.expect_ty())),
472 } else if !(as_.is_empty() || bs.is_empty()) {
473 Err(TypeError::TupleSize(expected_found(relation, as_.len(), bs.len())))
475 Err(TypeError::Sorts(expected_found(relation, a, b)))
479 (&ty::FnDef(a_def_id, a_substs), &ty::FnDef(b_def_id, b_substs))
480 if a_def_id == b_def_id =>
482 let substs = relation.relate_item_substs(a_def_id, a_substs, b_substs)?;
483 Ok(tcx.mk_fn_def(a_def_id, substs))
486 (&ty::FnPtr(a_fty), &ty::FnPtr(b_fty)) => {
487 let fty = relation.relate(a_fty, b_fty)?;
488 Ok(tcx.mk_fn_ptr(fty))
491 // these two are already handled downstream in case of lazy normalization
492 (&ty::Projection(a_data), &ty::Projection(b_data)) => {
493 let projection_ty = relation.relate(a_data, b_data)?;
494 Ok(tcx.mk_projection(projection_ty.item_def_id, projection_ty.substs))
497 (&ty::Opaque(a_def_id, a_substs), &ty::Opaque(b_def_id, b_substs))
498 if a_def_id == b_def_id =>
500 let substs = relate_substs(relation, None, a_substs, b_substs)?;
501 Ok(tcx.mk_opaque(a_def_id, substs))
504 _ => Err(TypeError::Sorts(expected_found(relation, a, b))),
508 /// The main "const relation" routine. Note that this does not handle
509 /// inference artifacts, so you should filter those out before calling
511 pub fn super_relate_consts<R: TypeRelation<'tcx>>(
513 a: &'tcx ty::Const<'tcx>,
514 b: &'tcx ty::Const<'tcx>,
515 ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
516 debug!("{}.super_relate_consts(a = {:?}, b = {:?})", relation.tag(), a, b);
517 let tcx = relation.tcx();
519 // FIXME(oli-obk): once const generics can have generic types, this assertion
520 // will likely get triggered. Move to `normalize_erasing_regions` at that point.
521 let a_ty = tcx.erase_regions(a.ty);
522 let b_ty = tcx.erase_regions(b.ty);
524 relation.tcx().sess.delay_span_bug(
526 &format!("cannot relate constants of different types: {} != {}", a_ty, b_ty),
530 let eagerly_eval = |x: &'tcx ty::Const<'tcx>| x.eval(tcx, relation.param_env());
531 let a = eagerly_eval(a);
532 let b = eagerly_eval(b);
534 // Currently, the values that can be unified are primitive types,
535 // and those that derive both `PartialEq` and `Eq`, corresponding
536 // to structural-match types.
537 let is_match = match (a.val, b.val) {
538 (ty::ConstKind::Infer(_), _) | (_, ty::ConstKind::Infer(_)) => {
539 // The caller should handle these cases!
540 bug!("var types encountered in super_relate_consts: {:?} {:?}", a, b)
543 (ty::ConstKind::Error(_), _) => return Ok(a),
544 (_, ty::ConstKind::Error(_)) => return Ok(b),
546 (ty::ConstKind::Param(a_p), ty::ConstKind::Param(b_p)) => a_p.index == b_p.index,
547 (ty::ConstKind::Placeholder(p1), ty::ConstKind::Placeholder(p2)) => p1 == p2,
548 (ty::ConstKind::Value(a_val), ty::ConstKind::Value(b_val)) => {
549 check_const_value_eq(relation, a_val, b_val, a, b)?
552 (ty::ConstKind::Unevaluated(au), ty::ConstKind::Unevaluated(bu))
553 if tcx.features().const_evaluatable_checked =>
555 tcx.try_unify_abstract_consts(((au.def, au.substs), (bu.def, bu.substs)))
558 // While this is slightly incorrect, it shouldn't matter for `min_const_generics`
559 // and is the better alternative to waiting until `const_evaluatable_checked` can
561 (ty::ConstKind::Unevaluated(au), ty::ConstKind::Unevaluated(bu))
562 if au.def == bu.def && au.promoted == bu.promoted =>
564 let substs = relation.relate_with_variance(
565 ty::Variance::Invariant,
566 ty::VarianceDiagInfo::default(),
570 return Ok(tcx.mk_const(ty::Const {
571 val: ty::ConstKind::Unevaluated(ty::Unevaluated {
574 promoted: au.promoted,
581 if is_match { Ok(a) } else { Err(TypeError::ConstMismatch(expected_found(relation, a, b))) }
584 fn check_const_value_eq<R: TypeRelation<'tcx>>(
586 a_val: ConstValue<'tcx>,
587 b_val: ConstValue<'tcx>,
588 // FIXME(oli-obk): these arguments should go away with valtrees
589 a: &'tcx ty::Const<'tcx>,
590 b: &'tcx ty::Const<'tcx>,
591 // FIXME(oli-obk): this should just be `bool` with valtrees
592 ) -> RelateResult<'tcx, bool> {
593 let tcx = relation.tcx();
594 Ok(match (a_val, b_val) {
595 (ConstValue::Scalar(Scalar::Int(a_val)), ConstValue::Scalar(Scalar::Int(b_val))) => {
599 ConstValue::Scalar(Scalar::Ptr(a_val, _a_size)),
600 ConstValue::Scalar(Scalar::Ptr(b_val, _b_size)),
603 || match (tcx.global_alloc(a_val.provenance), tcx.global_alloc(b_val.provenance)) {
604 (GlobalAlloc::Function(a_instance), GlobalAlloc::Function(b_instance)) => {
605 a_instance == b_instance
611 (ConstValue::Slice { .. }, ConstValue::Slice { .. }) => {
612 get_slice_bytes(&tcx, a_val) == get_slice_bytes(&tcx, b_val)
615 (ConstValue::ByRef { .. }, ConstValue::ByRef { .. }) => {
616 let a_destructured = tcx.destructure_const(relation.param_env().and(a));
617 let b_destructured = tcx.destructure_const(relation.param_env().and(b));
619 // Both the variant and each field have to be equal.
620 if a_destructured.variant == b_destructured.variant {
621 for (a_field, b_field) in iter::zip(a_destructured.fields, b_destructured.fields) {
622 relation.consts(a_field, b_field)?;
635 impl<'tcx> Relate<'tcx> for &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>> {
636 fn relate<R: TypeRelation<'tcx>>(
640 ) -> RelateResult<'tcx, Self> {
641 let tcx = relation.tcx();
643 // FIXME: this is wasteful, but want to do a perf run to see how slow it is.
644 // We need to perform this deduplication as we sometimes generate duplicate projections
646 let mut a_v: Vec<_> = a.into_iter().collect();
647 let mut b_v: Vec<_> = b.into_iter().collect();
648 // `skip_binder` here is okay because `stable_cmp` doesn't look at binders
649 a_v.sort_by(|a, b| a.skip_binder().stable_cmp(tcx, &b.skip_binder()));
651 b_v.sort_by(|a, b| a.skip_binder().stable_cmp(tcx, &b.skip_binder()));
653 if a_v.len() != b_v.len() {
654 return Err(TypeError::ExistentialMismatch(expected_found(relation, a, b)));
657 let v = iter::zip(a_v, b_v).map(|(ep_a, ep_b)| {
658 use crate::ty::ExistentialPredicate::*;
659 match (ep_a.skip_binder(), ep_b.skip_binder()) {
660 (Trait(a), Trait(b)) => Ok(ep_a
661 .rebind(Trait(relation.relate(ep_a.rebind(a), ep_b.rebind(b))?.skip_binder()))),
662 (Projection(a), Projection(b)) => Ok(ep_a.rebind(Projection(
663 relation.relate(ep_a.rebind(a), ep_b.rebind(b))?.skip_binder(),
665 (AutoTrait(a), AutoTrait(b)) if a == b => Ok(ep_a.rebind(AutoTrait(a))),
666 _ => Err(TypeError::ExistentialMismatch(expected_found(relation, a, b))),
669 tcx.mk_poly_existential_predicates(v)
673 impl<'tcx> Relate<'tcx> for ty::ClosureSubsts<'tcx> {
674 fn relate<R: TypeRelation<'tcx>>(
676 a: ty::ClosureSubsts<'tcx>,
677 b: ty::ClosureSubsts<'tcx>,
678 ) -> RelateResult<'tcx, ty::ClosureSubsts<'tcx>> {
679 let substs = relate_substs(relation, None, a.substs, b.substs)?;
680 Ok(ty::ClosureSubsts { substs })
684 impl<'tcx> Relate<'tcx> for ty::GeneratorSubsts<'tcx> {
685 fn relate<R: TypeRelation<'tcx>>(
687 a: ty::GeneratorSubsts<'tcx>,
688 b: ty::GeneratorSubsts<'tcx>,
689 ) -> RelateResult<'tcx, ty::GeneratorSubsts<'tcx>> {
690 let substs = relate_substs(relation, None, a.substs, b.substs)?;
691 Ok(ty::GeneratorSubsts { substs })
695 impl<'tcx> Relate<'tcx> for SubstsRef<'tcx> {
696 fn relate<R: TypeRelation<'tcx>>(
700 ) -> RelateResult<'tcx, SubstsRef<'tcx>> {
701 relate_substs(relation, None, a, b)
705 impl<'tcx> Relate<'tcx> for ty::Region<'tcx> {
706 fn relate<R: TypeRelation<'tcx>>(
710 ) -> RelateResult<'tcx, ty::Region<'tcx>> {
711 relation.regions(a, b)
715 impl<'tcx> Relate<'tcx> for &'tcx ty::Const<'tcx> {
716 fn relate<R: TypeRelation<'tcx>>(
718 a: &'tcx ty::Const<'tcx>,
719 b: &'tcx ty::Const<'tcx>,
720 ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
721 relation.consts(a, b)
725 impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for ty::Binder<'tcx, T> {
726 fn relate<R: TypeRelation<'tcx>>(
728 a: ty::Binder<'tcx, T>,
729 b: ty::Binder<'tcx, T>,
730 ) -> RelateResult<'tcx, ty::Binder<'tcx, T>> {
731 relation.binders(a, b)
735 impl<'tcx> Relate<'tcx> for GenericArg<'tcx> {
736 fn relate<R: TypeRelation<'tcx>>(
740 ) -> RelateResult<'tcx, GenericArg<'tcx>> {
741 match (a.unpack(), b.unpack()) {
742 (GenericArgKind::Lifetime(a_lt), GenericArgKind::Lifetime(b_lt)) => {
743 Ok(relation.relate(a_lt, b_lt)?.into())
745 (GenericArgKind::Type(a_ty), GenericArgKind::Type(b_ty)) => {
746 Ok(relation.relate(a_ty, b_ty)?.into())
748 (GenericArgKind::Const(a_ct), GenericArgKind::Const(b_ct)) => {
749 Ok(relation.relate(a_ct, b_ct)?.into())
751 (GenericArgKind::Lifetime(unpacked), x) => {
752 bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x)
754 (GenericArgKind::Type(unpacked), x) => {
755 bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x)
757 (GenericArgKind::Const(unpacked), x) => {
758 bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x)
764 impl<'tcx> Relate<'tcx> for ty::TraitPredicate<'tcx> {
765 fn relate<R: TypeRelation<'tcx>>(
767 a: ty::TraitPredicate<'tcx>,
768 b: ty::TraitPredicate<'tcx>,
769 ) -> RelateResult<'tcx, ty::TraitPredicate<'tcx>> {
770 Ok(ty::TraitPredicate { trait_ref: relation.relate(a.trait_ref, b.trait_ref)? })
774 impl<'tcx> Relate<'tcx> for ty::ProjectionPredicate<'tcx> {
775 fn relate<R: TypeRelation<'tcx>>(
777 a: ty::ProjectionPredicate<'tcx>,
778 b: ty::ProjectionPredicate<'tcx>,
779 ) -> RelateResult<'tcx, ty::ProjectionPredicate<'tcx>> {
780 Ok(ty::ProjectionPredicate {
781 projection_ty: relation.relate(a.projection_ty, b.projection_ty)?,
782 ty: relation.relate(a.ty, b.ty)?,
787 ///////////////////////////////////////////////////////////////////////////
790 pub fn expected_found<R, T>(relation: &mut R, a: T, b: T) -> ExpectedFound<T>
792 R: TypeRelation<'tcx>,
794 expected_found_bool(relation.a_is_expected(), a, b)
797 pub fn expected_found_bool<T>(a_is_expected: bool, a: T, b: T) -> ExpectedFound<T> {
799 ExpectedFound { expected: a, found: b }
801 ExpectedFound { expected: b, found: a }