Skip to content

Commit e9d072e

Browse files
committed
Consolidate "make sure types are the same" fns. Issue #2644.
1 parent 393f739 commit e9d072e

File tree

5 files changed

+21
-42
lines changed

5 files changed

+21
-42
lines changed

src/rustc/middle/typeck.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -188,33 +188,29 @@ fn no_params(t: ty::t) -> ty::ty_param_bounds_and_ty {
188188

189189
fn require_same_types(
190190
tcx: ty::ctxt,
191+
maybe_infcx: option<infer::infer_ctxt>,
191192
span: span,
192193
t1: ty::t,
193194
t2: ty::t,
194195
msg: fn() -> str) -> bool {
195196

196-
alt infer::compare_tys(tcx, t1, t2) {
197-
result::ok(()) { true }
198-
result::err(terr) {
199-
tcx.sess.span_err(span, msg() + ": " +
200-
ty::type_err_to_str(tcx, terr));
201-
false
197+
let l_tcx, l_infcx;
198+
alt maybe_infcx {
199+
none {
200+
l_tcx = tcx;
201+
l_infcx = infer::new_infer_ctxt(tcx);
202+
}
203+
some(i) {
204+
l_tcx = i.tcx;
205+
l_infcx = i;
202206
}
203207
}
204-
}
205-
206-
fn require_same_types_in_infcx(
207-
infcx: infer::infer_ctxt,
208-
span: span,
209-
t1: ty::t,
210-
t2: ty::t,
211-
msg: fn() -> str) -> bool {
212208

213-
alt infer::compare_tys_in_infcx(infcx, t1, t2) {
209+
alt infer::mk_eqty(l_infcx, t1, t2) {
214210
result::ok(()) { true }
215211
result::err(terr) {
216-
infcx.tcx.sess.span_err(span, msg() + ": " +
217-
ty::type_err_to_str(infcx.tcx, terr));
212+
l_tcx.sess.span_err(span, msg() + ": " +
213+
ty::type_err_to_str(l_tcx, terr));
218214
false
219215
}
220216
}

src/rustc/middle/typeck/check.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -606,14 +606,6 @@ fn do_autoderef(fcx: @fn_ctxt, sp: span, t: ty::t) -> ty::t {
606606
};
607607
}
608608

609-
// Returns true if the two types unify and false if they don't.
610-
fn are_compatible(fcx: @fn_ctxt, expected: ty::t, actual: ty::t) -> bool {
611-
alt fcx.mk_eqty(expected, actual) {
612-
result::ok(_) { ret true; }
613-
result::err(_) { ret false; }
614-
}
615-
}
616-
617609
// AST fragment checking
618610
fn check_lit(fcx: @fn_ctxt, lit: @ast::lit) -> ty::t {
619611
let tcx = fcx.ccx.tcx;
@@ -1248,9 +1240,11 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
12481240
};
12491241
alt expr_opt {
12501242
none {
1251-
if !are_compatible(fcx, ret_ty, ty::mk_nil(tcx)) {
1243+
alt fcx.mk_eqty(ret_ty, ty::mk_nil(tcx)) {
1244+
result::ok(_) { /* fall through */ }
1245+
result::err(_) {
12521246
tcx.sess.span_err(expr.span,
1253-
"ret; in function returning non-nil");
1247+
"ret; in function returning non-nil"); }
12541248
}
12551249
}
12561250
some(e) { check_expr_with(fcx, e, ret_ty); }
@@ -2305,7 +2299,7 @@ fn check_intrinsic_type(ccx: @crate_ctxt, it: @ast::native_item) {
23052299
expected %u", i_n_tps, n_tps));
23062300
} else {
23072301
require_same_types(
2308-
tcx, it.span, i_ty.ty, fty,
2302+
tcx, none, it.span, i_ty.ty, fty,
23092303
{|| #fmt["intrinsic has wrong type. \
23102304
expected %s",
23112305
ty_to_str(ccx.tcx, fty)]});

src/rustc/middle/typeck/check/alt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ fn check_pat(pcx: pat_ctxt, pat: @ast::pat, expected: ty::t) {
141141
fcx.infcx.resolve_type_vars_if_possible(fcx.expr_ty(end));
142142
#debug["pat_range beginning type: %?", b_ty];
143143
#debug["pat_range ending type: %?", e_ty];
144-
if !require_same_types_in_infcx(
145-
fcx.infcx, pat.span, b_ty, e_ty,
144+
if !require_same_types(
145+
tcx, some(fcx.infcx), pat.span, b_ty, e_ty,
146146
{|| "mismatched types in range" }) {
147147
// no-op
148148
} else if !ty::type_is_numeric(b_ty) {

src/rustc/middle/typeck/collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ fn compare_impl_method(tcx: ty::ctxt, sp: span,
210210
ty::subst(tcx, substs, if_fty)
211211
};
212212
require_same_types(
213-
tcx, sp, impl_fty, if_fty,
213+
tcx, none, sp, impl_fty, if_fty,
214214
{|| "method `" + *if_m.ident + "` has an incompatible type"});
215215
ret;
216216

src/rustc/middle/typeck/infer.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,6 @@ export resolve_deep;
193193
export resolve_deep_var;
194194
export methods; // for infer_ctxt
195195
export unify_methods; // for infer_ctxt
196-
export compare_tys;
197-
export compare_tys_in_infcx;
198196
export fixup_err, fixup_err_to_str;
199197
export assignment;
200198
export root, to_str;
@@ -399,15 +397,6 @@ fn can_mk_assignty(cx: infer_ctxt, anmnt: assignment,
399397
} }.to_ures()
400398
}
401399

402-
fn compare_tys(tcx: ty::ctxt, a: ty::t, b: ty::t) -> ures {
403-
let infcx = new_infer_ctxt(tcx);
404-
mk_eqty(infcx, a, b)
405-
}
406-
407-
fn compare_tys_in_infcx(infcx: infer_ctxt, a: ty::t, b: ty::t) -> ures {
408-
mk_eqty(infcx, a, b)
409-
}
410-
411400
// See comment on the type `resolve_state` below
412401
fn resolve_shallow(cx: infer_ctxt, a: ty::t,
413402
force_vars: force_level) -> fres<ty::t> {

0 commit comments

Comments
 (0)