Skip to content

Commit 790d9c4

Browse files
committed
Refactor and cleanup inference code: s/get_ref()/fields/, use try! macro rather than if_ok!
1 parent ae31451 commit 790d9c4

File tree

8 files changed

+143
-131
lines changed

8 files changed

+143
-131
lines changed

src/librustc/middle/ty.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use middle::subst::{Subst, Substs, VecPerParamSpace};
3030
use middle::subst;
3131
use middle::ty;
3232
use middle::typeck;
33-
use middle::typeck::MethodCall;
3433
use middle::ty_fold;
3534
use middle::ty_fold::{TypeFoldable,TypeFolder};
3635
use middle;

src/librustc/middle/typeck/infer/coercion.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl<'f> Coerce<'f> {
247247
let a_borrowed = ty::mk_rptr(self.get_ref().infcx.tcx,
248248
r_borrow,
249249
mt {ty: inner_ty, mutbl: mutbl_b});
250-
if_ok!(sub.tys(a_borrowed, b));
250+
try!(sub.tys(a_borrowed, b));
251251

252252
Ok(Some(AutoDerefRef(AutoDerefRef {
253253
autoderefs: 1,
@@ -273,7 +273,7 @@ impl<'f> Coerce<'f> {
273273
let r_borrow = self.get_ref().infcx.next_region_var(coercion);
274274
let unsized_ty = ty::mk_slice(self.get_ref().infcx.tcx, r_borrow,
275275
mt {ty: t_a, mutbl: mutbl_b});
276-
if_ok!(self.get_ref().infcx.try(|| sub.tys(unsized_ty, b)));
276+
try!(self.get_ref().infcx.try(|| sub.tys(unsized_ty, b)));
277277
Ok(Some(AutoDerefRef(AutoDerefRef {
278278
autoderefs: 0,
279279
autoref: Some(ty::AutoPtr(r_borrow,
@@ -316,7 +316,7 @@ impl<'f> Coerce<'f> {
316316
let ty = ty::mk_rptr(self.get_ref().infcx.tcx,
317317
r_borrow,
318318
ty::mt{ty: ty, mutbl: mt_b.mutbl});
319-
if_ok!(self.get_ref().infcx.try(|| sub.tys(ty, b)));
319+
try!(self.get_ref().infcx.try(|| sub.tys(ty, b)));
320320
debug!("Success, coerced with AutoDerefRef(1, \
321321
AutoPtr(AutoUnsize({:?})))", kind);
322322
Ok(Some(AutoDerefRef(AutoDerefRef {
@@ -334,7 +334,7 @@ impl<'f> Coerce<'f> {
334334
match self.unsize_ty(sty_a, t_b) {
335335
Some((ty, kind)) => {
336336
let ty = ty::mk_uniq(self.get_ref().infcx.tcx, ty);
337-
if_ok!(self.get_ref().infcx.try(|| sub.tys(ty, b)));
337+
try!(self.get_ref().infcx.try(|| sub.tys(ty, b)));
338338
debug!("Success, coerced with AutoDerefRef(1, \
339339
AutoUnsizeUniq({:?}))", kind);
340340
Ok(Some(AutoDerefRef(AutoDerefRef {
@@ -458,7 +458,7 @@ impl<'f> Coerce<'f> {
458458
}
459459
};
460460

461-
if_ok!(self.subtype(a_borrowed, b));
461+
try!(self.subtype(a_borrowed, b));
462462
Ok(Some(AutoDerefRef(AutoDerefRef {
463463
autoderefs: 1,
464464
autoref: Some(AutoPtr(r_a, b_mutbl, None))
@@ -512,7 +512,7 @@ impl<'f> Coerce<'f> {
512512
sig: fn_ty_a.sig.clone(),
513513
.. *fn_ty_b
514514
});
515-
if_ok!(self.subtype(a_closure, b));
515+
try!(self.subtype(a_closure, b));
516516
Ok(Some(adj))
517517
})
518518
}
@@ -536,7 +536,7 @@ impl<'f> Coerce<'f> {
536536

537537
// check that the types which they point at are compatible
538538
let a_unsafe = ty::mk_ptr(self.get_ref().infcx.tcx, mt_a);
539-
if_ok!(self.subtype(a_unsafe, b));
539+
try!(self.subtype(a_unsafe, b));
540540

541541
// although references and unsafe ptrs have the same
542542
// representation, we still register an AutoDerefRef so that

src/librustc/middle/typeck/infer/combine.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub trait Combine {
121121
for &space in subst::ParamSpace::all().iter() {
122122
let a_tps = a_subst.types.get_slice(space);
123123
let b_tps = b_subst.types.get_slice(space);
124-
let tps = if_ok!(self.tps(space, a_tps, b_tps));
124+
let tps = try!(self.tps(space, a_tps, b_tps));
125125

126126
let a_regions = a_subst.regions().get_slice(space);
127127
let b_regions = b_subst.regions().get_slice(space);
@@ -137,11 +137,11 @@ pub trait Combine {
137137
}
138138
};
139139

140-
let regions = if_ok!(relate_region_params(self,
141-
item_def_id,
142-
r_variances,
143-
a_regions,
144-
b_regions));
140+
let regions = try!(relate_region_params(self,
141+
item_def_id,
142+
r_variances,
143+
a_regions,
144+
b_regions));
145145

146146
substs.types.replace(space, tps);
147147
substs.mut_regions().replace(space, regions);
@@ -185,17 +185,17 @@ pub trait Combine {
185185
ty::Contravariant => this.contraregions(a_r, b_r),
186186
ty::Bivariant => Ok(a_r),
187187
};
188-
rs.push(if_ok!(r));
188+
rs.push(try!(r));
189189
}
190190
Ok(rs)
191191
}
192192
}
193193

194194
fn bare_fn_tys(&self, a: &ty::BareFnTy,
195195
b: &ty::BareFnTy) -> cres<ty::BareFnTy> {
196-
let fn_style = if_ok!(self.fn_styles(a.fn_style, b.fn_style));
197-
let abi = if_ok!(self.abi(a.abi, b.abi));
198-
let sig = if_ok!(self.fn_sigs(&a.sig, &b.sig));
196+
let fn_style = try!(self.fn_styles(a.fn_style, b.fn_style));
197+
let abi = try!(self.abi(a.abi, b.abi));
198+
let sig = try!(self.fn_sigs(&a.sig, &b.sig));
199199
Ok(ty::BareFnTy {fn_style: fn_style,
200200
abi: abi,
201201
sig: sig})
@@ -207,7 +207,7 @@ pub trait Combine {
207207
let store = match (a.store, b.store) {
208208
(ty::RegionTraitStore(a_r, a_m),
209209
ty::RegionTraitStore(b_r, b_m)) if a_m == b_m => {
210-
let r = if_ok!(self.contraregions(a_r, b_r));
210+
let r = try!(self.contraregions(a_r, b_r));
211211
ty::RegionTraitStore(r, a_m)
212212
}
213213

@@ -219,11 +219,11 @@ pub trait Combine {
219219
return Err(ty::terr_sigil_mismatch(expected_found(self, a.store, b.store)))
220220
}
221221
};
222-
let fn_style = if_ok!(self.fn_styles(a.fn_style, b.fn_style));
223-
let onceness = if_ok!(self.oncenesses(a.onceness, b.onceness));
224-
let bounds = if_ok!(self.existential_bounds(a.bounds, b.bounds));
225-
let sig = if_ok!(self.fn_sigs(&a.sig, &b.sig));
226-
let abi = if_ok!(self.abi(a.abi, b.abi));
222+
let fn_style = try!(self.fn_styles(a.fn_style, b.fn_style));
223+
let onceness = try!(self.oncenesses(a.onceness, b.onceness));
224+
let bounds = try!(self.existential_bounds(a.bounds, b.bounds));
225+
let sig = try!(self.fn_sigs(&a.sig, &b.sig));
226+
let abi = try!(self.abi(a.abi, b.abi));
227227
Ok(ty::ClosureTy {
228228
fn_style: fn_style,
229229
onceness: onceness,
@@ -311,7 +311,7 @@ pub trait Combine {
311311
Err(ty::terr_traits(
312312
expected_found(self, a.def_id, b.def_id)))
313313
} else {
314-
let substs = if_ok!(self.substs(a.def_id, &a.substs, &b.substs));
314+
let substs = try!(self.substs(a.def_id, &a.substs, &b.substs));
315315
Ok(ty::TraitRef { def_id: a.def_id,
316316
substs: substs })
317317
}
@@ -377,10 +377,10 @@ pub fn super_fn_sigs<C:Combine>(this: &C, a: &ty::FnSig, b: &ty::FnSig) -> cres<
377377
return Err(ty::terr_variadic_mismatch(expected_found(this, a.variadic, b.variadic)));
378378
}
379379

380-
let inputs = if_ok!(argvecs(this,
380+
let inputs = try!(argvecs(this,
381381
a.inputs.as_slice(),
382382
b.inputs.as_slice()));
383-
let output = if_ok!(this.tys(a.output, b.output));
383+
let output = try!(this.tys(a.output, b.output));
384384
Ok(FnSig {binder_id: a.binder_id,
385385
inputs: inputs,
386386
output: output,
@@ -430,7 +430,7 @@ pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
430430

431431
// Relate integral variables to other types
432432
(&ty::ty_infer(IntVar(a_id)), &ty::ty_infer(IntVar(b_id))) => {
433-
if_ok!(this.infcx().simple_vars(this.a_is_expected(),
433+
try!(this.infcx().simple_vars(this.a_is_expected(),
434434
a_id, b_id));
435435
Ok(a)
436436
}
@@ -453,7 +453,7 @@ pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
453453

454454
// Relate floating-point variables to other types
455455
(&ty::ty_infer(FloatVar(a_id)), &ty::ty_infer(FloatVar(b_id))) => {
456-
if_ok!(this.infcx().simple_vars(this.a_is_expected(),
456+
try!(this.infcx().simple_vars(this.a_is_expected(),
457457
a_id, b_id));
458458
Ok(a)
459459
}
@@ -485,7 +485,7 @@ pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
485485
(&ty::ty_enum(a_id, ref a_substs),
486486
&ty::ty_enum(b_id, ref b_substs))
487487
if a_id == b_id => {
488-
let substs = if_ok!(this.substs(a_id,
488+
let substs = try!(this.substs(a_id,
489489
a_substs,
490490
b_substs));
491491
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> {
495495
&ty::ty_trait(ref b_))
496496
if a_.def_id == b_.def_id => {
497497
debug!("Trying to match traits {:?} and {:?}", a, b);
498-
let substs = if_ok!(this.substs(a_.def_id, &a_.substs, &b_.substs));
499-
let bounds = if_ok!(this.existential_bounds(a_.bounds, b_.bounds));
498+
let substs = try!(this.substs(a_.def_id, &a_.substs, &b_.substs));
499+
let bounds = try!(this.existential_bounds(a_.bounds, b_.bounds));
500500
Ok(ty::mk_trait(tcx,
501501
a_.def_id,
502502
substs.clone(),
@@ -505,7 +505,7 @@ pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
505505

506506
(&ty::ty_struct(a_id, ref a_substs), &ty::ty_struct(b_id, ref b_substs))
507507
if a_id == b_id => {
508-
let substs = if_ok!(this.substs(a_id, a_substs, b_substs));
508+
let substs = try!(this.substs(a_id, a_substs, b_substs));
509509
Ok(ty::mk_struct(tcx, a_id, substs))
510510
}
511511

@@ -521,27 +521,27 @@ pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
521521
}
522522

523523
(&ty::ty_uniq(a_inner), &ty::ty_uniq(b_inner)) => {
524-
let typ = if_ok!(this.tys(a_inner, b_inner));
524+
let typ = try!(this.tys(a_inner, b_inner));
525525
check_ptr_to_unsized(this, a, b, a_inner, b_inner, ty::mk_uniq(tcx, typ))
526526
}
527527

528528
(&ty::ty_ptr(ref a_mt), &ty::ty_ptr(ref b_mt)) => {
529-
let mt = if_ok!(this.mts(a_mt, b_mt));
529+
let mt = try!(this.mts(a_mt, b_mt));
530530
check_ptr_to_unsized(this, a, b, a_mt.ty, b_mt.ty, ty::mk_ptr(tcx, mt))
531531
}
532532

533533
(&ty::ty_rptr(a_r, ref a_mt), &ty::ty_rptr(b_r, ref b_mt)) => {
534-
let r = if_ok!(this.contraregions(a_r, b_r));
534+
let r = try!(this.contraregions(a_r, b_r));
535535
// FIXME(14985) If we have mutable references to trait objects, we
536536
// used to use covariant subtyping. I have preserved this behaviour,
537537
// even though it is probably incorrect. So don't go down the usual
538538
// path which would require invariance.
539539
let mt = match (&ty::get(a_mt.ty).sty, &ty::get(b_mt.ty).sty) {
540540
(&ty::ty_trait(..), &ty::ty_trait(..)) if a_mt.mutbl == b_mt.mutbl => {
541-
let ty = if_ok!(this.tys(a_mt.ty, b_mt.ty));
541+
let ty = try!(this.tys(a_mt.ty, b_mt.ty));
542542
ty::mt { ty: ty, mutbl: a_mt.mutbl }
543543
}
544-
_ => if_ok!(this.mts(a_mt, b_mt))
544+
_ => try!(this.mts(a_mt, b_mt))
545545
};
546546
check_ptr_to_unsized(this, a, b, a_mt.ty, b_mt.ty, ty::mk_rptr(tcx, r, mt))
547547
}
@@ -592,7 +592,7 @@ pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
592592
vid: ty::IntVid,
593593
val: ty::IntVarValue) -> cres<ty::t>
594594
{
595-
if_ok!(this.infcx().simple_var_t(vid_is_expected, vid, val));
595+
try!(this.infcx().simple_var_t(vid_is_expected, vid, val));
596596
match val {
597597
IntType(v) => Ok(ty::mk_mach_int(v)),
598598
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> {
605605
vid: ty::FloatVid,
606606
val: ast::FloatTy) -> cres<ty::t>
607607
{
608-
if_ok!(this.infcx().simple_var_t(vid_is_expected, vid, val));
608+
try!(this.infcx().simple_var_t(vid_is_expected, vid, val));
609609
Ok(ty::mk_mach_float(val))
610610
}
611611
}

src/librustc/middle/typeck/infer/glb.rs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,28 @@ use util::common::{indenter};
2929
use util::ppaux::mt_to_string;
3030
use util::ppaux::Repr;
3131

32-
pub struct Glb<'f>(pub CombineFields<'f>); // "greatest lower bound" (common subtype)
32+
/// "Greatest lower bound" (common subtype)
33+
pub struct Glb<'f> {
34+
pub fields: CombineFields<'f>
35+
}
3336

34-
impl<'f> Glb<'f> {
35-
pub fn get_ref<'a>(&'a self) -> &'a CombineFields<'f> { let Glb(ref v) = *self; v }
37+
#[allow(non_snake_case_functions)]
38+
pub fn Glb<'f>(cf: CombineFields<'f>) -> Glb<'f> {
39+
Glb { fields: cf }
3640
}
3741

3842
impl<'f> Combine for Glb<'f> {
39-
fn infcx<'a>(&'a self) -> &'a InferCtxt<'a> { self.get_ref().infcx }
43+
fn infcx<'a>(&'a self) -> &'a InferCtxt<'a> { self.fields.infcx }
4044
fn tag(&self) -> String { "glb".to_string() }
41-
fn a_is_expected(&self) -> bool { self.get_ref().a_is_expected }
42-
fn trace(&self) -> TypeTrace { self.get_ref().trace.clone() }
45+
fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
46+
fn trace(&self) -> TypeTrace { self.fields.trace.clone() }
4347

44-
fn sub<'a>(&'a self) -> Sub<'a> { Sub(self.get_ref().clone()) }
45-
fn lub<'a>(&'a self) -> Lub<'a> { Lub(self.get_ref().clone()) }
46-
fn glb<'a>(&'a self) -> Glb<'a> { Glb(self.get_ref().clone()) }
48+
fn sub<'a>(&'a self) -> Sub<'a> { Sub(self.fields.clone()) }
49+
fn lub<'a>(&'a self) -> Lub<'a> { Lub(self.fields.clone()) }
50+
fn glb<'a>(&'a self) -> Glb<'a> { Glb(self.fields.clone()) }
4751

4852
fn mts(&self, a: &ty::mt, b: &ty::mt) -> cres<ty::mt> {
49-
let tcx = self.get_ref().infcx.tcx;
53+
let tcx = self.fields.infcx.tcx;
5054

5155
debug!("{}.mts({}, {})",
5256
self.tag(),
@@ -108,10 +112,10 @@ impl<'f> Combine for Glb<'f> {
108112
fn regions(&self, a: ty::Region, b: ty::Region) -> cres<ty::Region> {
109113
debug!("{}.regions({:?}, {:?})",
110114
self.tag(),
111-
a.repr(self.get_ref().infcx.tcx),
112-
b.repr(self.get_ref().infcx.tcx));
115+
a.repr(self.fields.infcx.tcx),
116+
b.repr(self.fields.infcx.tcx));
113117

114-
Ok(self.get_ref().infcx.region_vars.glb_regions(Subtype(self.trace()), a, b))
118+
Ok(self.fields.infcx.region_vars.glb_regions(Subtype(self.trace()), a, b))
115119
}
116120

117121
fn contraregions(&self, a: ty::Region, b: ty::Region)
@@ -128,33 +132,33 @@ impl<'f> Combine for Glb<'f> {
128132
// please see the large comment in `region_inference.rs`.
129133

130134
debug!("{}.fn_sigs({:?}, {:?})",
131-
self.tag(), a.repr(self.get_ref().infcx.tcx), b.repr(self.get_ref().infcx.tcx));
135+
self.tag(), a.repr(self.fields.infcx.tcx), b.repr(self.fields.infcx.tcx));
132136
let _indenter = indenter();
133137

134138
// Make a mark so we can examine "all bindings that were
135139
// created as part of this type comparison".
136-
let mark = self.get_ref().infcx.region_vars.mark();
140+
let mark = self.fields.infcx.region_vars.mark();
137141

138142
// Instantiate each bound region with a fresh region variable.
139143
let (a_with_fresh, a_map) =
140-
self.get_ref().infcx.replace_late_bound_regions_with_fresh_regions(
144+
self.fields.infcx.replace_late_bound_regions_with_fresh_regions(
141145
self.trace(), a);
142146
let a_vars = var_ids(self, &a_map);
143147
let (b_with_fresh, b_map) =
144-
self.get_ref().infcx.replace_late_bound_regions_with_fresh_regions(
148+
self.fields.infcx.replace_late_bound_regions_with_fresh_regions(
145149
self.trace(), b);
146150
let b_vars = var_ids(self, &b_map);
147151

148152
// Collect constraints.
149-
let sig0 = if_ok!(super_fn_sigs(self, &a_with_fresh, &b_with_fresh));
150-
debug!("sig0 = {}", sig0.repr(self.get_ref().infcx.tcx));
153+
let sig0 = try!(super_fn_sigs(self, &a_with_fresh, &b_with_fresh));
154+
debug!("sig0 = {}", sig0.repr(self.fields.infcx.tcx));
151155

152156
// Generalize the regions appearing in fn_ty0 if possible
153157
let new_vars =
154-
self.get_ref().infcx.region_vars.vars_created_since_mark(mark);
158+
self.fields.infcx.region_vars.vars_created_since_mark(mark);
155159
let sig1 =
156160
fold_regions_in_sig(
157-
self.get_ref().infcx.tcx,
161+
self.fields.infcx.tcx,
158162
&sig0,
159163
|r| {
160164
generalize_region(self,
@@ -166,7 +170,7 @@ impl<'f> Combine for Glb<'f> {
166170
b_vars.as_slice(),
167171
r)
168172
});
169-
debug!("sig1 = {}", sig1.repr(self.get_ref().infcx.tcx));
173+
debug!("sig1 = {}", sig1.repr(self.fields.infcx.tcx));
170174
return Ok(sig1);
171175

172176
fn generalize_region(this: &Glb,
@@ -182,7 +186,7 @@ impl<'f> Combine for Glb<'f> {
182186
return r0;
183187
}
184188

185-
let tainted = this.get_ref().infcx.region_vars.tainted(mark, r0);
189+
let tainted = this.fields.infcx.region_vars.tainted(mark, r0);
186190

187191
let mut a_r = None;
188192
let mut b_r = None;
@@ -249,14 +253,14 @@ impl<'f> Combine for Glb<'f> {
249253
return ty::ReLateBound(new_binder_id, *a_br);
250254
}
251255
}
252-
this.get_ref().infcx.tcx.sess.span_bug(
253-
this.get_ref().trace.origin.span(),
256+
this.fields.infcx.tcx.sess.span_bug(
257+
this.fields.trace.origin.span(),
254258
format!("could not find original bound region for {:?}",
255259
r).as_slice())
256260
}
257261

258262
fn fresh_bound_variable(this: &Glb, binder_id: NodeId) -> ty::Region {
259-
this.get_ref().infcx.region_vars.new_bound(binder_id)
263+
this.fields.infcx.region_vars.new_bound(binder_id)
260264
}
261265
}
262266
}

0 commit comments

Comments
 (0)