Skip to content

Plumb inference obligations through librustc/middle, take 3/2 #32542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions src/librustc/infer/bivariate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use super::combine::{self, CombineFields};
use super::type_variable::{BiTo};

use traits::PredicateObligations;
use ty::{self, Ty, TyCtxt};
use ty::TyVar;
use ty::relate::{Relate, RelateResult, TypeRelation};
Expand All @@ -42,7 +43,7 @@ impl<'a, 'tcx> Bivariate<'a, 'tcx> {
}
}

impl<'a, 'tcx> TypeRelation<'a, 'tcx> for Bivariate<'a, 'tcx> {
impl<'a, 'tcx> TypeRelation<'a, 'tcx, PredicateObligations<'tcx>> for Bivariate<'a, 'tcx> {
fn tag(&self) -> &'static str { "Bivariate" }

fn tcx(&self) -> &'a TyCtxt<'tcx> { self.fields.tcx() }
Expand All @@ -52,7 +53,8 @@ impl<'a, 'tcx> TypeRelation<'a, 'tcx> for Bivariate<'a, 'tcx> {
fn relate_with_variance<T:Relate<'a,'tcx>>(&mut self,
variance: ty::Variance,
a: &T,
b: &T)
b: &T,
side_effects: &mut PredicateObligations<'tcx>)
-> RelateResult<'tcx, T>
{
match variance {
Expand All @@ -63,15 +65,17 @@ impl<'a, 'tcx> TypeRelation<'a, 'tcx> for Bivariate<'a, 'tcx> {
// Foo<B> <: Foo<A>
//
// then still A must equal B.
ty::Invariant => self.relate(a, b),
ty::Invariant => self.relate(a, b, side_effects),

ty::Covariant => self.relate(a, b),
ty::Bivariant => self.relate(a, b),
ty::Contravariant => self.relate(a, b),
ty::Covariant => self.relate(a, b, side_effects),
ty::Bivariant => self.relate(a, b, side_effects),
ty::Contravariant => self.relate(a, b, side_effects),
}
}

fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>, side_effects: &mut PredicateObligations<'tcx>)
-> RelateResult<'tcx, Ty<'tcx>>
{
debug!("{}.tys({:?}, {:?})", self.tag(),
a, b);
if a == b { return Ok(a); }
Expand All @@ -86,32 +90,35 @@ impl<'a, 'tcx> TypeRelation<'a, 'tcx> for Bivariate<'a, 'tcx> {
}

(&ty::TyInfer(TyVar(a_id)), _) => {
self.fields.instantiate(b, BiTo, a_id)?;
self.fields.instantiate(b, BiTo, a_id, side_effects)?;
Ok(a)
}

(_, &ty::TyInfer(TyVar(b_id))) => {
self.fields.instantiate(a, BiTo, b_id)?;
self.fields.instantiate(a, BiTo, b_id, side_effects)?;
Ok(a)
}

_ => {
combine::super_combine_tys(self.fields.infcx, self, a, b)
combine::super_combine_tys(self.fields.infcx, self, a, b, side_effects)
}
}
}

fn regions(&mut self, a: ty::Region, _: ty::Region) -> RelateResult<'tcx, ty::Region> {
fn regions(&mut self, a: ty::Region, _: ty::Region, _: &mut PredicateObligations<'tcx>)
-> RelateResult<'tcx, ty::Region>
{
Ok(a)
}

fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
-> RelateResult<'tcx, ty::Binder<T>>
where T: Relate<'a,'tcx>
fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>,
side_effects: &mut PredicateObligations<'tcx>)
-> RelateResult<'tcx, ty::Binder<T>>
where T: Relate<'a, 'tcx>
{
let a1 = self.tcx().erase_late_bound_regions(a);
let b1 = self.tcx().erase_late_bound_regions(b);
let c = self.relate(&a1, &b1)?;
let c = self.relate(&a1, &b1, side_effects)?;
Ok(ty::Binder(c))
}
}
28 changes: 16 additions & 12 deletions src/librustc/infer/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use super::{InferCtxt};
use super::{MiscVariable, TypeTrace};
use super::type_variable::{RelationDir, BiTo, EqTo, SubtypeOf, SupertypeOf};

use traits::PredicateObligations;
use ty::{IntType, UintType};
use ty::{self, Ty, TyCtxt};
use ty::error::TypeError;
Expand All @@ -58,12 +59,13 @@ pub struct CombineFields<'a, 'tcx: 'a> {
pub cause: Option<ty::relate::Cause>,
}

pub fn super_combine_tys<'a,'tcx:'a,R>(infcx: &InferCtxt<'a, 'tcx>,
relation: &mut R,
a: Ty<'tcx>,
b: Ty<'tcx>)
-> RelateResult<'tcx, Ty<'tcx>>
where R: TypeRelation<'a,'tcx>
pub fn super_combine_tys<'a, 'tcx, R>(infcx: &InferCtxt<'a, 'tcx>,
relation: &mut R,
a: Ty<'tcx>,
b: Ty<'tcx>,
side_effects: &mut PredicateObligations<'tcx>)
-> RelateResult<'tcx, Ty<'tcx>>
where R: TypeRelation<'a, 'tcx, PredicateObligations<'tcx>>, 'tcx: 'a
{
let a_is_expected = relation.a_is_expected();

Expand Down Expand Up @@ -112,7 +114,7 @@ pub fn super_combine_tys<'a,'tcx:'a,R>(infcx: &InferCtxt<'a, 'tcx>,


_ => {
ty::relate::super_relate_tys(relation, a, b)
ty::relate::super_relate_tys(relation, a, b, side_effects)
}
}
}
Expand Down Expand Up @@ -181,7 +183,8 @@ impl<'a, 'tcx> CombineFields<'a, 'tcx> {
pub fn instantiate(&self,
a_ty: Ty<'tcx>,
dir: RelationDir,
b_vid: ty::TyVid)
b_vid: ty::TyVid,
side_effects: &mut PredicateObligations<'tcx>)
-> RelateResult<'tcx, ()>
{
let mut stack = Vec::new();
Expand Down Expand Up @@ -251,10 +254,11 @@ impl<'a, 'tcx> CombineFields<'a, '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().relate(&a_ty, &b_ty, side_effects),
EqTo => self.equate().relate(&a_ty, &b_ty, side_effects),
SubtypeOf => self.sub().relate(&a_ty, &b_ty, side_effects),
SupertypeOf => self.sub().relate_with_variance(ty::Contravariant, &a_ty, &b_ty,
side_effects),
}?;
}

Expand Down
39 changes: 23 additions & 16 deletions src/librustc/infer/equate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::higher_ranked::HigherRankedRelations;
use super::{Subtype};
use super::type_variable::{EqTo};

use traits::PredicateObligations;
use ty::{self, Ty, TyCtxt};
use ty::TyVar;
use ty::relate::{Relate, RelateResult, TypeRelation};
Expand All @@ -28,23 +29,26 @@ impl<'a, 'tcx> Equate<'a, 'tcx> {
}
}

