Skip to content

Commit

Permalink
Refactor and cleanup inference code: s/get_ref()/fields/, use try! ma…
Browse files Browse the repository at this point in the history
…cro rather than if_ok!
  • Loading branch information
nikomatsakis committed Aug 28, 2014
1 parent ae31451 commit 790d9c4
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 131 deletions.
1 change: 0 additions & 1 deletion src/librustc/middle/ty.rs
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions src/librustc/middle/typeck/infer/coercion.rs
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
})
}
Expand All @@ -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
Expand Down
64 changes: 32 additions & 32 deletions src/librustc/middle/typeck/infer/combine.rs
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -185,17 +185,17 @@ 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)
}
}

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})
Expand All @@ -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)
}

Expand All @@ -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,
Expand Down Expand Up @@ -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 })
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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))
Expand All @@ -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(),
Expand All @@ -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))
}

Expand All @@ -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))
}
Expand Down Expand Up @@ -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))
Expand All @@ -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))
}
}
56 changes: 30 additions & 26 deletions src/librustc/middle/typeck/infer/glb.rs
Expand Up @@ -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(),
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit 790d9c4

Please sign in to comment.