Refactor and cleanup inference code: s/get_ref()/fields/, use try! macro rather than if_ok!
This commit is contained in:
parent
ae314512e3
commit
790d9c4708
@ -30,7 +30,6 @@ use middle::subst::{Subst, Substs, VecPerParamSpace};
|
||||
use middle::subst;
|
||||
use middle::ty;
|
||||
use middle::typeck;
|
||||
use middle::typeck::MethodCall;
|
||||
use middle::ty_fold;
|
||||
use middle::ty_fold::{TypeFoldable,TypeFolder};
|
||||
use middle;
|
||||
|
@ -247,7 +247,7 @@ impl<'f> Coerce<'f> {
|
||||
let a_borrowed = ty::mk_rptr(self.get_ref().infcx.tcx,
|
||||
r_borrow,
|
||||
mt {ty: inner_ty, mutbl: mutbl_b});
|
||||
if_ok!(sub.tys(a_borrowed, b));
|
||||
try!(sub.tys(a_borrowed, b));
|
||||
|
||||
Ok(Some(AutoDerefRef(AutoDerefRef {
|
||||
autoderefs: 1,
|
||||
@ -273,7 +273,7 @@ impl<'f> Coerce<'f> {
|
||||
let r_borrow = self.get_ref().infcx.next_region_var(coercion);
|
||||
let unsized_ty = ty::mk_slice(self.get_ref().infcx.tcx, r_borrow,
|
||||
mt {ty: t_a, mutbl: mutbl_b});
|
||||
if_ok!(self.get_ref().infcx.try(|| sub.tys(unsized_ty, b)));
|
||||
try!(self.get_ref().infcx.try(|| sub.tys(unsized_ty, b)));
|
||||
Ok(Some(AutoDerefRef(AutoDerefRef {
|
||||
autoderefs: 0,
|
||||
autoref: Some(ty::AutoPtr(r_borrow,
|
||||
@ -316,7 +316,7 @@ impl<'f> Coerce<'f> {
|
||||
let ty = ty::mk_rptr(self.get_ref().infcx.tcx,
|
||||
r_borrow,
|
||||
ty::mt{ty: ty, mutbl: mt_b.mutbl});
|
||||
if_ok!(self.get_ref().infcx.try(|| sub.tys(ty, b)));
|
||||
try!(self.get_ref().infcx.try(|| sub.tys(ty, b)));
|
||||
debug!("Success, coerced with AutoDerefRef(1, \
|
||||
AutoPtr(AutoUnsize({:?})))", kind);
|
||||
Ok(Some(AutoDerefRef(AutoDerefRef {
|
||||
@ -334,7 +334,7 @@ impl<'f> Coerce<'f> {
|
||||
match self.unsize_ty(sty_a, t_b) {
|
||||
Some((ty, kind)) => {
|
||||
let ty = ty::mk_uniq(self.get_ref().infcx.tcx, ty);
|
||||
if_ok!(self.get_ref().infcx.try(|| sub.tys(ty, b)));
|
||||
try!(self.get_ref().infcx.try(|| sub.tys(ty, b)));
|
||||
debug!("Success, coerced with AutoDerefRef(1, \
|
||||
AutoUnsizeUniq({:?}))", kind);
|
||||
Ok(Some(AutoDerefRef(AutoDerefRef {
|
||||
@ -458,7 +458,7 @@ impl<'f> Coerce<'f> {
|
||||
}
|
||||
};
|
||||
|
||||
if_ok!(self.subtype(a_borrowed, b));
|
||||
try!(self.subtype(a_borrowed, b));
|
||||
Ok(Some(AutoDerefRef(AutoDerefRef {
|
||||
autoderefs: 1,
|
||||
autoref: Some(AutoPtr(r_a, b_mutbl, None))
|
||||
@ -512,7 +512,7 @@ impl<'f> Coerce<'f> {
|
||||
sig: fn_ty_a.sig.clone(),
|
||||
.. *fn_ty_b
|
||||
});
|
||||
if_ok!(self.subtype(a_closure, b));
|
||||
try!(self.subtype(a_closure, b));
|
||||
Ok(Some(adj))
|
||||
})
|
||||
}
|
||||
@ -536,7 +536,7 @@ impl<'f> Coerce<'f> {
|
||||
|
||||
// check that the types which they point at are compatible
|
||||
let a_unsafe = ty::mk_ptr(self.get_ref().infcx.tcx, mt_a);
|
||||
if_ok!(self.subtype(a_unsafe, b));
|
||||
try!(self.subtype(a_unsafe, b));
|
||||
|
||||
// although references and unsafe ptrs have the same
|
||||
// representation, we still register an AutoDerefRef so that
|
||||
|
@ -121,7 +121,7 @@ pub trait Combine {
|
||||
for &space in subst::ParamSpace::all().iter() {
|
||||
let a_tps = a_subst.types.get_slice(space);
|
||||
let b_tps = b_subst.types.get_slice(space);
|
||||
let tps = if_ok!(self.tps(space, a_tps, b_tps));
|
||||
let tps = try!(self.tps(space, a_tps, b_tps));
|
||||
|
||||
let a_regions = a_subst.regions().get_slice(space);
|
||||
let b_regions = b_subst.regions().get_slice(space);
|
||||
@ -137,11 +137,11 @@ pub trait Combine {
|
||||
}
|
||||
};
|
||||
|
||||
let regions = if_ok!(relate_region_params(self,
|
||||
item_def_id,
|
||||
r_variances,
|
||||
a_regions,
|
||||
b_regions));
|
||||
let regions = try!(relate_region_params(self,
|
||||
item_def_id,
|
||||
r_variances,
|
||||
a_regions,
|
||||
b_regions));
|
||||
|
||||
substs.types.replace(space, tps);
|
||||
substs.mut_regions().replace(space, regions);
|
||||
@ -185,7 +185,7 @@ pub trait Combine {
|
||||
ty::Contravariant => this.contraregions(a_r, b_r),
|
||||
ty::Bivariant => Ok(a_r),
|
||||
};
|
||||
rs.push(if_ok!(r));
|
||||
rs.push(try!(r));
|
||||
}
|
||||
Ok(rs)
|
||||
}
|
||||
@ -193,9 +193,9 @@ pub trait Combine {
|
||||
|
||||
fn bare_fn_tys(&self, a: &ty::BareFnTy,
|
||||
b: &ty::BareFnTy) -> cres<ty::BareFnTy> {
|
||||
let fn_style = if_ok!(self.fn_styles(a.fn_style, b.fn_style));
|
||||
let abi = if_ok!(self.abi(a.abi, b.abi));
|
||||
let sig = if_ok!(self.fn_sigs(&a.sig, &b.sig));
|
||||
let fn_style = try!(self.fn_styles(a.fn_style, b.fn_style));
|
||||
let abi = try!(self.abi(a.abi, b.abi));
|
||||
let sig = try!(self.fn_sigs(&a.sig, &b.sig));
|
||||
Ok(ty::BareFnTy {fn_style: fn_style,
|
||||
abi: abi,
|
||||
sig: sig})
|
||||
@ -207,7 +207,7 @@ pub trait Combine {
|
||||
let store = match (a.store, b.store) {
|
||||
(ty::RegionTraitStore(a_r, a_m),
|
||||
ty::RegionTraitStore(b_r, b_m)) if a_m == b_m => {
|
||||
let r = if_ok!(self.contraregions(a_r, b_r));
|
||||
let r = try!(self.contraregions(a_r, b_r));
|
||||
ty::RegionTraitStore(r, a_m)
|
||||
}
|
||||
|
||||
@ -219,11 +219,11 @@ pub trait Combine {
|
||||
return Err(ty::terr_sigil_mismatch(expected_found(self, a.store, b.store)))
|
||||
}
|
||||
};
|
||||
let fn_style = if_ok!(self.fn_styles(a.fn_style, b.fn_style));
|
||||
let onceness = if_ok!(self.oncenesses(a.onceness, b.onceness));
|
||||
let bounds = if_ok!(self.existential_bounds(a.bounds, b.bounds));
|
||||
let sig = if_ok!(self.fn_sigs(&a.sig, &b.sig));
|
||||
let abi = if_ok!(self.abi(a.abi, b.abi));
|
||||
let fn_style = try!(self.fn_styles(a.fn_style, b.fn_style));
|
||||
let onceness = try!(self.oncenesses(a.onceness, b.onceness));
|
||||
let bounds = try!(self.existential_bounds(a.bounds, b.bounds));
|
||||
let sig = try!(self.fn_sigs(&a.sig, &b.sig));
|
||||
let abi = try!(self.abi(a.abi, b.abi));
|
||||
Ok(ty::ClosureTy {
|
||||
fn_style: fn_style,
|
||||
onceness: onceness,
|
||||
@ -311,7 +311,7 @@ pub trait Combine {
|
||||
Err(ty::terr_traits(
|
||||
expected_found(self, a.def_id, b.def_id)))
|
||||
} else {
|
||||
let substs = if_ok!(self.substs(a.def_id, &a.substs, &b.substs));
|
||||
let substs = try!(self.substs(a.def_id, &a.substs, &b.substs));
|
||||
Ok(ty::TraitRef { def_id: a.def_id,
|
||||
substs: substs })
|
||||
}
|
||||
@ -377,10 +377,10 @@ pub fn super_fn_sigs<C:Combine>(this: &C, a: &ty::FnSig, b: &ty::FnSig) -> cres<
|
||||
return Err(ty::terr_variadic_mismatch(expected_found(this, a.variadic, b.variadic)));
|
||||
}
|
||||
|
||||
let inputs = if_ok!(argvecs(this,
|
||||
let inputs = try!(argvecs(this,
|
||||
a.inputs.as_slice(),
|
||||
b.inputs.as_slice()));
|
||||
let output = if_ok!(this.tys(a.output, b.output));
|
||||
let output = try!(this.tys(a.output, b.output));
|
||||
Ok(FnSig {binder_id: a.binder_id,
|
||||
inputs: inputs,
|
||||
output: output,
|
||||
@ -430,7 +430,7 @@ pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
|
||||
|
||||
// Relate integral variables to other types
|
||||
(&ty::ty_infer(IntVar(a_id)), &ty::ty_infer(IntVar(b_id))) => {
|
||||
if_ok!(this.infcx().simple_vars(this.a_is_expected(),
|
||||
try!(this.infcx().simple_vars(this.a_is_expected(),
|
||||
a_id, b_id));
|
||||
Ok(a)
|
||||
}
|
||||
@ -453,7 +453,7 @@ pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
|
||||
|
||||
// Relate floating-point variables to other types
|
||||
(&ty::ty_infer(FloatVar(a_id)), &ty::ty_infer(FloatVar(b_id))) => {
|
||||
if_ok!(this.infcx().simple_vars(this.a_is_expected(),
|
||||
try!(this.infcx().simple_vars(this.a_is_expected(),
|
||||
a_id, b_id));
|
||||
Ok(a)
|
||||
}
|
||||
@ -485,7 +485,7 @@ pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
|
||||
(&ty::ty_enum(a_id, ref a_substs),
|
||||
&ty::ty_enum(b_id, ref b_substs))
|
||||
if a_id == b_id => {
|
||||
let substs = if_ok!(this.substs(a_id,
|
||||
let substs = try!(this.substs(a_id,
|
||||
a_substs,
|
||||
b_substs));
|
||||
Ok(ty::mk_enum(tcx, a_id, substs))
|
||||
@ -495,8 +495,8 @@ pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
|
||||
&ty::ty_trait(ref b_))
|
||||
if a_.def_id == b_.def_id => {
|
||||
debug!("Trying to match traits {:?} and {:?}", a, b);
|
||||
let substs = if_ok!(this.substs(a_.def_id, &a_.substs, &b_.substs));
|
||||
let bounds = if_ok!(this.existential_bounds(a_.bounds, b_.bounds));
|
||||
let substs = try!(this.substs(a_.def_id, &a_.substs, &b_.substs));
|
||||
let bounds = try!(this.existential_bounds(a_.bounds, b_.bounds));
|
||||
Ok(ty::mk_trait(tcx,
|
||||
a_.def_id,
|
||||
substs.clone(),
|
||||
@ -505,7 +505,7 @@ pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
|
||||
|
||||
(&ty::ty_struct(a_id, ref a_substs), &ty::ty_struct(b_id, ref b_substs))
|
||||
if a_id == b_id => {
|
||||
let substs = if_ok!(this.substs(a_id, a_substs, b_substs));
|
||||
let substs = try!(this.substs(a_id, a_substs, b_substs));
|
||||
Ok(ty::mk_struct(tcx, a_id, substs))
|
||||
}
|
||||
|
||||
@ -521,27 +521,27 @@ pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
|
||||
}
|
||||
|
||||
(&ty::ty_uniq(a_inner), &ty::ty_uniq(b_inner)) => {
|
||||
let typ = if_ok!(this.tys(a_inner, b_inner));
|
||||
let typ = try!(this.tys(a_inner, b_inner));
|
||||
check_ptr_to_unsized(this, a, b, a_inner, b_inner, ty::mk_uniq(tcx, typ))
|
||||
}
|
||||
|
||||
(&ty::ty_ptr(ref a_mt), &ty::ty_ptr(ref b_mt)) => {
|
||||
let mt = if_ok!(this.mts(a_mt, b_mt));
|
||||
let mt = try!(this.mts(a_mt, b_mt));
|
||||
check_ptr_to_unsized(this, a, b, a_mt.ty, b_mt.ty, ty::mk_ptr(tcx, mt))
|
||||
}
|
||||
|
||||
(&ty::ty_rptr(a_r, ref a_mt), &ty::ty_rptr(b_r, ref b_mt)) => {
|
||||
let r = if_ok!(this.contraregions(a_r, b_r));
|
||||
let r = try!(this.contraregions(a_r, b_r));
|
||||
// FIXME(14985) If we have mutable references to trait objects, we
|
||||
// used to use covariant subtyping. I have preserved this behaviour,
|
||||
// even though it is probably incorrect. So don't go down the usual
|
||||
// path which would require invariance.
|
||||
let mt = match (&ty::get(a_mt.ty).sty, &ty::get(b_mt.ty).sty) {
|
||||
(&ty::ty_trait(..), &ty::ty_trait(..)) if a_mt.mutbl == b_mt.mutbl => {
|
||||
let ty = if_ok!(this.tys(a_mt.ty, b_mt.ty));
|
||||
let ty = try!(this.tys(a_mt.ty, b_mt.ty));
|
||||
ty::mt { ty: ty, mutbl: a_mt.mutbl }
|
||||
}
|
||||
_ => if_ok!(this.mts(a_mt, b_mt))
|
||||
_ => try!(this.mts(a_mt, b_mt))
|
||||
};
|
||||
check_ptr_to_unsized(this, a, b, a_mt.ty, b_mt.ty, ty::mk_rptr(tcx, r, mt))
|
||||
}
|
||||
@ -592,7 +592,7 @@ pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
|
||||
vid: ty::IntVid,
|
||||
val: ty::IntVarValue) -> cres<ty::t>
|
||||
{
|
||||
if_ok!(this.infcx().simple_var_t(vid_is_expected, vid, val));
|
||||
try!(this.infcx().simple_var_t(vid_is_expected, vid, val));
|
||||
match val {
|
||||
IntType(v) => Ok(ty::mk_mach_int(v)),
|
||||
UintType(v) => Ok(ty::mk_mach_uint(v))
|
||||
@ -605,7 +605,7 @@ pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
|
||||
vid: ty::FloatVid,
|
||||
val: ast::FloatTy) -> cres<ty::t>
|
||||
{
|
||||
if_ok!(this.infcx().simple_var_t(vid_is_expected, vid, val));
|
||||
try!(this.infcx().simple_var_t(vid_is_expected, vid, val));
|
||||
Ok(ty::mk_mach_float(val))
|
||||
}
|
||||
}
|
||||
|
@ -29,24 +29,28 @@ use util::common::{indenter};
|
||||
use util::ppaux::mt_to_string;
|
||||
use util::ppaux::Repr;
|
||||
|
||||
pub struct Glb<'f>(pub CombineFields<'f>); // "greatest lower bound" (common subtype)
|
||||
/// "Greatest lower bound" (common subtype)
|
||||
pub struct Glb<'f> {
|
||||
pub fields: CombineFields<'f>
|
||||
}
|
||||
|
||||
impl<'f> Glb<'f> {
|
||||
pub fn get_ref<'a>(&'a self) -> &'a CombineFields<'f> { let Glb(ref v) = *self; v }
|
||||
#[allow(non_snake_case_functions)]
|
||||
pub fn Glb<'f>(cf: CombineFields<'f>) -> Glb<'f> {
|
||||
Glb { fields: cf }
|
||||
}
|
||||
|
||||
impl<'f> Combine for Glb<'f> {
|
||||
fn infcx<'a>(&'a self) -> &'a InferCtxt<'a> { self.get_ref().infcx }
|
||||
fn infcx<'a>(&'a self) -> &'a InferCtxt<'a> { self.fields.infcx }
|
||||
fn tag(&self) -> String { "glb".to_string() }
|
||||
fn a_is_expected(&self) -> bool { self.get_ref().a_is_expected }
|
||||
fn trace(&self) -> TypeTrace { self.get_ref().trace.clone() }
|
||||
fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
|
||||
fn trace(&self) -> TypeTrace { self.fields.trace.clone() }
|
||||
|
||||
fn sub<'a>(&'a self) -> Sub<'a> { Sub(self.get_ref().clone()) }
|
||||
fn lub<'a>(&'a self) -> Lub<'a> { Lub(self.get_ref().clone()) }
|
||||
fn glb<'a>(&'a self) -> Glb<'a> { Glb(self.get_ref().clone()) }
|
||||
fn sub<'a>(&'a self) -> Sub<'a> { Sub(self.fields.clone()) }
|
||||
fn lub<'a>(&'a self) -> Lub<'a> { Lub(self.fields.clone()) }
|
||||
fn glb<'a>(&'a self) -> Glb<'a> { Glb(self.fields.clone()) }
|
||||
|
||||
fn mts(&self, a: &ty::mt, b: &ty::mt) -> cres<ty::mt> {
|
||||
let tcx = self.get_ref().infcx.tcx;
|
||||
let tcx = self.fields.infcx.tcx;
|
||||
|
||||
debug!("{}.mts({}, {})",
|
||||
self.tag(),
|
||||
@ -108,10 +112,10 @@ impl<'f> Combine for Glb<'f> {
|
||||
fn regions(&self, a: ty::Region, b: ty::Region) -> cres<ty::Region> {
|
||||
debug!("{}.regions({:?}, {:?})",
|
||||
self.tag(),
|
||||
a.repr(self.get_ref().infcx.tcx),
|
||||
b.repr(self.get_ref().infcx.tcx));
|
||||
a.repr(self.fields.infcx.tcx),
|
||||
b.repr(self.fields.infcx.tcx));
|
||||
|
||||
Ok(self.get_ref().infcx.region_vars.glb_regions(Subtype(self.trace()), a, b))
|
||||
Ok(self.fields.infcx.region_vars.glb_regions(Subtype(self.trace()), a, b))
|
||||
}
|
||||
|
||||
fn contraregions(&self, a: ty::Region, b: ty::Region)
|
||||
@ -128,33 +132,33 @@ impl<'f> Combine for Glb<'f> {
|
||||
// please see the large comment in `region_inference.rs`.
|
||||
|
||||
debug!("{}.fn_sigs({:?}, {:?})",
|
||||
self.tag(), a.repr(self.get_ref().infcx.tcx), b.repr(self.get_ref().infcx.tcx));
|
||||
self.tag(), a.repr(self.fields.infcx.tcx), b.repr(self.fields.infcx.tcx));
|
||||
let _indenter = indenter();
|
||||
|
||||
// Make a mark so we can examine "all bindings that were
|
||||
// created as part of this type comparison".
|
||||
let mark = self.get_ref().infcx.region_vars.mark();
|
||||
let mark = self.fields.infcx.region_vars.mark();
|
||||
|
||||
// Instantiate each bound region with a fresh region variable.
|
||||
let (a_with_fresh, a_map) =
|
||||
self.get_ref().infcx.replace_late_bound_regions_with_fresh_regions(
|
||||
self.fields.infcx.replace_late_bound_regions_with_fresh_regions(
|
||||
self.trace(), a);
|
||||
let a_vars = var_ids(self, &a_map);
|
||||
let (b_with_fresh, b_map) =
|
||||
self.get_ref().infcx.replace_late_bound_regions_with_fresh_regions(
|
||||
self.fields.infcx.replace_late_bound_regions_with_fresh_regions(
|
||||
self.trace(), b);
|
||||
let b_vars = var_ids(self, &b_map);
|
||||
|
||||
// Collect constraints.
|
||||
let sig0 = if_ok!(super_fn_sigs(self, &a_with_fresh, &b_with_fresh));
|
||||
debug!("sig0 = {}", sig0.repr(self.get_ref().infcx.tcx));
|
||||
let sig0 = try!(super_fn_sigs(self, &a_with_fresh, &b_with_fresh));
|
||||
debug!("sig0 = {}", sig0.repr(self.fields.infcx.tcx));
|
||||
|
||||
// Generalize the regions appearing in fn_ty0 if possible
|
||||
let new_vars =
|
||||
self.get_ref().infcx.region_vars.vars_created_since_mark(mark);
|
||||
self.fields.infcx.region_vars.vars_created_since_mark(mark);
|
||||
let sig1 =
|
||||
fold_regions_in_sig(
|
||||
self.get_ref().infcx.tcx,
|
||||
self.fields.infcx.tcx,
|
||||
&sig0,
|
||||
|r| {
|
||||
generalize_region(self,
|
||||
@ -166,7 +170,7 @@ impl<'f> Combine for Glb<'f> {
|
||||
b_vars.as_slice(),
|
||||
r)
|
||||
});
|
||||
debug!("sig1 = {}", sig1.repr(self.get_ref().infcx.tcx));
|
||||
debug!("sig1 = {}", sig1.repr(self.fields.infcx.tcx));
|
||||
return Ok(sig1);
|
||||
|
||||
fn generalize_region(this: &Glb,
|
||||
@ -182,7 +186,7 @@ impl<'f> Combine for Glb<'f> {
|
||||
return r0;
|
||||
}
|
||||
|
||||
let tainted = this.get_ref().infcx.region_vars.tainted(mark, r0);
|
||||
let tainted = this.fields.infcx.region_vars.tainted(mark, r0);
|
||||
|
||||
let mut a_r = None;
|
||||
let mut b_r = None;
|
||||
@ -249,14 +253,14 @@ impl<'f> Combine for Glb<'f> {
|
||||
return ty::ReLateBound(new_binder_id, *a_br);
|
||||
}
|
||||
}
|
||||
this.get_ref().infcx.tcx.sess.span_bug(
|
||||
this.get_ref().trace.origin.span(),
|
||||
this.fields.infcx.tcx.sess.span_bug(
|
||||
this.fields.trace.origin.span(),
|
||||
format!("could not find original bound region for {:?}",
|
||||
r).as_slice())
|
||||
}
|
||||
|
||||
fn fresh_bound_variable(this: &Glb, binder_id: NodeId) -> ty::Region {
|
||||
this.get_ref().infcx.region_vars.new_bound(binder_id)
|
||||
this.fields.infcx.region_vars.new_bound(binder_id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,6 @@
|
||||
// except according to those terms.
|
||||
|
||||
/*!
|
||||
*
|
||||
* # Lattice Variables
|
||||
*
|
||||
* This file contains generic code for operating on inference variables
|
||||
@ -343,7 +342,7 @@ pub trait TyLatticeDir {
|
||||
}
|
||||
|
||||
impl<'f> LatticeDir for Lub<'f> {
|
||||
fn combine_fields<'a>(&'a self) -> CombineFields<'a> { self.get_ref().clone() }
|
||||
fn combine_fields<'a>(&'a self) -> CombineFields<'a> { self.fields.clone() }
|
||||
fn bnd<T:Clone>(&self, b: &Bounds<T>) -> Option<T> { b.ub.clone() }
|
||||
fn with_bnd<T:Clone>(&self, b: &Bounds<T>, t: T) -> Bounds<T> {
|
||||
Bounds { ub: Some(t), ..(*b).clone() }
|
||||
@ -357,7 +356,7 @@ impl<'f> TyLatticeDir for Lub<'f> {
|
||||
}
|
||||
|
||||
impl<'f> LatticeDir for Glb<'f> {
|
||||
fn combine_fields<'a>(&'a self) -> CombineFields<'a> { self.get_ref().clone() }
|
||||
fn combine_fields<'a>(&'a self) -> CombineFields<'a> { self.fields.clone() }
|
||||
fn bnd<T:Clone>(&self, b: &Bounds<T>) -> Option<T> { b.lb.clone() }
|
||||
fn with_bnd<T:Clone>(&self, b: &Bounds<T>, t: T) -> Bounds<T> {
|
||||
Bounds { lb: Some(t), ..(*b).clone() }
|
||||
|
@ -28,24 +28,28 @@ use syntax::ast::{MutMutable, MutImmutable};
|
||||
use util::ppaux::mt_to_string;
|
||||
use util::ppaux::Repr;
|
||||
|
||||
pub struct Lub<'f>(pub CombineFields<'f>); // least-upper-bound: common supertype
|
||||
/// "Least upper bound" (common supertype)
|
||||
pub struct Lub<'f> {
|
||||
pub fields: CombineFields<'f>
|
||||
}
|
||||
|
||||
impl<'f> Lub<'f> {
|
||||
pub fn get_ref<'a>(&'a self) -> &'a CombineFields<'f> { let Lub(ref v) = *self; v }
|
||||
#[allow(non_snake_case_functions)]
|
||||
pub fn Lub<'f>(cf: CombineFields<'f>) -> Lub<'f> {
|
||||
Lub { fields: cf }
|
||||
}
|
||||
|
||||
impl<'f> Combine for Lub<'f> {
|
||||
fn infcx<'a>(&'a self) -> &'a InferCtxt<'a> { self.get_ref().infcx }
|
||||
fn infcx<'a>(&'a self) -> &'a InferCtxt<'a> { self.fields.infcx }
|
||||
fn tag(&self) -> String { "lub".to_string() }
|
||||
fn a_is_expected(&self) -> bool { self.get_ref().a_is_expected }
|
||||
fn trace(&self) -> TypeTrace { self.get_ref().trace.clone() }
|
||||
fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
|
||||
fn trace(&self) -> TypeTrace { self.fields.trace.clone() }
|
||||
|
||||
fn sub<'a>(&'a self) -> Sub<'a> { Sub(self.get_ref().clone()) }
|
||||
fn lub<'a>(&'a self) -> Lub<'a> { Lub(self.get_ref().clone()) }
|
||||
fn glb<'a>(&'a self) -> Glb<'a> { Glb(self.get_ref().clone()) }
|
||||
fn sub<'a>(&'a self) -> Sub<'a> { Sub(self.fields.clone()) }
|
||||
fn lub<'a>(&'a self) -> Lub<'a> { Lub(self.fields.clone()) }
|
||||
fn glb<'a>(&'a self) -> Glb<'a> { Glb(self.fields.clone()) }
|
||||
|
||||
fn mts(&self, a: &ty::mt, b: &ty::mt) -> cres<ty::mt> {
|
||||
let tcx = self.get_ref().infcx.tcx;
|
||||
let tcx = self.fields.infcx.tcx;
|
||||
|
||||
debug!("{}.mts({}, {})",
|
||||
self.tag(),
|
||||
@ -63,7 +67,7 @@ impl<'f> Combine for Lub<'f> {
|
||||
}
|
||||
|
||||
MutMutable => {
|
||||
self.get_ref().infcx.try(|| {
|
||||
self.fields.infcx.try(|| {
|
||||
eq_tys(self, a.ty, b.ty).then(|| {
|
||||
Ok(ty::mt {ty: a.ty, mutbl: m})
|
||||
})
|
||||
@ -107,10 +111,10 @@ impl<'f> Combine for Lub<'f> {
|
||||
fn regions(&self, a: ty::Region, b: ty::Region) -> cres<ty::Region> {
|
||||
debug!("{}.regions({}, {})",
|
||||
self.tag(),
|
||||
a.repr(self.get_ref().infcx.tcx),
|
||||
b.repr(self.get_ref().infcx.tcx));
|
||||
a.repr(self.fields.infcx.tcx),
|
||||
b.repr(self.fields.infcx.tcx));
|
||||
|
||||
Ok(self.get_ref().infcx.region_vars.lub_regions(Subtype(self.trace()), a, b))
|
||||
Ok(self.fields.infcx.region_vars.lub_regions(Subtype(self.trace()), a, b))
|
||||
}
|
||||
|
||||
fn fn_sigs(&self, a: &ty::FnSig, b: &ty::FnSig) -> cres<ty::FnSig> {
|
||||
@ -119,26 +123,26 @@ impl<'f> Combine for Lub<'f> {
|
||||
|
||||
// Make a mark so we can examine "all bindings that were
|
||||
// created as part of this type comparison".
|
||||
let mark = self.get_ref().infcx.region_vars.mark();
|
||||
let mark = self.fields.infcx.region_vars.mark();
|
||||
|
||||
// Instantiate each bound region with a fresh region variable.
|
||||
let (a_with_fresh, a_map) =
|
||||
self.get_ref().infcx.replace_late_bound_regions_with_fresh_regions(
|
||||
self.fields.infcx.replace_late_bound_regions_with_fresh_regions(
|
||||
self.trace(), a);
|
||||
let (b_with_fresh, _) =
|
||||
self.get_ref().infcx.replace_late_bound_regions_with_fresh_regions(
|
||||
self.fields.infcx.replace_late_bound_regions_with_fresh_regions(
|
||||
self.trace(), b);
|
||||
|
||||
// Collect constraints.
|
||||
let sig0 = if_ok!(super_fn_sigs(self, &a_with_fresh, &b_with_fresh));
|
||||
debug!("sig0 = {}", sig0.repr(self.get_ref().infcx.tcx));
|
||||
let sig0 = try!(super_fn_sigs(self, &a_with_fresh, &b_with_fresh));
|
||||
debug!("sig0 = {}", sig0.repr(self.fields.infcx.tcx));
|
||||
|
||||
// Generalize the regions appearing in sig0 if possible
|
||||
let new_vars =
|
||||
self.get_ref().infcx.region_vars.vars_created_since_mark(mark);
|
||||
self.fields.infcx.region_vars.vars_created_since_mark(mark);
|
||||
let sig1 =
|
||||
fold_regions_in_sig(
|
||||
self.get_ref().infcx.tcx,
|
||||
self.fields.infcx.tcx,
|
||||
&sig0,
|
||||
|r| generalize_region(self, mark, new_vars.as_slice(),
|
||||
sig0.binder_id, &a_map, r));
|
||||
@ -158,7 +162,7 @@ impl<'f> Combine for Lub<'f> {
|
||||
return r0;
|
||||
}
|
||||
|
||||
let tainted = this.get_ref().infcx.region_vars.tainted(mark, r0);
|
||||
let tainted = this.fields.infcx.region_vars.tainted(mark, r0);
|
||||
|
||||
// Variables created during LUB computation which are
|
||||
// *related* to regions that pre-date the LUB computation
|
||||
@ -185,8 +189,8 @@ impl<'f> Combine for Lub<'f> {
|
||||
}
|
||||
}
|
||||
|
||||
this.get_ref().infcx.tcx.sess.span_bug(
|
||||
this.get_ref().trace.origin.span(),
|
||||
this.fields.infcx.tcx.sess.span_bug(
|
||||
this.fields.trace.origin.span(),
|
||||
format!("region {:?} is not associated with \
|
||||
any bound region from A!",
|
||||
r0).as_slice())
|
||||
|
@ -253,7 +253,7 @@ impl<'a> RegionVarBindings<'a> {
|
||||
}
|
||||
|
||||
pub fn commit(&self, snapshot: RegionSnapshot) {
|
||||
debug!("RegionVarBindings: commit()");
|
||||
debug!("RegionVarBindings: commit({})", snapshot.length);
|
||||
assert!(self.undo_log.borrow().len() > snapshot.length);
|
||||
assert!(*self.undo_log.borrow().get(snapshot.length) == OpenSnapshot);
|
||||
|
||||
|
@ -26,35 +26,41 @@ use util::ppaux::{bound_region_to_string, Repr};
|
||||
|
||||
use syntax::ast::{Onceness, FnStyle, MutImmutable, MutMutable};
|
||||
|
||||
pub struct Sub<'f>(pub CombineFields<'f>); // "subtype", "subregion" etc
|
||||
|
||||
impl<'f> Sub<'f> {
|
||||
pub fn get_ref<'a>(&'a self) -> &'a CombineFields<'f> { let Sub(ref v) = *self; v }
|
||||
/// "Greatest lower bound" (common subtype)
|
||||
pub struct Sub<'f> {
|
||||
fields: CombineFields<'f>
|
||||
}
|
||||
|
||||
#[allow(non_snake_case_functions)]
|
||||
pub fn Sub<'f>(cf: CombineFields<'f>) -> Sub<'f> {
|
||||
Sub { fields: cf }
|
||||
}
|
||||
|
||||
impl<'f> Combine for Sub<'f> {
|
||||
fn infcx<'a>(&'a self) -> &'a InferCtxt<'a> { self.get_ref().infcx }
|
||||
fn infcx<'a>(&'a self) -> &'a InferCtxt<'a> { self.fields.infcx }
|
||||
fn tag(&self) -> String { "sub".to_string() }
|
||||
fn a_is_expected(&self) -> bool { self.get_ref().a_is_expected }
|
||||
fn trace(&self) -> TypeTrace { self.get_ref().trace.clone() }
|
||||
fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
|
||||
fn trace(&self) -> TypeTrace { self.fields.trace.clone() }
|
||||
|
||||
fn sub<'a>(&'a self) -> Sub<'a> { Sub(self.get_ref().clone()) }
|
||||
fn lub<'a>(&'a self) -> Lub<'a> { Lub(self.get_ref().clone()) }
|
||||
fn glb<'a>(&'a self) -> Glb<'a> { Glb(self.get_ref().clone()) }
|
||||
fn sub<'a>(&'a self) -> Sub<'a> { Sub(self.fields.clone()) }
|
||||
fn lub<'a>(&'a self) -> Lub<'a> { Lub(self.fields.clone()) }
|
||||
fn glb<'a>(&'a self) -> Glb<'a> { Glb(self.fields.clone()) }
|
||||
|
||||
fn contratys(&self, a: ty::t, b: ty::t) -> cres<ty::t> {
|
||||
let opp = CombineFields {
|
||||
a_is_expected: !self.get_ref().a_is_expected,
|
||||
..self.get_ref().clone()
|
||||
a_is_expected: !self.fields.a_is_expected,
|
||||
..self.fields.clone()
|
||||
};
|
||||
Sub(opp).tys(b, a)
|
||||
}
|
||||
|
||||
fn contraregions(&self, a: ty::Region, b: ty::Region)
|
||||
-> cres<ty::Region> {
|
||||
-> cres<ty::Region>
|
||||
{
|
||||
let opp = CombineFields {
|
||||
a_is_expected: !self.get_ref().a_is_expected,
|
||||
..self.get_ref().clone()
|
||||
a_is_expected: !self.fields.a_is_expected,
|
||||
..self.fields.clone()
|
||||
};
|
||||
Sub(opp).regions(b, a)
|
||||
}
|
||||
@ -62,16 +68,16 @@ impl<'f> Combine for Sub<'f> {
|
||||
fn regions(&self, a: ty::Region, b: ty::Region) -> cres<ty::Region> {
|
||||
debug!("{}.regions({}, {})",
|
||||
self.tag(),
|
||||
a.repr(self.get_ref().infcx.tcx),
|
||||
b.repr(self.get_ref().infcx.tcx));
|
||||
self.get_ref().infcx.region_vars.make_subregion(Subtype(self.trace()), a, b);
|
||||
a.repr(self.fields.infcx.tcx),
|
||||
b.repr(self.fields.infcx.tcx));
|
||||
self.fields.infcx.region_vars.make_subregion(Subtype(self.trace()), a, b);
|
||||
Ok(a)
|
||||
}
|
||||
|
||||
fn mts(&self, a: &ty::mt, b: &ty::mt) -> cres<ty::mt> {
|
||||
debug!("mts({} <: {})",
|
||||
a.repr(self.get_ref().infcx.tcx),
|
||||
b.repr(self.get_ref().infcx.tcx));
|
||||
a.repr(self.fields.infcx.tcx),
|
||||
b.repr(self.fields.infcx.tcx));
|
||||
|
||||
if a.mutbl != b.mutbl {
|
||||
return Err(ty::terr_mutability);
|
||||
@ -118,7 +124,7 @@ impl<'f> Combine for Sub<'f> {
|
||||
|
||||
fn tys(&self, a: ty::t, b: ty::t) -> cres<ty::t> {
|
||||
debug!("{}.tys({}, {})", self.tag(),
|
||||
a.repr(self.get_ref().infcx.tcx), b.repr(self.get_ref().infcx.tcx));
|
||||
a.repr(self.fields.infcx.tcx), b.repr(self.fields.infcx.tcx));
|
||||
if a == b { return Ok(a); }
|
||||
let _indenter = indenter();
|
||||
match (&ty::get(a).sty, &ty::get(b).sty) {
|
||||
@ -127,7 +133,7 @@ impl<'f> Combine for Sub<'f> {
|
||||
}
|
||||
|
||||
(&ty::ty_infer(TyVar(a_id)), &ty::ty_infer(TyVar(b_id))) => {
|
||||
if_ok!(self.get_ref().var_sub_var(a_id, b_id));
|
||||
if_ok!(self.fields.var_sub_var(a_id, b_id));
|
||||
Ok(a)
|
||||
}
|
||||
// The vec/str check here and below is so that we don't unify
|
||||
@ -139,7 +145,7 @@ impl<'f> Combine for Sub<'f> {
|
||||
Err(ty::terr_sorts(expected_found(self, a, b)))
|
||||
}
|
||||
(&ty::ty_infer(TyVar(a_id)), _) => {
|
||||
if_ok!(self.get_ref().var_sub_t(a_id, b));
|
||||
if_ok!(self.fields.var_sub_t(a_id, b));
|
||||
Ok(a)
|
||||
}
|
||||
|
||||
@ -148,7 +154,7 @@ impl<'f> Combine for Sub<'f> {
|
||||
Err(ty::terr_sorts(expected_found(self, a, b)))
|
||||
}
|
||||
(_, &ty::ty_infer(TyVar(b_id))) => {
|
||||
if_ok!(self.get_ref().t_sub_var(a, b_id));
|
||||
if_ok!(self.fields.t_sub_var(a, b_id));
|
||||
Ok(a)
|
||||
}
|
||||
|
||||
@ -164,7 +170,7 @@ impl<'f> Combine for Sub<'f> {
|
||||
|
||||
fn fn_sigs(&self, a: &ty::FnSig, b: &ty::FnSig) -> cres<ty::FnSig> {
|
||||
debug!("fn_sigs(a={}, b={})",
|
||||
a.repr(self.get_ref().infcx.tcx), b.repr(self.get_ref().infcx.tcx));
|
||||
a.repr(self.fields.infcx.tcx), b.repr(self.fields.infcx.tcx));
|
||||
let _indenter = indenter();
|
||||
|
||||
// Rather than checking the subtype relationship between `a` and `b`
|
||||
@ -176,38 +182,38 @@ impl<'f> Combine for Sub<'f> {
|
||||
|
||||
// Make a mark so we can examine "all bindings that were
|
||||
// created as part of this type comparison".
|
||||
let mark = self.get_ref().infcx.region_vars.mark();
|
||||
let mark = self.fields.infcx.region_vars.mark();
|
||||
|
||||
// First, we instantiate each bound region in the subtype with a fresh
|
||||
// region variable.
|
||||
let (a_sig, _) =
|
||||
self.get_ref().infcx.replace_late_bound_regions_with_fresh_regions(
|
||||
self.fields.infcx.replace_late_bound_regions_with_fresh_regions(
|
||||
self.trace(), a);
|
||||
|
||||
// Second, we instantiate each bound region in the supertype with a
|
||||
// fresh concrete region.
|
||||
let (skol_map, b_sig) = {
|
||||
replace_late_bound_regions_in_fn_sig(self.get_ref().infcx.tcx, b, |br| {
|
||||
let skol = self.get_ref().infcx.region_vars.new_skolemized(br);
|
||||
replace_late_bound_regions_in_fn_sig(self.fields.infcx.tcx, b, |br| {
|
||||
let skol = self.fields.infcx.region_vars.new_skolemized(br);
|
||||
debug!("Bound region {} skolemized to {:?}",
|
||||
bound_region_to_string(self.get_ref().infcx.tcx, "", false, br),
|
||||
bound_region_to_string(self.fields.infcx.tcx, "", false, br),
|
||||
skol);
|
||||
skol
|
||||
})
|
||||
};
|
||||
|
||||
debug!("a_sig={}", a_sig.repr(self.get_ref().infcx.tcx));
|
||||
debug!("b_sig={}", b_sig.repr(self.get_ref().infcx.tcx));
|
||||
debug!("a_sig={}", a_sig.repr(self.fields.infcx.tcx));
|
||||
debug!("b_sig={}", b_sig.repr(self.fields.infcx.tcx));
|
||||
|
||||
// Compare types now that bound regions have been replaced.
|
||||
let sig = if_ok!(super_fn_sigs(self, &a_sig, &b_sig));
|
||||
let sig = try!(super_fn_sigs(self, &a_sig, &b_sig));
|
||||
|
||||
// Presuming type comparison succeeds, we need to check
|
||||
// that the skolemized regions do not "leak".
|
||||
let new_vars =
|
||||
self.get_ref().infcx.region_vars.vars_created_since_mark(mark);
|
||||
self.fields.infcx.region_vars.vars_created_since_mark(mark);
|
||||
for (&skol_br, &skol) in skol_map.iter() {
|
||||
let tainted = self.get_ref().infcx.region_vars.tainted(mark, skol);
|
||||
let tainted = self.fields.infcx.region_vars.tainted(mark, skol);
|
||||
for tainted_region in tainted.iter() {
|
||||
// Each skolemized should only be relatable to itself
|
||||
// or new variables:
|
||||
@ -224,16 +230,16 @@ impl<'f> Combine for Sub<'f> {
|
||||
if self.a_is_expected() {
|
||||
debug!("Not as polymorphic!");
|
||||
return Err(ty::terr_regions_insufficiently_polymorphic(
|
||||
skol_br, *tainted_region));
|
||||
skol_br, *tainted_region));
|
||||
} else {
|
||||
debug!("Overly polymorphic!");
|
||||
return Err(ty::terr_regions_overly_polymorphic(
|
||||
skol_br, *tainted_region));
|
||||
skol_br, *tainted_region));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(sig);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user