From dddaf34cfc3d8c143d4a7cd59349c7da1a2e4622 Mon Sep 17 00:00:00 2001 From: Masood Malekghassemi Date: Mon, 25 Jul 2016 15:07:57 -0700 Subject: [PATCH] Move `a_is_expected` out of `CombineFields` --- src/librustc/infer/bivariate.rs | 13 +++++---- src/librustc/infer/combine.rs | 39 ++++++++++--------------- src/librustc/infer/equate.rs | 17 ++++++----- src/librustc/infer/glb.rs | 19 ++++++------ src/librustc/infer/higher_ranked/mod.rs | 19 ++++++------ src/librustc/infer/lub.rs | 19 ++++++------ src/librustc/infer/mod.rs | 15 +++++----- src/librustc/infer/sub.rs | 27 ++++++++++------- 8 files changed, 86 insertions(+), 82 deletions(-) diff --git a/src/librustc/infer/bivariate.rs b/src/librustc/infer/bivariate.rs index 610e25e0f8a..d790391cd4c 100644 --- a/src/librustc/infer/bivariate.rs +++ b/src/librustc/infer/bivariate.rs @@ -33,12 +33,13 @@ use ty::TyVar; use ty::relate::{Relate, RelateResult, TypeRelation}; pub struct Bivariate<'infcx, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> { - fields: CombineFields<'infcx, 'gcx, 'tcx> + fields: CombineFields<'infcx, 'gcx, 'tcx>, + a_is_expected: bool, } impl<'infcx, 'gcx, 'tcx> Bivariate<'infcx, 'gcx, 'tcx> { - pub fn new(fields: CombineFields<'infcx, 'gcx, 'tcx>) -> Bivariate<'infcx, 'gcx, 'tcx> { - Bivariate { fields: fields } + pub fn new(fields: CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool) -> Bivariate<'infcx, 'gcx, 'tcx> { + Bivariate { fields: fields, a_is_expected: a_is_expected } } } @@ -47,7 +48,7 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Bivariate<'infcx, fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.tcx() } - fn a_is_expected(&self) -> bool { self.fields.a_is_expected } + fn a_is_expected(&self) -> bool { self.a_is_expected } fn relate_with_variance>(&mut self, variance: ty::Variance, @@ -86,12 +87,12 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Bivariate<'infcx, } (&ty::TyInfer(TyVar(a_id)), _) => { - self.fields.instantiate(b, BiTo, a_id)?; + self.fields.instantiate(b, BiTo, a_id, self.a_is_expected)?; Ok(a) } (_, &ty::TyInfer(TyVar(b_id))) => { - self.fields.instantiate(a, BiTo, b_id)?; + self.fields.instantiate(a, BiTo, b_id, self.a_is_expected)?; Ok(a) } diff --git a/src/librustc/infer/combine.rs b/src/librustc/infer/combine.rs index 36ea228956d..41369299e72 100644 --- a/src/librustc/infer/combine.rs +++ b/src/librustc/infer/combine.rs @@ -54,7 +54,6 @@ use syntax_pos::Span; #[derive(Clone)] pub struct CombineFields<'infcx, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> { pub infcx: &'infcx InferCtxt<'infcx, 'gcx, 'tcx>, - pub a_is_expected: bool, pub trace: TypeTrace<'tcx>, pub cause: Option, pub obligations: PredicateObligations<'tcx>, @@ -155,37 +154,31 @@ impl<'infcx, 'gcx, 'tcx> CombineFields<'infcx, 'gcx, 'tcx> { self.infcx.tcx } - pub fn switch_expected(&self) -> CombineFields<'infcx, 'gcx, 'tcx> { - CombineFields { - a_is_expected: !self.a_is_expected, - ..(*self).clone() - } + pub fn equate(&self, a_is_expected: bool) -> Equate<'infcx, 'gcx, 'tcx> { + Equate::new(self.clone(), a_is_expected) } - pub fn equate(&self) -> Equate<'infcx, 'gcx, 'tcx> { - Equate::new(self.clone()) + pub fn bivariate(&self, a_is_expected: bool) -> Bivariate<'infcx, 'gcx, 'tcx> { + Bivariate::new(self.clone(), a_is_expected) } - pub fn bivariate(&self) -> Bivariate<'infcx, 'gcx, 'tcx> { - Bivariate::new(self.clone()) + pub fn sub(&self, a_is_expected: bool) -> Sub<'infcx, 'gcx, 'tcx> { + Sub::new(self.clone(), a_is_expected) } - pub fn sub(&self) -> Sub<'infcx, 'gcx, 'tcx> { - Sub::new(self.clone()) + pub fn lub(&self, a_is_expected: bool) -> Lub<'infcx, 'gcx, 'tcx> { + Lub::new(self.clone(), a_is_expected) } - pub fn lub(&self) -> Lub<'infcx, 'gcx, 'tcx> { - Lub::new(self.clone()) - } - - pub fn glb(&self) -> Glb<'infcx, 'gcx, 'tcx> { - Glb::new(self.clone()) + pub fn glb(&self, a_is_expected: bool) -> Glb<'infcx, 'gcx, 'tcx> { + Glb::new(self.clone(), a_is_expected) } pub fn instantiate(&self, a_ty: Ty<'tcx>, dir: RelationDir, - b_vid: ty::TyVid) + b_vid: ty::TyVid, + a_is_expected: bool) -> RelateResult<'tcx, ()> { let mut stack = Vec::new(); @@ -255,10 +248,10 @@ impl<'infcx, 'gcx, 'tcx> CombineFields<'infcx, 'gcx, 'tcx> { // to associate causes/spans with each of the relations in // the stack to get this right. match dir { - BiTo => self.bivariate().relate(&a_ty, &b_ty), - EqTo => self.equate().relate(&a_ty, &b_ty), - SubtypeOf => self.sub().relate(&a_ty, &b_ty), - SupertypeOf => self.sub().relate_with_variance(ty::Contravariant, &a_ty, &b_ty), + BiTo => self.bivariate(a_is_expected).relate(&a_ty, &b_ty), + EqTo => self.equate(a_is_expected).relate(&a_ty, &b_ty), + SubtypeOf => self.sub(a_is_expected).relate(&a_ty, &b_ty), + SupertypeOf => self.sub(a_is_expected).relate_with_variance(ty::Contravariant, &a_ty, &b_ty), }?; } diff --git a/src/librustc/infer/equate.rs b/src/librustc/infer/equate.rs index f3dc44e8efd..7980ba91c5e 100644 --- a/src/librustc/infer/equate.rs +++ b/src/librustc/infer/equate.rs @@ -19,12 +19,13 @@ use traits::PredicateObligations; /// Ensures `a` is made equal to `b`. Returns `a` on success. pub struct Equate<'infcx, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> { - fields: CombineFields<'infcx, 'gcx, 'tcx> + fields: CombineFields<'infcx, 'gcx, 'tcx>, + a_is_expected: bool, } impl<'infcx, 'gcx, 'tcx> Equate<'infcx, 'gcx, 'tcx> { - pub fn new(fields: CombineFields<'infcx, 'gcx, 'tcx>) -> Equate<'infcx, 'gcx, 'tcx> { - Equate { fields: fields } + pub fn new(fields: CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool) -> Equate<'infcx, 'gcx, 'tcx> { + Equate { fields: fields, a_is_expected: a_is_expected } } pub fn obligations(self) -> PredicateObligations<'tcx> { @@ -37,7 +38,7 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Equate<'infcx, 'gc fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.tcx() } - fn a_is_expected(&self) -> bool { self.fields.a_is_expected } + fn a_is_expected(&self) -> bool { self.a_is_expected } fn relate_with_variance>(&mut self, _: ty::Variance, @@ -63,12 +64,12 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Equate<'infcx, 'gc } (&ty::TyInfer(TyVar(a_id)), _) => { - self.fields.instantiate(b, EqTo, a_id)?; + self.fields.instantiate(b, EqTo, a_id, self.a_is_expected)?; Ok(a) } (_, &ty::TyInfer(TyVar(b_id))) => { - self.fields.instantiate(a, EqTo, b_id)?; + self.fields.instantiate(a, EqTo, b_id, self.a_is_expected)?; Ok(a) } @@ -93,7 +94,7 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Equate<'infcx, 'gc -> RelateResult<'tcx, ty::Binder> where T: Relate<'tcx> { - self.fields.higher_ranked_sub(a, b)?; - self.fields.higher_ranked_sub(b, a) + self.fields.higher_ranked_sub(a, b, self.a_is_expected)?; + self.fields.higher_ranked_sub(b, a, self.a_is_expected) } } diff --git a/src/librustc/infer/glb.rs b/src/librustc/infer/glb.rs index 51386083ab3..7893846b214 100644 --- a/src/librustc/infer/glb.rs +++ b/src/librustc/infer/glb.rs @@ -19,12 +19,13 @@ use traits::PredicateObligations; /// "Greatest lower bound" (common subtype) pub struct Glb<'infcx, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> { - fields: CombineFields<'infcx, 'gcx, 'tcx> + fields: CombineFields<'infcx, 'gcx, 'tcx>, + a_is_expected: bool, } impl<'infcx, 'gcx, 'tcx> Glb<'infcx, 'gcx, 'tcx> { - pub fn new(fields: CombineFields<'infcx, 'gcx, 'tcx>) -> Glb<'infcx, 'gcx, 'tcx> { - Glb { fields: fields } + pub fn new(fields: CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool) -> Glb<'infcx, 'gcx, 'tcx> { + Glb { fields: fields, a_is_expected: a_is_expected } } pub fn obligations(self) -> PredicateObligations<'tcx> { @@ -37,7 +38,7 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Glb<'infcx, 'gcx, fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.tcx() } - fn a_is_expected(&self) -> bool { self.fields.a_is_expected } + fn a_is_expected(&self) -> bool { self.a_is_expected } fn relate_with_variance>(&mut self, variance: ty::Variance, @@ -46,10 +47,10 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Glb<'infcx, 'gcx, -> RelateResult<'tcx, T> { match variance { - ty::Invariant => self.fields.equate().relate(a, b), + ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b), ty::Covariant => self.relate(a, b), - ty::Bivariant => self.fields.bivariate().relate(a, b), - ty::Contravariant => self.fields.lub().relate(a, b), + ty::Bivariant => self.fields.bivariate(self.a_is_expected).relate(a, b), + ty::Contravariant => self.fields.lub(self.a_is_expected).relate(a, b), } } @@ -71,7 +72,7 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Glb<'infcx, 'gcx, -> RelateResult<'tcx, ty::Binder> where T: Relate<'tcx> { - self.fields.higher_ranked_glb(a, b) + self.fields.higher_ranked_glb(a, b, self.a_is_expected) } } @@ -81,7 +82,7 @@ impl<'infcx, 'gcx, 'tcx> LatticeDir<'infcx, 'gcx, 'tcx> for Glb<'infcx, 'gcx, 't } fn relate_bound(&self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> { - let mut sub = self.fields.sub(); + let mut sub = self.fields.sub(self.a_is_expected); sub.relate(&v, &a)?; sub.relate(&v, &b)?; Ok(()) diff --git a/src/librustc/infer/higher_ranked/mod.rs b/src/librustc/infer/higher_ranked/mod.rs index 03a09917c53..386c1ba272f 100644 --- a/src/librustc/infer/higher_ranked/mod.rs +++ b/src/librustc/infer/higher_ranked/mod.rs @@ -40,7 +40,7 @@ pub struct HrMatchResult { } impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> { - pub fn higher_ranked_sub(&self, a: &Binder, b: &Binder) + pub fn higher_ranked_sub(&self, a: &Binder, b: &Binder, a_is_expected: bool) -> RelateResult<'tcx, Binder> where T: Relate<'tcx> { @@ -77,11 +77,11 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> { debug!("b_prime={:?}", b_prime); // Compare types now that bound regions have been replaced. - let result = self.sub().relate(&a_prime, &b_prime)?; + let result = self.sub(a_is_expected).relate(&a_prime, &b_prime)?; // Presuming type comparison succeeds, we need to check // that the skolemized regions do not "leak". - self.infcx.leak_check(!self.a_is_expected, span, &skol_map, snapshot)?; + self.infcx.leak_check(!a_is_expected, span, &skol_map, snapshot)?; // We are finished with the skolemized regions now so pop // them off. @@ -109,7 +109,8 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> { pub fn higher_ranked_match(&self, span: Span, a_pair: &Binder<(T, U)>, - b_match: &T) + b_match: &T, + a_is_expected: bool) -> RelateResult<'tcx, HrMatchResult> where T: Relate<'tcx>, U: TypeFoldable<'tcx> @@ -129,7 +130,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> { debug!("higher_ranked_match: skol_map={:?}", skol_map); // Equate types now that bound regions have been replaced. - try!(self.equate().relate(&a_match, &b_match)); + try!(self.equate(a_is_expected).relate(&a_match, &b_match)); // Map each skolemized region to a vector of other regions that it // must be equated with. (Note that this vector may include other @@ -221,7 +222,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> { }); } - pub fn higher_ranked_lub(&self, a: &Binder, b: &Binder) + pub fn higher_ranked_lub(&self, a: &Binder, b: &Binder, a_is_expected: bool) -> RelateResult<'tcx, Binder> where T: Relate<'tcx> { @@ -239,7 +240,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> { // Collect constraints. let result0 = - self.lub().relate(&a_with_fresh, &b_with_fresh)?; + self.lub(a_is_expected).relate(&a_with_fresh, &b_with_fresh)?; let result0 = self.infcx.resolve_type_vars_if_possible(&result0); debug!("lub result0 = {:?}", result0); @@ -311,7 +312,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> { } } - pub fn higher_ranked_glb(&self, a: &Binder, b: &Binder) + pub fn higher_ranked_glb(&self, a: &Binder, b: &Binder, a_is_expected: bool) -> RelateResult<'tcx, Binder> where T: Relate<'tcx> { @@ -333,7 +334,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> { // Collect constraints. let result0 = - self.glb().relate(&a_with_fresh, &b_with_fresh)?; + self.glb(a_is_expected).relate(&a_with_fresh, &b_with_fresh)?; let result0 = self.infcx.resolve_type_vars_if_possible(&result0); debug!("glb result0 = {:?}", result0); diff --git a/src/librustc/infer/lub.rs b/src/librustc/infer/lub.rs index bd87c91dea5..6f3bd004f1f 100644 --- a/src/librustc/infer/lub.rs +++ b/src/librustc/infer/lub.rs @@ -19,12 +19,13 @@ use traits::PredicateObligations; /// "Least upper bound" (common supertype) pub struct Lub<'infcx, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> { - fields: CombineFields<'infcx, 'gcx, 'tcx> + fields: CombineFields<'infcx, 'gcx, 'tcx>, + a_is_expected: bool, } impl<'infcx, 'gcx, 'tcx> Lub<'infcx, 'gcx, 'tcx> { - pub fn new(fields: CombineFields<'infcx, 'gcx, 'tcx>) -> Lub<'infcx, 'gcx, 'tcx> { - Lub { fields: fields } + pub fn new(fields: CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool) -> Lub<'infcx, 'gcx, 'tcx> { + Lub { fields: fields, a_is_expected: a_is_expected } } pub fn obligations(self) -> PredicateObligations<'tcx> { @@ -37,7 +38,7 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Lub<'infcx, 'gcx, fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.tcx() } - fn a_is_expected(&self) -> bool { self.fields.a_is_expected } + fn a_is_expected(&self) -> bool { self.a_is_expected } fn relate_with_variance>(&mut self, variance: ty::Variance, @@ -46,10 +47,10 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Lub<'infcx, 'gcx, -> RelateResult<'tcx, T> { match variance { - ty::Invariant => self.fields.equate().relate(a, b), + ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b), ty::Covariant => self.relate(a, b), - ty::Bivariant => self.fields.bivariate().relate(a, b), - ty::Contravariant => self.fields.glb().relate(a, b), + ty::Bivariant => self.fields.bivariate(self.a_is_expected).relate(a, b), + ty::Contravariant => self.fields.glb(self.a_is_expected).relate(a, b), } } @@ -71,7 +72,7 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Lub<'infcx, 'gcx, -> RelateResult<'tcx, ty::Binder> where T: Relate<'tcx> { - self.fields.higher_ranked_lub(a, b) + self.fields.higher_ranked_lub(a, b, self.a_is_expected) } } @@ -81,7 +82,7 @@ impl<'infcx, 'gcx, 'tcx> LatticeDir<'infcx, 'gcx, 'tcx> for Lub<'infcx, 'gcx, 't } fn relate_bound(&self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> { - let mut sub = self.fields.sub(); + let mut sub = self.fields.sub(self.a_is_expected); sub.relate(&a, &v)?; sub.relate(&b, &v)?; Ok(()) diff --git a/src/librustc/infer/mod.rs b/src/librustc/infer/mod.rs index 4e0c5b58b64..9ee0da4d0ed 100644 --- a/src/librustc/infer/mod.rs +++ b/src/librustc/infer/mod.rs @@ -799,11 +799,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { return variables; } - fn combine_fields(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>) + fn combine_fields(&'a self, trace: TypeTrace<'tcx>) -> CombineFields<'a, 'gcx, 'tcx> { CombineFields { infcx: self, - a_is_expected: a_is_expected, trace: trace, cause: None, obligations: PredicateObligations::new(), @@ -814,7 +813,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { -> InferResult<'tcx, T> where T: Relate<'tcx> { - let mut equate = self.combine_fields(a_is_expected, trace).equate(); + let mut equate = self.combine_fields(trace).equate(a_is_expected); let result = equate.relate(a, b); result.map(|t| InferOk { value: t, obligations: equate.obligations() }) } @@ -823,7 +822,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { -> InferResult<'tcx, T> where T: Relate<'tcx> { - let mut sub = self.combine_fields(a_is_expected, trace).sub(); + let mut sub = self.combine_fields(trace).sub(a_is_expected); let result = sub.relate(a, b); result.map(|t| InferOk { value: t, obligations: sub.obligations() }) } @@ -832,7 +831,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { -> InferResult<'tcx, T> where T: Relate<'tcx> { - let mut lub = self.combine_fields(a_is_expected, trace).lub(); + let mut lub = self.combine_fields(trace).lub(a_is_expected); let result = lub.relate(a, b); result.map(|t| InferOk { value: t, obligations: lub.obligations() }) } @@ -841,7 +840,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { -> InferResult<'tcx, T> where T: Relate<'tcx> { - let mut glb = self.combine_fields(a_is_expected, trace).glb(); + let mut glb = self.combine_fields(trace).glb(a_is_expected); let result = glb.relate(a, b); result.map(|t| InferOk { value: t, obligations: glb.obligations() }) } @@ -1646,8 +1645,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { }; let match_pair = match_a.map_bound(|p| (p.projection_ty.trait_ref, p.ty)); - let combine = self.combine_fields(true, trace); - let result = combine.higher_ranked_match(span, &match_pair, &match_b)?; + let combine = self.combine_fields(trace); + let result = combine.higher_ranked_match(span, &match_pair, &match_b, true)?; Ok(InferOk { value: result, obligations: combine.obligations }) } diff --git a/src/librustc/infer/sub.rs b/src/librustc/infer/sub.rs index 404763b3bd3..7a25ea9f3c2 100644 --- a/src/librustc/infer/sub.rs +++ b/src/librustc/infer/sub.rs @@ -21,22 +21,30 @@ use std::mem; /// Ensures `a` is made a subtype of `b`. Returns `a` on success. pub struct Sub<'infcx, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> { fields: CombineFields<'infcx, 'gcx, 'tcx>, + a_is_expected: bool, } impl<'infcx, 'gcx, 'tcx> Sub<'infcx, 'gcx, 'tcx> { - pub fn new(f: CombineFields<'infcx, 'gcx, 'tcx>) -> Sub<'infcx, 'gcx, 'tcx> { - Sub { fields: f } + pub fn new(f: CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool) -> Sub<'infcx, 'gcx, 'tcx> { + Sub { fields: f, a_is_expected: a_is_expected } } pub fn obligations(self) -> PredicateObligations<'tcx> { self.fields.obligations } + + fn with_expected_switched R>(&mut self, f: F) -> R { + self.a_is_expected = !self.a_is_expected; + let result = f(self); + self.a_is_expected = !self.a_is_expected; + result + } } impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Sub<'infcx, 'gcx, 'tcx> { fn tag(&self) -> &'static str { "Sub" } fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.infcx.tcx } - fn a_is_expected(&self) -> bool { self.fields.a_is_expected } + fn a_is_expected(&self) -> bool { self.a_is_expected } fn with_cause(&mut self, cause: Cause, f: F) -> R where F: FnOnce(&mut Self) -> R @@ -56,10 +64,10 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Sub<'infcx, 'gcx, -> RelateResult<'tcx, T> { match variance { - ty::Invariant => self.fields.equate().relate(a, b), + ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b), ty::Covariant => self.relate(a, b), - ty::Bivariant => self.fields.bivariate().relate(a, b), - ty::Contravariant => self.fields.switch_expected().sub().relate(b, a), + ty::Bivariant => self.fields.bivariate(self.a_is_expected).relate(a, b), + ty::Contravariant => self.with_expected_switched(|this| { this.relate(b, a) }), } } @@ -80,12 +88,11 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Sub<'infcx, 'gcx, } (&ty::TyInfer(TyVar(a_id)), _) => { self.fields - .switch_expected() - .instantiate(b, SupertypeOf, a_id)?; + .instantiate(b, SupertypeOf, a_id, !self.a_is_expected)?; Ok(a) } (_, &ty::TyInfer(TyVar(b_id))) => { - self.fields.instantiate(a, SubtypeOf, b_id)?; + self.fields.instantiate(a, SubtypeOf, b_id, self.a_is_expected)?; Ok(a) } @@ -116,6 +123,6 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Sub<'infcx, 'gcx, -> RelateResult<'tcx, ty::Binder> where T: Relate<'tcx> { - self.fields.higher_ranked_sub(a, b) + self.fields.higher_ranked_sub(a, b, self.a_is_expected) } }