1 use super::SubregionOrigin;
2 use super::combine::{CombineFields, RelationDir};
4 use crate::traits::Obligation;
5 use crate::ty::{self, Ty, TyCtxt};
7 use crate::ty::fold::TypeFoldable;
8 use crate::ty::relate::{Cause, Relate, RelateResult, TypeRelation};
11 /// Ensures `a` is made a subtype of `b`. Returns `a` on success.
12 pub struct Sub<'combine, 'infcx: 'combine, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
13 fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>,
17 impl<'combine, 'infcx, 'gcx, 'tcx> Sub<'combine, 'infcx, 'gcx, 'tcx> {
18 pub fn new(f: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool)
19 -> Sub<'combine, 'infcx, 'gcx, 'tcx>
21 Sub { fields: f, a_is_expected: a_is_expected }
24 fn with_expected_switched<R, F: FnOnce(&mut Self) -> R>(&mut self, f: F) -> R {
25 self.a_is_expected = !self.a_is_expected;
27 self.a_is_expected = !self.a_is_expected;
32 impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
33 for Sub<'combine, 'infcx, 'gcx, 'tcx>
35 fn tag(&self) -> &'static str { "Sub" }
36 fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.infcx.tcx }
37 fn a_is_expected(&self) -> bool { self.a_is_expected }
39 fn with_cause<F,R>(&mut self, cause: Cause, f: F) -> R
40 where F: FnOnce(&mut Self) -> R
42 debug!("sub with_cause={:?}", cause);
43 let old_cause = mem::replace(&mut self.fields.cause, Some(cause));
45 debug!("sub old_cause={:?}", old_cause);
46 self.fields.cause = old_cause;
50 fn relate_with_variance<T: Relate<'tcx>>(&mut self,
51 variance: ty::Variance,
54 -> RelateResult<'tcx, T>
57 ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
58 ty::Covariant => self.relate(a, b),
59 ty::Bivariant => Ok(a.clone()),
60 ty::Contravariant => self.with_expected_switched(|this| { this.relate(b, a) }),
64 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
65 debug!("{}.tys({:?}, {:?})", self.tag(), a, b);
67 if a == b { return Ok(a); }
69 let infcx = self.fields.infcx;
70 let a = infcx.type_variables.borrow_mut().replace_if_possible(a);
71 let b = infcx.type_variables.borrow_mut().replace_if_possible(b);
72 match (&a.sty, &b.sty) {
73 (&ty::Infer(TyVar(a_vid)), &ty::Infer(TyVar(b_vid))) => {
74 // Shouldn't have any LBR here, so we can safely put
75 // this under a binder below without fear of accidental
77 assert!(!a.has_escaping_bound_vars());
78 assert!(!b.has_escaping_bound_vars());
80 // can't make progress on `A <: B` if both A and B are
81 // type variables, so record an obligation. We also
82 // have to record in the `type_variables` tracker that
83 // the two variables are equal modulo subtyping, which
84 // is important to the occurs check later on.
85 infcx.type_variables.borrow_mut().sub(a_vid, b_vid);
86 self.fields.obligations.push(
88 self.fields.trace.cause.clone(),
89 self.fields.param_env,
90 ty::Predicate::Subtype(
91 ty::Binder::dummy(ty::SubtypePredicate {
92 a_is_expected: self.a_is_expected,
99 (&ty::Infer(TyVar(a_id)), _) => {
101 .instantiate(b, RelationDir::SupertypeOf, a_id, !self.a_is_expected)?;
104 (_, &ty::Infer(TyVar(b_id))) => {
105 self.fields.instantiate(a, RelationDir::SubtypeOf, b_id, self.a_is_expected)?;
109 (&ty::Error, _) | (_, &ty::Error) => {
110 infcx.set_tainted_by_errors();
111 Ok(self.tcx().types.err)
115 self.fields.infcx.super_combine_tys(self, a, b)?;
121 fn regions(&mut self, a: ty::Region<'tcx>, b: ty::Region<'tcx>)
122 -> RelateResult<'tcx, ty::Region<'tcx>> {
123 debug!("{}.regions({:?}, {:?}) self.cause={:?}",
124 self.tag(), a, b, self.fields.cause);
126 // FIXME -- we have more fine-grained information available
127 // from the "cause" field, we could perhaps give more tailored
129 let origin = SubregionOrigin::Subtype(self.fields.trace.clone());
130 self.fields.infcx.borrow_region_constraints()
131 .make_subregion(origin, a, b);
136 fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
137 -> RelateResult<'tcx, ty::Binder<T>>
138 where T: Relate<'tcx>
140 self.fields.higher_ranked_sub(a, b, self.a_is_expected)