impl<'a, 'tcx> TypeRelation<'a,'tcx> for Equate<'a, 'tcx> {
impl<'a, 'tcx> TypeRelation<'a, 'tcx, PredicateObligations<'tcx>> for Equate<'a, 'tcx> {
fn tag(&self) -> &'static str { "Equate" }

fn tcx(&self) -> &'a TyCtxt<'tcx> { self.fields.tcx() }

fn a_is_expected(&self) -> bool { self.fields.a_is_expected }

fn relate_with_variance<T:Relate<'a,'tcx>>(&mut self,
_: ty::Variance,
a: &T,
b: &T)
-> RelateResult<'tcx, T>
fn relate_with_variance<T: Relate<'a, 'tcx>>(&mut self,
_: ty::Variance,
a: &T,
b: &T,
side_effects: &mut PredicateObligations<'tcx>)
-> RelateResult<'tcx, T>
{
self.relate(a, b)
self.relate(a, b, side_effects)
}

fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>, side_effects: &mut PredicateObligations<'tcx>)
-> RelateResult<'tcx, Ty<'tcx>>
{
debug!("{}.tys({:?}, {:?})", self.tag(),
a, b);
if a == b { return Ok(a); }
Expand All @@ -59,23 +63,25 @@ impl<'a, 'tcx> TypeRelation<'a,'tcx> for Equate<'a, 'tcx> {
}

(&ty::TyInfer(TyVar(a_id)), _) => {
self.fields.instantiate(b, EqTo, a_id)?;
self.fields.instantiate(b, EqTo, a_id, side_effects)?;
Ok(a)
}

(_, &ty::TyInfer(TyVar(b_id))) => {
self.fields.instantiate(a, EqTo, b_id)?;
self.fields.instantiate(a, EqTo, b_id, side_effects)?;
Ok(a)
}

_ => {
combine::super_combine_tys(self.fields.infcx, self, a, b)?;
combine::super_combine_tys(self.fields.infcx, self, a, b, side_effects)?;
Ok(a)
}
}
}

fn regions(&mut self, a: ty::Region, b: ty::Region) -> RelateResult<'tcx, ty::Region> {
fn regions(&mut self, a: ty::Region, b: ty::Region, _: &mut PredicateObligations<'tcx>)
-> RelateResult<'tcx, ty::Region>
{
debug!("{}.regions({:?}, {:?})",
self.tag(),
a,
Expand All @@ -85,11 +91,12 @@ impl<'a, 'tcx> TypeRelation<'a,'tcx> for Equate<'a, 'tcx> {
Ok(a)
}

fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
-> RelateResult<'tcx, ty::Binder<T>>
fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>,
side_effects: &mut PredicateObligations<'tcx>)
-> RelateResult<'tcx, ty::Binder<T>>
where T: Relate<'a, 'tcx>
{
self.fields.higher_ranked_sub(a, b)?;
self.fields.higher_ranked_sub(b, a)
self.fields.higher_ranked_sub(a, b, side_effects)?;
self.fields.higher_ranked_sub(b, a, side_effects)
}
}
41 changes: 26 additions & 15 deletions src/librustc/infer/glb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::InferCtxt;
use super::lattice::{self, LatticeDir};
use super::Subtype;

use traits::PredicateObligations;
use ty::{self, Ty, TyCtxt};
use ty::relate::{Relate, RelateResult, TypeRelation};

Expand All @@ -28,7 +29,7 @@ impl<'a, 'tcx> Glb<'a, 'tcx> {
}
}

impl<'a, 'tcx> TypeRelation<'a, 'tcx> for Glb<'a, 'tcx> {
impl<'a, 'tcx> TypeRelation<'a, 'tcx, PredicateObligations<'tcx>> for Glb<'a, 'tcx> {
fn tag(&self) -> &'static str { "Glb" }

fn tcx(&self) -> &'a TyCtxt<'tcx> { self.fields.tcx() }
Expand All @@ -38,22 +39,28 @@ impl<'a, 'tcx> TypeRelation<'a, 'tcx> for Glb<'a, 'tcx> {
fn relate_with_variance<T:Relate<'a,'tcx>>(&mut self,
variance: ty::Variance,
a: &T,
b: &T)
b: &T,
side_effects: &mut PredicateObligations<'tcx>)
-> RelateResult<'tcx, T>
{
match variance {
ty::Invariant => self.fields.equate().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::Invariant => self.fields.equate().relate(a, b, side_effects),
ty::Covariant => self.relate(a, b, side_effects),
ty::Bivariant => self.fields.bivariate().relate(a, b, side_effects),
ty::Contravariant => self.fields.lub().relate(a, b, side_effects),
}
}

fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
lattice::super_lattice_tys(self, a, b)
fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>, side_effects: &mut PredicateObligations<'tcx>)
-> RelateResult<'tcx, Ty<'tcx>>
{
lattice::super_lattice_tys(self, a, b, side_effects)
}

fn regions(&mut self, a: ty::Region, b: ty::Region) -> RelateResult<'tcx, ty::Region> {
fn regions(&mut self, a: ty::Region, b: ty::Region,
_: &mut PredicateObligations<'tcx>)
-> RelateResult<'tcx, ty::Region>
{
debug!("{}.regions({:?}, {:?})",
self.tag(),
a,
Expand All @@ -63,11 +70,12 @@ impl<'a, 'tcx> TypeRelation<'a, 'tcx> for Glb<'a, 'tcx> {
Ok(self.fields.infcx.region_vars.glb_regions(origin, a, b))
}

fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
-> RelateResult<'tcx, ty::Binder<T>>
fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>,
side_effects: &mut PredicateObligations<'tcx>)
-> RelateResult<'tcx, ty::Binder<T>>
where T: Relate<'a, 'tcx>
{
self.fields.higher_ranked_glb(a, b)
self.fields.higher_ranked_glb(a, b, side_effects)
}
}

Expand All @@ -76,10 +84,13 @@ impl<'a, 'tcx> LatticeDir<'a,'tcx> for Glb<'a, 'tcx> {
self.fields.infcx
}

fn relate_bound(&self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
fn relate_bound(&self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>,
side_effects: &mut PredicateObligations<'tcx>)
-> RelateResult<'tcx, ()>
{
let mut sub = self.fields.sub();
sub.relate(&v, &a)?;
sub.relate(&v, &b)?;
sub.relate(&v, &a, side_effects)?;
sub.relate(&v, &b, side_effects)?;
Ok(())
}
}
Loading