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;
23 use syntax_pos::DUMMY_SP;
26 use rustc_target::spec::abi;
30 pub type RelateResult<'tcx, T> = Result<T, TypeError<'tcx>>;
32 #[derive(Clone, Debug)]
34 ExistentialRegionBound, // relating an existential region bound
37 pub trait TypeRelation<'a, 'gcx: 'a+'tcx, 'tcx: 'a> : Sized {
38 fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx>;
40 /// Returns a static string we can use for printouts.
41 fn tag(&self) -> &'static str;
43 /// Returns true if the value `a` is the "expected" type in the
44 /// relation. Just affects error messages.
45 fn a_is_expected(&self) -> bool;
47 fn with_cause<F,R>(&mut self, _cause: Cause, f: F) -> R
48 where F: FnOnce(&mut Self) -> R
53 /// Generic relation routine suitable for most anything.
54 fn relate<T: Relate<'tcx>>(&mut self, a: &T, b: &T) -> RelateResult<'tcx, T> {
55 Relate::relate(self, a, b)
58 /// Relate the two substitutions for the given item. The default
59 /// is to look up the variance for the item and proceed
61 fn relate_item_substs(&mut self,
63 a_subst: &'tcx Substs<'tcx>,
64 b_subst: &'tcx Substs<'tcx>)
65 -> RelateResult<'tcx, &'tcx Substs<'tcx>>
67 debug!("relate_item_substs(item_def_id={:?}, a_subst={:?}, b_subst={:?})",
72 let opt_variances = self.tcx().variances_of(item_def_id);
73 relate_substs(self, Some(&opt_variances), a_subst, b_subst)
76 /// Switch variance for the purpose of relating `a` and `b`.
77 fn relate_with_variance<T: Relate<'tcx>>(&mut self,
78 variance: ty::Variance,
81 -> RelateResult<'tcx, T>;
83 // Overrideable relations. You shouldn't typically call these
84 // directly, instead call `relate()`, which in turn calls
85 // these. This is both more uniform but also allows us to add
86 // additional hooks for other types in the future if needed
87 // without making older code, which called `relate`, obsolete.
89 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>)
90 -> RelateResult<'tcx, Ty<'tcx>>;
92 fn regions(&mut self, a: ty::Region<'tcx>, b: ty::Region<'tcx>)
93 -> RelateResult<'tcx, ty::Region<'tcx>>;
95 fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
96 -> RelateResult<'tcx, ty::Binder<T>>
97 where T: Relate<'tcx>;
100 pub trait Relate<'tcx>: TypeFoldable<'tcx> {
101 fn relate<'a, 'gcx, R>(relation: &mut R, a: &Self, b: &Self)
102 -> RelateResult<'tcx, Self>
103 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a;
106 ///////////////////////////////////////////////////////////////////////////
109 impl<'tcx> Relate<'tcx> for ty::TypeAndMut<'tcx> {
110 fn relate<'a, 'gcx, R>(relation: &mut R,
111 a: &ty::TypeAndMut<'tcx>,
112 b: &ty::TypeAndMut<'tcx>)
113 -> RelateResult<'tcx, ty::TypeAndMut<'tcx>>
114 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
116 debug!("{}.mts({:?}, {:?})",
120 if a.mutbl != b.mutbl {
121 Err(TypeError::Mutability)
124 let variance = match mutbl {
125 ast::Mutability::MutImmutable => ty::Covariant,
126 ast::Mutability::MutMutable => ty::Invariant,
128 let ty = relation.relate_with_variance(variance, &a.ty, &b.ty)?;
129 Ok(ty::TypeAndMut {ty: ty, mutbl: mutbl})
134 pub fn relate_substs<'a, 'gcx, 'tcx, R>(relation: &mut R,
135 variances: Option<&Vec<ty::Variance>>,
136 a_subst: &'tcx Substs<'tcx>,
137 b_subst: &'tcx Substs<'tcx>)
138 -> RelateResult<'tcx, &'tcx Substs<'tcx>>
139 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
141 let tcx = relation.tcx();
143 let params = a_subst.iter().zip(b_subst).enumerate().map(|(i, (a, b))| {
144 let variance = variances.map_or(ty::Invariant, |v| v[i]);
145 relation.relate_with_variance(variance, a, b)
148 Ok(tcx.mk_substs(params)?)
151 impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> {
152 fn relate<'a, 'gcx, R>(relation: &mut R,
155 -> RelateResult<'tcx, ty::FnSig<'tcx>>
156 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
158 let tcx = relation.tcx();
160 if a.variadic != b.variadic {
161 return Err(TypeError::VariadicMismatch(
162 expected_found(relation, &a.variadic, &b.variadic)));
164 let unsafety = relation.relate(&a.unsafety, &b.unsafety)?;
165 let abi = relation.relate(&a.abi, &b.abi)?;
167 if a.inputs().len() != b.inputs().len() {
168 return Err(TypeError::ArgCount);
171 let inputs_and_output = a.inputs().iter().cloned()
172 .zip(b.inputs().iter().cloned())
174 .chain(iter::once(((a.output(), b.output()), true)))
175 .map(|((a, b), is_output)| {
177 relation.relate(&a, &b)
179 relation.relate_with_variance(ty::Contravariant, &a, &b)
183 inputs_and_output: tcx.mk_type_list(inputs_and_output)?,
184 variadic: a.variadic,
191 impl<'tcx> Relate<'tcx> for ast::Unsafety {
192 fn relate<'a, 'gcx, R>(relation: &mut R,
195 -> RelateResult<'tcx, ast::Unsafety>
196 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
199 Err(TypeError::UnsafetyMismatch(expected_found(relation, a, b)))
206 impl<'tcx> Relate<'tcx> for abi::Abi {
207 fn relate<'a, 'gcx, R>(relation: &mut R,
210 -> RelateResult<'tcx, abi::Abi>
211 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
216 Err(TypeError::AbiMismatch(expected_found(relation, a, b)))
221 impl<'tcx> Relate<'tcx> for ty::ProjectionTy<'tcx> {
222 fn relate<'a, 'gcx, R>(relation: &mut R,
223 a: &ty::ProjectionTy<'tcx>,
224 b: &ty::ProjectionTy<'tcx>)
225 -> RelateResult<'tcx, ty::ProjectionTy<'tcx>>
226 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
228 if a.item_def_id != b.item_def_id {
229 Err(TypeError::ProjectionMismatched(
230 expected_found(relation, &a.item_def_id, &b.item_def_id)))
232 let substs = relation.relate(&a.substs, &b.substs)?;
233 Ok(ty::ProjectionTy {
234 item_def_id: a.item_def_id,
241 impl<'tcx> Relate<'tcx> for ty::ExistentialProjection<'tcx> {
242 fn relate<'a, 'gcx, R>(relation: &mut R,
243 a: &ty::ExistentialProjection<'tcx>,
244 b: &ty::ExistentialProjection<'tcx>)
245 -> RelateResult<'tcx, ty::ExistentialProjection<'tcx>>
246 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
248 if a.item_def_id != b.item_def_id {
249 Err(TypeError::ProjectionMismatched(
250 expected_found(relation, &a.item_def_id, &b.item_def_id)))
252 let ty = relation.relate(&a.ty, &b.ty)?;
253 let substs = relation.relate(&a.substs, &b.substs)?;
254 Ok(ty::ExistentialProjection {
255 item_def_id: a.item_def_id,
263 impl<'tcx> Relate<'tcx> for Vec<ty::PolyExistentialProjection<'tcx>> {
264 fn relate<'a, 'gcx, R>(relation: &mut R,
265 a: &Vec<ty::PolyExistentialProjection<'tcx>>,
266 b: &Vec<ty::PolyExistentialProjection<'tcx>>)
267 -> RelateResult<'tcx, Vec<ty::PolyExistentialProjection<'tcx>>>
268 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
270 // To be compatible, `a` and `b` must be for precisely the
271 // same set of traits and item names. We always require that
272 // projection bounds lists are sorted by trait-def-id and item-name,
273 // so we can just iterate through the lists pairwise, so long as they are the
275 if a.len() != b.len() {
276 Err(TypeError::ProjectionBoundsLength(expected_found(relation, &a.len(), &b.len())))
280 .map(|(a, b)| relation.relate(a, b))
286 impl<'tcx> Relate<'tcx> for ty::TraitRef<'tcx> {
287 fn relate<'a, 'gcx, R>(relation: &mut R,
288 a: &ty::TraitRef<'tcx>,
289 b: &ty::TraitRef<'tcx>)
290 -> RelateResult<'tcx, ty::TraitRef<'tcx>>
291 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
293 // Different traits cannot be related
294 if a.def_id != b.def_id {
295 Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
297 let substs = relate_substs(relation, None, a.substs, b.substs)?;
298 Ok(ty::TraitRef { def_id: a.def_id, substs: substs })
303 impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
304 fn relate<'a, 'gcx, R>(relation: &mut R,
305 a: &ty::ExistentialTraitRef<'tcx>,
306 b: &ty::ExistentialTraitRef<'tcx>)
307 -> RelateResult<'tcx, ty::ExistentialTraitRef<'tcx>>
308 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
310 // Different traits cannot be related
311 if a.def_id != b.def_id {
312 Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
314 let substs = relate_substs(relation, None, a.substs, b.substs)?;
315 Ok(ty::ExistentialTraitRef { def_id: a.def_id, substs: substs })
320 #[derive(Debug, Clone)]
321 struct GeneratorWitness<'tcx>(&'tcx ty::List<Ty<'tcx>>);
323 TupleStructTypeFoldableImpl! {
324 impl<'tcx> TypeFoldable<'tcx> for GeneratorWitness<'tcx> {
329 impl<'tcx> Relate<'tcx> for GeneratorWitness<'tcx> {
330 fn relate<'a, 'gcx, R>(relation: &mut R,
331 a: &GeneratorWitness<'tcx>,
332 b: &GeneratorWitness<'tcx>)
333 -> RelateResult<'tcx, GeneratorWitness<'tcx>>
334 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
336 assert_eq!(a.0.len(), b.0.len());
337 let tcx = relation.tcx();
338 let types = tcx.mk_type_list(a.0.iter().zip(b.0).map(|(a, b)| relation.relate(a, b)))?;
339 Ok(GeneratorWitness(types))
343 impl<'tcx> Relate<'tcx> for Ty<'tcx> {
344 fn relate<'a, 'gcx, R>(relation: &mut R,
347 -> RelateResult<'tcx, Ty<'tcx>>
348 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
354 /// The main "type relation" routine. Note that this does not handle
355 /// inference artifacts, so you should filter those out before calling
357 pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
360 -> RelateResult<'tcx, Ty<'tcx>>
361 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
363 let tcx = relation.tcx();
366 debug!("super_relate_tys: a_sty={:?} b_sty={:?}", a_sty, b_sty);
367 match (a_sty, b_sty) {
369 (_, &ty::Infer(_)) =>
371 // The caller should handle these cases!
372 bug!("var types encountered in super_relate_tys")
375 (ty::Bound(..), _) | (_, ty::Bound(..)) => {
376 bug!("bound types encountered in super_relate_tys")
379 (&ty::Error, _) | (_, &ty::Error) =>
396 (&ty::Param(ref a_p), &ty::Param(ref b_p))
397 if a_p.idx == b_p.idx =>
402 (ty::Placeholder(p1), ty::Placeholder(p2)) if p1 == p2 => {
406 (&ty::Adt(a_def, a_substs), &ty::Adt(b_def, b_substs))
409 let substs = relation.relate_item_substs(a_def.did, a_substs, b_substs)?;
410 Ok(tcx.mk_adt(a_def, substs))
413 (&ty::Foreign(a_id), &ty::Foreign(b_id))
416 Ok(tcx.mk_foreign(a_id))
419 (&ty::Dynamic(ref a_obj, ref a_region), &ty::Dynamic(ref b_obj, ref b_region)) => {
420 let region_bound = relation.with_cause(Cause::ExistentialRegionBound,
422 relation.relate_with_variance(
427 Ok(tcx.mk_dynamic(relation.relate(a_obj, b_obj)?, region_bound))
430 (&ty::Generator(a_id, a_substs, movability),
431 &ty::Generator(b_id, b_substs, _))
434 // All Generator types with the same id represent
435 // the (anonymous) type of the same generator expression. So
436 // all of their regions should be equated.
437 let substs = relation.relate(&a_substs, &b_substs)?;
438 Ok(tcx.mk_generator(a_id, substs, movability))
441 (&ty::GeneratorWitness(a_types), &ty::GeneratorWitness(b_types)) =>
443 // Wrap our types with a temporary GeneratorWitness struct
444 // inside the binder so we can related them
445 let a_types = a_types.map_bound(GeneratorWitness);
446 let b_types = b_types.map_bound(GeneratorWitness);
447 // Then remove the GeneratorWitness for the result
448 let types = relation.relate(&a_types, &b_types)?.map_bound(|witness| witness.0);
449 Ok(tcx.mk_generator_witness(types))
452 (&ty::Closure(a_id, a_substs),
453 &ty::Closure(b_id, b_substs))
456 // All Closure types with the same id represent
457 // the (anonymous) type of the same closure expression. So
458 // all of their regions should be equated.
459 let substs = relation.relate(&a_substs, &b_substs)?;
460 Ok(tcx.mk_closure(a_id, substs))
463 (&ty::RawPtr(ref a_mt), &ty::RawPtr(ref b_mt)) =>
465 let mt = relation.relate(a_mt, b_mt)?;
469 (&ty::Ref(a_r, a_ty, a_mutbl), &ty::Ref(b_r, b_ty, b_mutbl)) =>
471 let r = relation.relate_with_variance(ty::Contravariant, &a_r, &b_r)?;
472 let a_mt = ty::TypeAndMut { ty: a_ty, mutbl: a_mutbl };
473 let b_mt = ty::TypeAndMut { ty: b_ty, mutbl: b_mutbl };
474 let mt = relation.relate(&a_mt, &b_mt)?;
475 Ok(tcx.mk_ref(r, mt))
478 (&ty::Array(a_t, sz_a), &ty::Array(b_t, sz_b)) =>
480 let t = relation.relate(&a_t, &b_t)?;
481 assert_eq!(sz_a.ty, tcx.types.usize);
482 assert_eq!(sz_b.ty, tcx.types.usize);
483 let to_u64 = |x: &'tcx ty::Const<'tcx>| -> Result<u64, ErrorReported> {
484 if let Some(s) = x.assert_usize(tcx) {
488 ConstValue::Unevaluated(def_id, substs) => {
489 // FIXME(eddyb) get the right param_env.
490 let param_env = ty::ParamEnv::empty();
491 if let Some(substs) = tcx.lift_to_global(&substs) {
492 let instance = ty::Instance::resolve(
498 if let Some(instance) = instance {
503 if let Some(s) = tcx.const_eval(param_env.and(cid))
505 .map(|c| c.unwrap_usize(tcx)) {
510 tcx.sess.delay_span_bug(tcx.def_span(def_id),
511 "array length could not be evaluated");
515 tcx.sess.delay_span_bug(DUMMY_SP,
516 &format!("arrays should not have {:?} as length", x));
521 match (to_u64(sz_a), to_u64(sz_b)) {
522 (Ok(sz_a_u64), Ok(sz_b_u64)) => {
523 if sz_a_u64 == sz_b_u64 {
524 Ok(tcx.mk_ty(ty::Array(t, sz_a)))
526 Err(TypeError::FixedArraySize(
527 expected_found(relation, &sz_a_u64, &sz_b_u64)))
530 // We reported an error or will ICE, so we can return Error.
531 (Err(ErrorReported), _) | (_, Err(ErrorReported)) => {
537 (&ty::Slice(a_t), &ty::Slice(b_t)) =>
539 let t = relation.relate(&a_t, &b_t)?;
543 (&ty::Tuple(as_), &ty::Tuple(bs)) =>
545 if as_.len() == bs.len() {
546 Ok(tcx.mk_tup(as_.iter().zip(bs).map(|(a, b)| relation.relate(a, b)))?)
547 } else if !(as_.is_empty() || bs.is_empty()) {
548 Err(TypeError::TupleSize(
549 expected_found(relation, &as_.len(), &bs.len())))
551 Err(TypeError::Sorts(expected_found(relation, &a, &b)))
555 (&ty::FnDef(a_def_id, a_substs), &ty::FnDef(b_def_id, b_substs))
556 if a_def_id == b_def_id =>
558 let substs = relation.relate_item_substs(a_def_id, a_substs, b_substs)?;
559 Ok(tcx.mk_fn_def(a_def_id, substs))
562 (&ty::FnPtr(a_fty), &ty::FnPtr(b_fty)) =>
564 let fty = relation.relate(&a_fty, &b_fty)?;
565 Ok(tcx.mk_fn_ptr(fty))
568 (ty::UnnormalizedProjection(a_data), ty::UnnormalizedProjection(b_data)) => {
569 let projection_ty = relation.relate(a_data, b_data)?;
570 Ok(tcx.mk_ty(ty::UnnormalizedProjection(projection_ty)))
573 // these two are already handled downstream in case of lazy normalization
574 (ty::Projection(a_data), ty::Projection(b_data)) => {
575 let projection_ty = relation.relate(a_data, b_data)?;
576 Ok(tcx.mk_projection(projection_ty.item_def_id, projection_ty.substs))
579 (&ty::Opaque(a_def_id, a_substs), &ty::Opaque(b_def_id, b_substs))
580 if a_def_id == b_def_id =>
582 let substs = relate_substs(relation, None, a_substs, b_substs)?;
583 Ok(tcx.mk_opaque(a_def_id, substs))
588 Err(TypeError::Sorts(expected_found(relation, &a, &b)))
593 impl<'tcx> Relate<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
594 fn relate<'a, 'gcx, R>(relation: &mut R,
597 -> RelateResult<'tcx, Self>
598 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a {
600 if a.len() != b.len() {
601 return Err(TypeError::ExistentialMismatch(expected_found(relation, a, b)));
604 let tcx = relation.tcx();
605 let v = a.iter().zip(b.iter()).map(|(ep_a, ep_b)| {
606 use ty::ExistentialPredicate::*;
607 match (*ep_a, *ep_b) {
608 (Trait(ref a), Trait(ref b)) => Ok(Trait(relation.relate(a, b)?)),
609 (Projection(ref a), Projection(ref b)) => Ok(Projection(relation.relate(a, b)?)),
610 (AutoTrait(ref a), AutoTrait(ref b)) if a == b => Ok(AutoTrait(*a)),
611 _ => Err(TypeError::ExistentialMismatch(expected_found(relation, a, b)))
614 Ok(tcx.mk_existential_predicates(v)?)
618 impl<'tcx> Relate<'tcx> for ty::ClosureSubsts<'tcx> {
619 fn relate<'a, 'gcx, R>(relation: &mut R,
620 a: &ty::ClosureSubsts<'tcx>,
621 b: &ty::ClosureSubsts<'tcx>)
622 -> RelateResult<'tcx, ty::ClosureSubsts<'tcx>>
623 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
625 let substs = relate_substs(relation, None, a.substs, b.substs)?;
626 Ok(ty::ClosureSubsts { substs })
630 impl<'tcx> Relate<'tcx> for ty::GeneratorSubsts<'tcx> {
631 fn relate<'a, 'gcx, R>(relation: &mut R,
632 a: &ty::GeneratorSubsts<'tcx>,
633 b: &ty::GeneratorSubsts<'tcx>)
634 -> RelateResult<'tcx, ty::GeneratorSubsts<'tcx>>
635 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
637 let substs = relate_substs(relation, None, a.substs, b.substs)?;
638 Ok(ty::GeneratorSubsts { substs })
642 impl<'tcx> Relate<'tcx> for &'tcx Substs<'tcx> {
643 fn relate<'a, 'gcx, R>(relation: &mut R,
644 a: &&'tcx Substs<'tcx>,
645 b: &&'tcx Substs<'tcx>)
646 -> RelateResult<'tcx, &'tcx Substs<'tcx>>
647 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
649 relate_substs(relation, None, a, b)
653 impl<'tcx> Relate<'tcx> for ty::Region<'tcx> {
654 fn relate<'a, 'gcx, R>(relation: &mut R,
655 a: &ty::Region<'tcx>,
656 b: &ty::Region<'tcx>)
657 -> RelateResult<'tcx, ty::Region<'tcx>>
658 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
660 relation.regions(*a, *b)
664 impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for ty::Binder<T> {
665 fn relate<'a, 'gcx, R>(relation: &mut R,
668 -> RelateResult<'tcx, ty::Binder<T>>
669 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
671 relation.binders(a, b)
675 impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for Rc<T> {
676 fn relate<'a, 'gcx, R>(relation: &mut R,
679 -> RelateResult<'tcx, Rc<T>>
680 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
684 Ok(Rc::new(relation.relate(a, b)?))
688 impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for Box<T> {
689 fn relate<'a, 'gcx, R>(relation: &mut R,
692 -> RelateResult<'tcx, Box<T>>
693 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
697 Ok(Box::new(relation.relate(a, b)?))
701 impl<'tcx> Relate<'tcx> for Kind<'tcx> {
702 fn relate<'a, 'gcx, R>(
706 ) -> RelateResult<'tcx, Kind<'tcx>>
708 R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a,
710 match (a.unpack(), b.unpack()) {
711 (UnpackedKind::Lifetime(a_lt), UnpackedKind::Lifetime(b_lt)) => {
712 Ok(relation.relate(&a_lt, &b_lt)?.into())
714 (UnpackedKind::Type(a_ty), UnpackedKind::Type(b_ty)) => {
715 Ok(relation.relate(&a_ty, &b_ty)?.into())
717 (UnpackedKind::Lifetime(unpacked), x) => {
718 bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x)
720 (UnpackedKind::Type(unpacked), x) => {
721 bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x)
727 impl<'tcx> Relate<'tcx> for ty::TraitPredicate<'tcx> {
728 fn relate<'a, 'gcx, R>(
730 a: &ty::TraitPredicate<'tcx>,
731 b: &ty::TraitPredicate<'tcx>
732 ) -> RelateResult<'tcx, ty::TraitPredicate<'tcx>>
733 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
735 Ok(ty::TraitPredicate {
736 trait_ref: relation.relate(&a.trait_ref, &b.trait_ref)?,
741 impl<'tcx> Relate<'tcx> for ty::ProjectionPredicate<'tcx> {
742 fn relate<'a, 'gcx, R>(
744 a: &ty::ProjectionPredicate<'tcx>,
745 b: &ty::ProjectionPredicate<'tcx>,
746 ) -> RelateResult<'tcx, ty::ProjectionPredicate<'tcx>>
747 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
749 Ok(ty::ProjectionPredicate {
750 projection_ty: relation.relate(&a.projection_ty, &b.projection_ty)?,
751 ty: relation.relate(&a.ty, &b.ty)?,
756 impl<'tcx> Relate<'tcx> for traits::WhereClause<'tcx> {
757 fn relate<'a, 'gcx, R>(
759 a: &traits::WhereClause<'tcx>,
760 b: &traits::WhereClause<'tcx>
761 ) -> RelateResult<'tcx, traits::WhereClause<'tcx>>
762 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
764 use traits::WhereClause::*;
766 (Implemented(a_pred), Implemented(b_pred)) => {
767 Ok(Implemented(relation.relate(a_pred, b_pred)?))
770 (ProjectionEq(a_pred), ProjectionEq(b_pred)) => {
771 Ok(ProjectionEq(relation.relate(a_pred, b_pred)?))
774 (RegionOutlives(a_pred), RegionOutlives(b_pred)) => {
775 Ok(RegionOutlives(ty::OutlivesPredicate(
776 relation.relate(&a_pred.0, &b_pred.0)?,
777 relation.relate(&a_pred.1, &b_pred.1)?,
781 (TypeOutlives(a_pred), TypeOutlives(b_pred)) => {
782 Ok(TypeOutlives(ty::OutlivesPredicate(
783 relation.relate(&a_pred.0, &b_pred.0)?,
784 relation.relate(&a_pred.1, &b_pred.1)?,
788 _ => Err(TypeError::Mismatch),
793 impl<'tcx> Relate<'tcx> for traits::WellFormed<'tcx> {
794 fn relate<'a, 'gcx, R>(
796 a: &traits::WellFormed<'tcx>,
797 b: &traits::WellFormed<'tcx>
798 ) -> RelateResult<'tcx, traits::WellFormed<'tcx>>
799 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
801 use traits::WellFormed::*;
803 (Trait(a_pred), Trait(b_pred)) => Ok(Trait(relation.relate(a_pred, b_pred)?)),
804 (Ty(a_ty), Ty(b_ty)) => Ok(Ty(relation.relate(a_ty, b_ty)?)),
805 _ => Err(TypeError::Mismatch),
810 impl<'tcx> Relate<'tcx> for traits::FromEnv<'tcx> {
811 fn relate<'a, 'gcx, R>(
813 a: &traits::FromEnv<'tcx>,
814 b: &traits::FromEnv<'tcx>
815 ) -> RelateResult<'tcx, traits::FromEnv<'tcx>>
816 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
818 use traits::FromEnv::*;
820 (Trait(a_pred), Trait(b_pred)) => Ok(Trait(relation.relate(a_pred, b_pred)?)),
821 (Ty(a_ty), Ty(b_ty)) => Ok(Ty(relation.relate(a_ty, b_ty)?)),
822 _ => Err(TypeError::Mismatch),
827 impl<'tcx> Relate<'tcx> for traits::DomainGoal<'tcx> {
828 fn relate<'a, 'gcx, R>(
830 a: &traits::DomainGoal<'tcx>,
831 b: &traits::DomainGoal<'tcx>
832 ) -> RelateResult<'tcx, traits::DomainGoal<'tcx>>
833 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
835 use traits::DomainGoal::*;
837 (Holds(a_wc), Holds(b_wc)) => Ok(Holds(relation.relate(a_wc, b_wc)?)),
838 (WellFormed(a_wf), WellFormed(b_wf)) => Ok(WellFormed(relation.relate(a_wf, b_wf)?)),
839 (FromEnv(a_fe), FromEnv(b_fe)) => Ok(FromEnv(relation.relate(a_fe, b_fe)?)),
841 (Normalize(a_pred), Normalize(b_pred)) => {
842 Ok(Normalize(relation.relate(a_pred, b_pred)?))
845 _ => Err(TypeError::Mismatch),
850 impl<'tcx> Relate<'tcx> for traits::Goal<'tcx> {
851 fn relate<'a, 'gcx, R>(
853 a: &traits::Goal<'tcx>,
854 b: &traits::Goal<'tcx>
855 ) -> RelateResult<'tcx, traits::Goal<'tcx>>
856 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
858 use traits::GoalKind::*;
860 (Implies(a_clauses, a_goal), Implies(b_clauses, b_goal)) => {
861 let clauses = relation.relate(a_clauses, b_clauses)?;
862 let goal = relation.relate(a_goal, b_goal)?;
863 Ok(relation.tcx().mk_goal(Implies(clauses, goal)))
866 (And(a_left, a_right), And(b_left, b_right)) => {
867 let left = relation.relate(a_left, b_left)?;
868 let right = relation.relate(a_right, b_right)?;
869 Ok(relation.tcx().mk_goal(And(left, right)))
872 (Not(a_goal), Not(b_goal)) => {
873 let goal = relation.relate(a_goal, b_goal)?;
874 Ok(relation.tcx().mk_goal(Not(goal)))
877 (DomainGoal(a_goal), DomainGoal(b_goal)) => {
878 let goal = relation.relate(a_goal, b_goal)?;
879 Ok(relation.tcx().mk_goal(DomainGoal(goal)))
882 (Quantified(a_qkind, a_goal), Quantified(b_qkind, b_goal))
883 if a_qkind == b_qkind =>
885 let goal = relation.relate(a_goal, b_goal)?;
886 Ok(relation.tcx().mk_goal(Quantified(*a_qkind, goal)))
889 (CannotProve, CannotProve) => Ok(*a),
891 _ => Err(TypeError::Mismatch),
896 impl<'tcx> Relate<'tcx> for traits::Goals<'tcx> {
897 fn relate<'a, 'gcx, R>(
899 a: &traits::Goals<'tcx>,
900 b: &traits::Goals<'tcx>
901 ) -> RelateResult<'tcx, traits::Goals<'tcx>>
902 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
904 if a.len() != b.len() {
905 return Err(TypeError::Mismatch);
908 let tcx = relation.tcx();
909 let goals = a.iter().zip(b.iter()).map(|(a, b)| relation.relate(a, b));
910 Ok(tcx.mk_goals(goals)?)
914 impl<'tcx> Relate<'tcx> for traits::Clause<'tcx> {
915 fn relate<'a, 'gcx, R>(
917 a: &traits::Clause<'tcx>,
918 b: &traits::Clause<'tcx>
919 ) -> RelateResult<'tcx, traits::Clause<'tcx>>
920 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
922 use traits::Clause::*;
924 (Implies(a_clause), Implies(b_clause)) => {
925 let clause = relation.relate(a_clause, b_clause)?;
929 (ForAll(a_clause), ForAll(b_clause)) => {
930 let clause = relation.relate(a_clause, b_clause)?;
934 _ => Err(TypeError::Mismatch),
939 impl<'tcx> Relate<'tcx> for traits::Clauses<'tcx> {
940 fn relate<'a, 'gcx, R>(
942 a: &traits::Clauses<'tcx>,
943 b: &traits::Clauses<'tcx>
944 ) -> RelateResult<'tcx, traits::Clauses<'tcx>>
945 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
947 if a.len() != b.len() {
948 return Err(TypeError::Mismatch);
951 let tcx = relation.tcx();
952 let clauses = a.iter().zip(b.iter()).map(|(a, b)| relation.relate(a, b));
953 Ok(tcx.mk_clauses(clauses)?)
957 impl<'tcx> Relate<'tcx> for traits::ProgramClause<'tcx> {
958 fn relate<'a, 'gcx, R>(
960 a: &traits::ProgramClause<'tcx>,
961 b: &traits::ProgramClause<'tcx>
962 ) -> RelateResult<'tcx, traits::ProgramClause<'tcx>>
963 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
965 Ok(traits::ProgramClause {
966 goal: relation.relate(&a.goal, &b.goal)?,
967 hypotheses: relation.relate(&a.hypotheses, &b.hypotheses)?,
968 category: traits::ProgramClauseCategory::Other,
973 impl<'tcx> Relate<'tcx> for traits::Environment<'tcx> {
974 fn relate<'a, 'gcx, R>(
976 a: &traits::Environment<'tcx>,
977 b: &traits::Environment<'tcx>
978 ) -> RelateResult<'tcx, traits::Environment<'tcx>>
979 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
981 Ok(traits::Environment {
982 clauses: relation.relate(&a.clauses, &b.clauses)?,
987 impl<'tcx, G> Relate<'tcx> for traits::InEnvironment<'tcx, G>
988 where G: Relate<'tcx>
990 fn relate<'a, 'gcx, R>(
992 a: &traits::InEnvironment<'tcx, G>,
993 b: &traits::InEnvironment<'tcx, G>
994 ) -> RelateResult<'tcx, traits::InEnvironment<'tcx, G>>
995 where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'tcx, 'tcx: 'a
997 Ok(traits::InEnvironment {
998 environment: relation.relate(&a.environment, &b.environment)?,
999 goal: relation.relate(&a.goal, &b.goal)?,
1004 ///////////////////////////////////////////////////////////////////////////
1007 pub fn expected_found<'a, 'gcx, 'tcx, R, T>(relation: &mut R,
1011 where R: TypeRelation<'a, 'gcx, 'tcx>, T: Clone, 'gcx: 'a+'tcx, 'tcx: 'a
1013 expected_found_bool(relation.a_is_expected(), a, b)
1016 pub fn expected_found_bool<T>(a_is_expected: bool,
1025 ExpectedFound {expected: a, found: b}
1027 ExpectedFound {expected: b, found: a}