1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Generalized type relating mechanism. A type relation R relates a
12 //! pair of values (A, B). A and B are usually types or regions but
13 //! can be other things. Examples of type relations are subtyping,
14 //! type equality, etc.
16 use hir::def_id::DefId;
17 use mir::interpret::ConstValue;
18 use ty::subst::{Kind, UnpackedKind, Substs};
19 use ty::{self, Ty, TyCtxt, TypeFoldable};
20 use ty::error::{ExpectedFound, TypeError};
21 use mir::interpret::GlobalId;
22 use util::common::ErrorReported;
25 use rustc_target::spec::abi;
28 pub type RelateResult<'tcx, T> = Result<T, TypeError<'tcx>>;
30 #[derive(Clone, Debug)]
32 ExistentialRegionBound, // relating an existential region bound
35 pub trait TypeRelation<'a, 'gcx: 'a+'tcx, 'tcx: 'a> : Sized {
36 fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx>;
38 /// Returns a static string we can use for printouts.
39 fn tag(&self) -> &'static str;
41 /// Returns true if the value `a` is the "expected" type in the
42 /// relation. Just affects error messages.
43 fn a_is_expected(&self) -> bool;
45 fn with_cause<F,R>(&mut self, _cause: Cause, f: F) -> R
46 where F: FnOnce(&mut Self) -> R
51 /// Generic relation routine suitable for most anything.
52 fn relate<T: Relate<'tcx>>(&mut self, a: &T, b: &T) -> RelateResult<'tcx, T> {
53 Relate::relate(self, a, b)
56 /// Relate the two substitutions for the given item. The default
57 /// is to look up the variance for the item and proceed
59 fn relate_item_substs(&mut self,
61 a_subst: &'tcx Substs<'tcx>,
62 b_subst: &'tcx Substs<'tcx>)
63 -> RelateResult<'tcx, &'tcx Substs<'tcx>>
65 debug!("relate_item_substs(item_def_id={:?}, a_subst={:?}, b_subst={:?})",
70 let opt_variances = self.tcx().variances_of(item_def_id);
71 relate_substs(self, Some(&opt_variances), a_subst, b_subst)
74 /// Switch variance for the purpose of relating `a` and `b`.
75 fn relate_with_variance<T: Relate<'tcx>>(&mut self,
76 variance: ty::Variance,
79 -> RelateResult<'tcx, T>;
81 // Overrideable relations. You shouldn't typically call these
82 // directly, instead call `relate()`, which in turn calls
83 // these. This is both more uniform but also allows us to add
84 // additional hooks for other types in the future if needed
85 // without making older code, which called `relate`, obsolete.
87 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>)
88 -> RelateResult<'tcx, Ty<'tcx>>;
90 fn regions(&mut self, a: ty::Region<'tcx>, b: ty::Region<'tcx>)
91 -> RelateResult<'tcx, ty::Region<'tcx>>;
93 fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
94 -> RelateResult<'tcx, ty::Binder<T>>
95 where T: Relate<'tcx>;
98 pub trait Relate<'tcx>: TypeFoldable<'tcx> {
99 fn relate<'a, 'gcx, R>(relation: &mut R, a: &Self, b: &Self)
100 -> RelateResult<'tcx, Self>
101 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a;
104 ///////////////////////////////////////////////////////////////////////////
107 impl<'tcx> Relate<'tcx> for ty::TypeAndMut<'tcx> {
108 fn relate<'a, 'gcx, R>(relation: &mut R,
109 a: &ty::TypeAndMut<'tcx>,
110 b: &ty::TypeAndMut<'tcx>)
111 -> RelateResult<'tcx, ty::TypeAndMut<'tcx>>
112 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
114 debug!("{}.mts({:?}, {:?})",
118 if a.mutbl != b.mutbl {
119 Err(TypeError::Mutability)
122 let variance = match mutbl {
123 ast::Mutability::MutImmutable => ty::Covariant,
124 ast::Mutability::MutMutable => ty::Invariant,
126 let ty = relation.relate_with_variance(variance, &a.ty, &b.ty)?;
127 Ok(ty::TypeAndMut {ty: ty, mutbl: mutbl})
132 pub fn relate_substs<'a, 'gcx, 'tcx, R>(relation: &mut R,
133 variances: Option<&Vec<ty::Variance>>,
134 a_subst: &'tcx Substs<'tcx>,
135 b_subst: &'tcx Substs<'tcx>)
136 -> RelateResult<'tcx, &'tcx Substs<'tcx>>
137 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
139 let tcx = relation.tcx();
141 let params = a_subst.iter().zip(b_subst).enumerate().map(|(i, (a, b))| {
142 let variance = variances.map_or(ty::Invariant, |v| v[i]);
143 relation.relate_with_variance(variance, a, b)
146 Ok(tcx.mk_substs(params)?)
149 impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> {
150 fn relate<'a, 'gcx, R>(relation: &mut R,
153 -> RelateResult<'tcx, ty::FnSig<'tcx>>
154 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
156 let tcx = relation.tcx();
158 if a.variadic != b.variadic {
159 return Err(TypeError::VariadicMismatch(
160 expected_found(relation, &a.variadic, &b.variadic)));
162 let unsafety = relation.relate(&a.unsafety, &b.unsafety)?;
163 let abi = relation.relate(&a.abi, &b.abi)?;
165 if a.inputs().len() != b.inputs().len() {
166 return Err(TypeError::ArgCount);
169 let inputs_and_output = a.inputs().iter().cloned()
170 .zip(b.inputs().iter().cloned())
172 .chain(iter::once(((a.output(), b.output()), true)))
173 .map(|((a, b), is_output)| {
175 relation.relate(&a, &b)
177 relation.relate_with_variance(ty::Contravariant, &a, &b)
181 inputs_and_output: tcx.mk_type_list(inputs_and_output)?,
182 variadic: a.variadic,
189 impl<'tcx> Relate<'tcx> for ast::Unsafety {
190 fn relate<'a, 'gcx, R>(relation: &mut R,
193 -> RelateResult<'tcx, ast::Unsafety>
194 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
197 Err(TypeError::UnsafetyMismatch(expected_found(relation, a, b)))
204 impl<'tcx> Relate<'tcx> for abi::Abi {
205 fn relate<'a, 'gcx, R>(relation: &mut R,
208 -> RelateResult<'tcx, abi::Abi>
209 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
214 Err(TypeError::AbiMismatch(expected_found(relation, a, b)))
219 impl<'tcx> Relate<'tcx> for ty::ProjectionTy<'tcx> {
220 fn relate<'a, 'gcx, R>(relation: &mut R,
221 a: &ty::ProjectionTy<'tcx>,
222 b: &ty::ProjectionTy<'tcx>)
223 -> RelateResult<'tcx, ty::ProjectionTy<'tcx>>
224 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
226 if a.item_def_id != b.item_def_id {
227 Err(TypeError::ProjectionMismatched(
228 expected_found(relation, &a.item_def_id, &b.item_def_id)))
230 let substs = relation.relate(&a.substs, &b.substs)?;
231 Ok(ty::ProjectionTy {
232 item_def_id: a.item_def_id,
239 impl<'tcx> Relate<'tcx> for ty::ExistentialProjection<'tcx> {
240 fn relate<'a, 'gcx, R>(relation: &mut R,
241 a: &ty::ExistentialProjection<'tcx>,
242 b: &ty::ExistentialProjection<'tcx>)
243 -> RelateResult<'tcx, ty::ExistentialProjection<'tcx>>
244 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
246 if a.item_def_id != b.item_def_id {
247 Err(TypeError::ProjectionMismatched(
248 expected_found(relation, &a.item_def_id, &b.item_def_id)))
250 let ty = relation.relate(&a.ty, &b.ty)?;
251 let substs = relation.relate(&a.substs, &b.substs)?;
252 Ok(ty::ExistentialProjection {
253 item_def_id: a.item_def_id,
261 impl<'tcx> Relate<'tcx> for Vec<ty::PolyExistentialProjection<'tcx>> {
262 fn relate<'a, 'gcx, R>(relation: &mut R,
263 a: &Vec<ty::PolyExistentialProjection<'tcx>>,
264 b: &Vec<ty::PolyExistentialProjection<'tcx>>)
265 -> RelateResult<'tcx, Vec<ty::PolyExistentialProjection<'tcx>>>
266 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
268 // To be compatible, `a` and `b` must be for precisely the
269 // same set of traits and item names. We always require that
270 // projection bounds lists are sorted by trait-def-id and item-name,
271 // so we can just iterate through the lists pairwise, so long as they are the
273 if a.len() != b.len() {
274 Err(TypeError::ProjectionBoundsLength(expected_found(relation, &a.len(), &b.len())))
277 .map(|(a, b)| relation.relate(a, b))
283 impl<'tcx> Relate<'tcx> for ty::TraitRef<'tcx> {
284 fn relate<'a, 'gcx, R>(relation: &mut R,
285 a: &ty::TraitRef<'tcx>,
286 b: &ty::TraitRef<'tcx>)
287 -> RelateResult<'tcx, ty::TraitRef<'tcx>>
288 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
290 // Different traits cannot be related
291 if a.def_id != b.def_id {
292 Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
294 let substs = relate_substs(relation, None, a.substs, b.substs)?;
295 Ok(ty::TraitRef { def_id: a.def_id, substs: substs })
300 impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
301 fn relate<'a, 'gcx, R>(relation: &mut R,
302 a: &ty::ExistentialTraitRef<'tcx>,
303 b: &ty::ExistentialTraitRef<'tcx>)
304 -> RelateResult<'tcx, ty::ExistentialTraitRef<'tcx>>
305 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
307 // Different traits cannot be related
308 if a.def_id != b.def_id {
309 Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
311 let substs = relate_substs(relation, None, a.substs, b.substs)?;
312 Ok(ty::ExistentialTraitRef { def_id: a.def_id, substs: substs })
317 #[derive(Debug, Clone)]
318 struct GeneratorWitness<'tcx>(&'tcx ty::Slice<Ty<'tcx>>);
320 TupleStructTypeFoldableImpl! {
321 impl<'tcx> TypeFoldable<'tcx> for GeneratorWitness<'tcx> {
326 impl<'tcx> Relate<'tcx> for GeneratorWitness<'tcx> {
327 fn relate<'a, 'gcx, R>(relation: &mut R,
328 a: &GeneratorWitness<'tcx>,
329 b: &GeneratorWitness<'tcx>)
330 -> RelateResult<'tcx, GeneratorWitness<'tcx>>
331 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
333 assert!(a.0.len() == b.0.len());
334 let tcx = relation.tcx();
335 let types = tcx.mk_type_list(a.0.iter().zip(b.0).map(|(a, b)| relation.relate(a, b)))?;
336 Ok(GeneratorWitness(types))
340 impl<'tcx> Relate<'tcx> for Ty<'tcx> {
341 fn relate<'a, 'gcx, R>(relation: &mut R,
344 -> RelateResult<'tcx, Ty<'tcx>>
345 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
351 /// The main "type relation" routine. Note that this does not handle
352 /// inference artifacts, so you should filter those out before calling
354 pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
357 -> RelateResult<'tcx, Ty<'tcx>>
358 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
360 let tcx = relation.tcx();
363 debug!("super_tys: a_sty={:?} b_sty={:?}", a_sty, b_sty);
364 match (a_sty, b_sty) {
365 (&ty::TyInfer(_), _) |
366 (_, &ty::TyInfer(_)) =>
368 // The caller should handle these cases!
369 bug!("var types encountered in super_relate_tys")
372 (&ty::TyError, _) | (_, &ty::TyError) =>
381 (&ty::TyUint(_), _) |
382 (&ty::TyFloat(_), _) |
389 (&ty::TyParam(ref a_p), &ty::TyParam(ref b_p))
390 if a_p.idx == b_p.idx =>
395 (&ty::TyAdt(a_def, a_substs), &ty::TyAdt(b_def, b_substs))
398 let substs = relation.relate_item_substs(a_def.did, a_substs, b_substs)?;
399 Ok(tcx.mk_adt(a_def, substs))
402 (&ty::TyForeign(a_id), &ty::TyForeign(b_id))
405 Ok(tcx.mk_foreign(a_id))
408 (&ty::TyDynamic(ref a_obj, ref a_region), &ty::TyDynamic(ref b_obj, ref b_region)) => {
409 let region_bound = relation.with_cause(Cause::ExistentialRegionBound,
411 relation.relate_with_variance(
416 Ok(tcx.mk_dynamic(relation.relate(a_obj, b_obj)?, region_bound))
419 (&ty::TyGenerator(a_id, a_substs, movability),
420 &ty::TyGenerator(b_id, b_substs, _))
423 // All TyGenerator types with the same id represent
424 // the (anonymous) type of the same generator expression. So
425 // all of their regions should be equated.
426 let substs = relation.relate(&a_substs, &b_substs)?;
427 Ok(tcx.mk_generator(a_id, substs, movability))
430 (&ty::TyGeneratorWitness(a_types), &ty::TyGeneratorWitness(b_types)) =>
432 // Wrap our types with a temporary GeneratorWitness struct
433 // inside the binder so we can related them
434 let a_types = a_types.map_bound(GeneratorWitness);
435 let b_types = b_types.map_bound(GeneratorWitness);
436 // Then remove the GeneratorWitness for the result
437 let types = relation.relate(&a_types, &b_types)?.map_bound(|witness| witness.0);
438 Ok(tcx.mk_generator_witness(types))
441 (&ty::TyClosure(a_id, a_substs),
442 &ty::TyClosure(b_id, b_substs))
445 // All TyClosure types with the same id represent
446 // the (anonymous) type of the same closure expression. So
447 // all of their regions should be equated.
448 let substs = relation.relate(&a_substs, &b_substs)?;
449 Ok(tcx.mk_closure(a_id, substs))
452 (&ty::TyRawPtr(ref a_mt), &ty::TyRawPtr(ref b_mt)) =>
454 let mt = relation.relate(a_mt, b_mt)?;
458 (&ty::TyRef(a_r, a_ty, a_mutbl), &ty::TyRef(b_r, b_ty, b_mutbl)) =>
460 let r = relation.relate_with_variance(ty::Contravariant, &a_r, &b_r)?;
461 let a_mt = ty::TypeAndMut { ty: a_ty, mutbl: a_mutbl };
462 let b_mt = ty::TypeAndMut { ty: b_ty, mutbl: b_mutbl };
463 let mt = relation.relate(&a_mt, &b_mt)?;
464 Ok(tcx.mk_ref(r, mt))
467 (&ty::TyArray(a_t, sz_a), &ty::TyArray(b_t, sz_b)) =>
469 let t = relation.relate(&a_t, &b_t)?;
470 assert_eq!(sz_a.ty, tcx.types.usize);
471 assert_eq!(sz_b.ty, tcx.types.usize);
472 let to_u64 = |x: &'tcx ty::Const<'tcx>| -> Result<u64, ErrorReported> {
473 if let Some(s) = x.assert_usize(tcx) {
477 ConstValue::Unevaluated(def_id, substs) => {
478 // FIXME(eddyb) get the right param_env.
479 let param_env = ty::ParamEnv::empty();
480 match tcx.lift_to_global(&substs) {
482 let instance = ty::Instance::resolve(
488 if let Some(instance) = instance {
493 if let Some(s) = tcx.const_eval(param_env.and(cid))
495 .map(|c| c.unwrap_usize(tcx)) {
502 tcx.sess.delay_span_bug(tcx.def_span(def_id),
503 "array length could not be evaluated");
506 _ => bug!("arrays should not have {:?} as length", x)
509 match (to_u64(sz_a), to_u64(sz_b)) {
510 (Ok(sz_a_u64), Ok(sz_b_u64)) => {
511 if sz_a_u64 == sz_b_u64 {
512 Ok(tcx.mk_ty(ty::TyArray(t, sz_a)))
514 Err(TypeError::FixedArraySize(
515 expected_found(relation, &sz_a_u64, &sz_b_u64)))
518 // We reported an error or will ICE, so we can return TyError.
519 (Err(ErrorReported), _) | (_, Err(ErrorReported)) => {
525 (&ty::TySlice(a_t), &ty::TySlice(b_t)) =>
527 let t = relation.relate(&a_t, &b_t)?;
531 (&ty::TyTuple(as_), &ty::TyTuple(bs)) =>
533 if as_.len() == bs.len() {
534 Ok(tcx.mk_tup(as_.iter().zip(bs).map(|(a, b)| relation.relate(a, b)))?)
535 } else if !(as_.is_empty() || bs.is_empty()) {
536 Err(TypeError::TupleSize(
537 expected_found(relation, &as_.len(), &bs.len())))
539 Err(TypeError::Sorts(expected_found(relation, &a, &b)))
543 (&ty::TyFnDef(a_def_id, a_substs), &ty::TyFnDef(b_def_id, b_substs))
544 if a_def_id == b_def_id =>
546 let substs = relation.relate_item_substs(a_def_id, a_substs, b_substs)?;
547 Ok(tcx.mk_fn_def(a_def_id, substs))
550 (&ty::TyFnPtr(a_fty), &ty::TyFnPtr(b_fty)) =>
552 let fty = relation.relate(&a_fty, &b_fty)?;
553 Ok(tcx.mk_fn_ptr(fty))
556 (&ty::TyProjection(ref a_data), &ty::TyProjection(ref b_data)) =>
558 let projection_ty = relation.relate(a_data, b_data)?;
559 Ok(tcx.mk_projection(projection_ty.item_def_id, projection_ty.substs))
562 (&ty::TyAnon(a_def_id, a_substs), &ty::TyAnon(b_def_id, b_substs))
563 if a_def_id == b_def_id =>
565 let substs = relate_substs(relation, None, a_substs, b_substs)?;
566 Ok(tcx.mk_anon(a_def_id, substs))
571 Err(TypeError::Sorts(expected_found(relation, &a, &b)))
576 impl<'tcx> Relate<'tcx> for &'tcx ty::Slice<ty::ExistentialPredicate<'tcx>> {
577 fn relate<'a, 'gcx, R>(relation: &mut R,
580 -> RelateResult<'tcx, Self>
581 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a {
583 if a.len() != b.len() {
584 return Err(TypeError::ExistentialMismatch(expected_found(relation, a, b)));
587 let tcx = relation.tcx();
588 let v = a.iter().zip(b.iter()).map(|(ep_a, ep_b)| {
589 use ty::ExistentialPredicate::*;
590 match (*ep_a, *ep_b) {
591 (Trait(ref a), Trait(ref b)) => Ok(Trait(relation.relate(a, b)?)),
592 (Projection(ref a), Projection(ref b)) => Ok(Projection(relation.relate(a, b)?)),
593 (AutoTrait(ref a), AutoTrait(ref b)) if a == b => Ok(AutoTrait(*a)),
594 _ => Err(TypeError::ExistentialMismatch(expected_found(relation, a, b)))
597 Ok(tcx.mk_existential_predicates(v)?)
601 impl<'tcx> Relate<'tcx> for ty::ClosureSubsts<'tcx> {
602 fn relate<'a, 'gcx, R>(relation: &mut R,
603 a: &ty::ClosureSubsts<'tcx>,
604 b: &ty::ClosureSubsts<'tcx>)
605 -> RelateResult<'tcx, ty::ClosureSubsts<'tcx>>
606 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
608 let substs = relate_substs(relation, None, a.substs, b.substs)?;
609 Ok(ty::ClosureSubsts { substs })
613 impl<'tcx> Relate<'tcx> for ty::GeneratorSubsts<'tcx> {
614 fn relate<'a, 'gcx, R>(relation: &mut R,
615 a: &ty::GeneratorSubsts<'tcx>,
616 b: &ty::GeneratorSubsts<'tcx>)
617 -> RelateResult<'tcx, ty::GeneratorSubsts<'tcx>>
618 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
620 let substs = relate_substs(relation, None, a.substs, b.substs)?;
621 Ok(ty::GeneratorSubsts { substs })
625 impl<'tcx> Relate<'tcx> for &'tcx Substs<'tcx> {
626 fn relate<'a, 'gcx, R>(relation: &mut R,
627 a: &&'tcx Substs<'tcx>,
628 b: &&'tcx Substs<'tcx>)
629 -> RelateResult<'tcx, &'tcx Substs<'tcx>>
630 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
632 relate_substs(relation, None, a, b)
636 impl<'tcx> Relate<'tcx> for ty::Region<'tcx> {
637 fn relate<'a, 'gcx, R>(relation: &mut R,
638 a: &ty::Region<'tcx>,
639 b: &ty::Region<'tcx>)
640 -> RelateResult<'tcx, ty::Region<'tcx>>
641 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
643 relation.regions(*a, *b)
647 impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for ty::Binder<T> {
648 fn relate<'a, 'gcx, R>(relation: &mut R,
651 -> RelateResult<'tcx, ty::Binder<T>>
652 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
654 relation.binders(a, b)
658 impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for Rc<T> {
659 fn relate<'a, 'gcx, R>(relation: &mut R,
662 -> RelateResult<'tcx, Rc<T>>
663 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
667 Ok(Rc::new(relation.relate(a, b)?))
671 impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for Box<T> {
672 fn relate<'a, 'gcx, R>(relation: &mut R,
675 -> RelateResult<'tcx, Box<T>>
676 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
680 Ok(Box::new(relation.relate(a, b)?))
684 impl<'tcx> Relate<'tcx> for Kind<'tcx> {
685 fn relate<'a, 'gcx, R>(
689 ) -> RelateResult<'tcx, Kind<'tcx>>
691 R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a,
693 match (a.unpack(), b.unpack()) {
694 (UnpackedKind::Lifetime(a_lt), UnpackedKind::Lifetime(b_lt)) => {
695 Ok(relation.relate(&a_lt, &b_lt)?.into())
697 (UnpackedKind::Type(a_ty), UnpackedKind::Type(b_ty)) => {
698 Ok(relation.relate(&a_ty, &b_ty)?.into())
700 (UnpackedKind::Lifetime(_), _) | (UnpackedKind::Type(_), _) => bug!()
705 ///////////////////////////////////////////////////////////////////////////
708 pub fn expected_found<'a, 'gcx, 'tcx, R, T>(relation: &mut R,
712 where R: TypeRelation<'a, 'gcx, 'tcx>, T: Clone, 'gcx: 'a+'tcx, 'tcx: 'a
714 expected_found_bool(relation.a_is_expected(), a, b)
717 pub fn expected_found_bool<T>(a_is_expected: bool,
726 ExpectedFound {expected: a, found: b}
728 ExpectedFound {expected: b, found: a}