Consolidate "make sure types are the same" fns. Issue #2644.

This commit is contained in:
Lindsey Kuper
2012-06-20 18:51:08 -07:00
parent 393f739990
commit e9d072ee89
5 changed files with 21 additions and 42 deletions

View File

@@ -188,33 +188,29 @@ fn no_params(t: ty::t) -> ty::ty_param_bounds_and_ty {
fn require_same_types( fn require_same_types(
tcx: ty::ctxt, tcx: ty::ctxt,
maybe_infcx: option<infer::infer_ctxt>,
span: span, span: span,
t1: ty::t, t1: ty::t,
t2: ty::t, t2: ty::t,
msg: fn() -> str) -> bool { msg: fn() -> str) -> bool {
alt infer::compare_tys(tcx, t1, t2) { let l_tcx, l_infcx;
result::ok(()) { true } alt maybe_infcx {
result::err(terr) { none {
tcx.sess.span_err(span, msg() + ": " + l_tcx = tcx;
ty::type_err_to_str(tcx, terr)); l_infcx = infer::new_infer_ctxt(tcx);
false
} }
some(i) {
l_tcx = i.tcx;
l_infcx = i;
} }
} }
fn require_same_types_in_infcx( alt infer::mk_eqty(l_infcx, t1, t2) {
infcx: infer::infer_ctxt,
span: span,
t1: ty::t,
t2: ty::t,
msg: fn() -> str) -> bool {
alt infer::compare_tys_in_infcx(infcx, t1, t2) {
result::ok(()) { true } result::ok(()) { true }
result::err(terr) { result::err(terr) {
infcx.tcx.sess.span_err(span, msg() + ": " + l_tcx.sess.span_err(span, msg() + ": " +
ty::type_err_to_str(infcx.tcx, terr)); ty::type_err_to_str(l_tcx, terr));
false false
} }
} }

View File

@@ -606,14 +606,6 @@ fn do_autoderef(fcx: @fn_ctxt, sp: span, t: ty::t) -> ty::t {
}; };
} }
// Returns true if the two types unify and false if they don't.
fn are_compatible(fcx: @fn_ctxt, expected: ty::t, actual: ty::t) -> bool {
alt fcx.mk_eqty(expected, actual) {
result::ok(_) { ret true; }
result::err(_) { ret false; }
}
}
// AST fragment checking // AST fragment checking
fn check_lit(fcx: @fn_ctxt, lit: @ast::lit) -> ty::t { fn check_lit(fcx: @fn_ctxt, lit: @ast::lit) -> ty::t {
let tcx = fcx.ccx.tcx; let tcx = fcx.ccx.tcx;
@@ -1248,9 +1240,11 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
}; };
alt expr_opt { alt expr_opt {
none { none {
if !are_compatible(fcx, ret_ty, ty::mk_nil(tcx)) { alt fcx.mk_eqty(ret_ty, ty::mk_nil(tcx)) {
result::ok(_) { /* fall through */ }
result::err(_) {
tcx.sess.span_err(expr.span, tcx.sess.span_err(expr.span,
"ret; in function returning non-nil"); "ret; in function returning non-nil"); }
} }
} }
some(e) { check_expr_with(fcx, e, ret_ty); } some(e) { check_expr_with(fcx, e, ret_ty); }
@@ -2305,7 +2299,7 @@ fn check_intrinsic_type(ccx: @crate_ctxt, it: @ast::native_item) {
expected %u", i_n_tps, n_tps)); expected %u", i_n_tps, n_tps));
} else { } else {
require_same_types( require_same_types(
tcx, it.span, i_ty.ty, fty, tcx, none, it.span, i_ty.ty, fty,
{|| #fmt["intrinsic has wrong type. \ {|| #fmt["intrinsic has wrong type. \
expected %s", expected %s",
ty_to_str(ccx.tcx, fty)]}); ty_to_str(ccx.tcx, fty)]});

View File

@@ -141,8 +141,8 @@ fn check_pat(pcx: pat_ctxt, pat: @ast::pat, expected: ty::t) {
fcx.infcx.resolve_type_vars_if_possible(fcx.expr_ty(end)); fcx.infcx.resolve_type_vars_if_possible(fcx.expr_ty(end));
#debug["pat_range beginning type: %?", b_ty]; #debug["pat_range beginning type: %?", b_ty];
#debug["pat_range ending type: %?", e_ty]; #debug["pat_range ending type: %?", e_ty];
if !require_same_types_in_infcx( if !require_same_types(
fcx.infcx, pat.span, b_ty, e_ty, tcx, some(fcx.infcx), pat.span, b_ty, e_ty,
{|| "mismatched types in range" }) { {|| "mismatched types in range" }) {
// no-op // no-op
} else if !ty::type_is_numeric(b_ty) { } else if !ty::type_is_numeric(b_ty) {

View File

@@ -210,7 +210,7 @@ fn compare_impl_method(tcx: ty::ctxt, sp: span,
ty::subst(tcx, substs, if_fty) ty::subst(tcx, substs, if_fty)
}; };
require_same_types( require_same_types(
tcx, sp, impl_fty, if_fty, tcx, none, sp, impl_fty, if_fty,
{|| "method `" + *if_m.ident + "` has an incompatible type"}); {|| "method `" + *if_m.ident + "` has an incompatible type"});
ret; ret;

View File

@@ -193,8 +193,6 @@ export resolve_deep;
export resolve_deep_var; export resolve_deep_var;
export methods; // for infer_ctxt export methods; // for infer_ctxt
export unify_methods; // for infer_ctxt export unify_methods; // for infer_ctxt
export compare_tys;
export compare_tys_in_infcx;
export fixup_err, fixup_err_to_str; export fixup_err, fixup_err_to_str;
export assignment; export assignment;
export root, to_str; export root, to_str;
@@ -399,15 +397,6 @@ fn can_mk_assignty(cx: infer_ctxt, anmnt: assignment,
} }.to_ures() } }.to_ures()
} }
fn compare_tys(tcx: ty::ctxt, a: ty::t, b: ty::t) -> ures {
let infcx = new_infer_ctxt(tcx);
mk_eqty(infcx, a, b)
}
fn compare_tys_in_infcx(infcx: infer_ctxt, a: ty::t, b: ty::t) -> ures {
mk_eqty(infcx, a, b)
}
// See comment on the type `resolve_state` below // See comment on the type `resolve_state` below
fn resolve_shallow(cx: infer_ctxt, a: ty::t, fn resolve_shallow(cx: infer_ctxt, a: ty::t,
force_vars: force_level) -> fres<ty::t> { force_vars: force_level) -> fres<ty::t> {