Consolidate "make sure types are the same" fns. Issue #2644.
This commit is contained in:
parent
393f739990
commit
e9d072ee89
@ -188,33 +188,29 @@ fn no_params(t: ty::t) -> ty::ty_param_bounds_and_ty {
|
||||
|
||||
fn require_same_types(
|
||||
tcx: ty::ctxt,
|
||||
maybe_infcx: option<infer::infer_ctxt>,
|
||||
span: span,
|
||||
t1: ty::t,
|
||||
t2: ty::t,
|
||||
msg: fn() -> str) -> bool {
|
||||
|
||||
alt infer::compare_tys(tcx, t1, t2) {
|
||||
result::ok(()) { true }
|
||||
result::err(terr) {
|
||||
tcx.sess.span_err(span, msg() + ": " +
|
||||
ty::type_err_to_str(tcx, terr));
|
||||
false
|
||||
let l_tcx, l_infcx;
|
||||
alt maybe_infcx {
|
||||
none {
|
||||
l_tcx = tcx;
|
||||
l_infcx = infer::new_infer_ctxt(tcx);
|
||||
}
|
||||
some(i) {
|
||||
l_tcx = i.tcx;
|
||||
l_infcx = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn require_same_types_in_infcx(
|
||||
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) {
|
||||
alt infer::mk_eqty(l_infcx, t1, t2) {
|
||||
result::ok(()) { true }
|
||||
result::err(terr) {
|
||||
infcx.tcx.sess.span_err(span, msg() + ": " +
|
||||
ty::type_err_to_str(infcx.tcx, terr));
|
||||
l_tcx.sess.span_err(span, msg() + ": " +
|
||||
ty::type_err_to_str(l_tcx, terr));
|
||||
false
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
fn check_lit(fcx: @fn_ctxt, lit: @ast::lit) -> ty::t {
|
||||
let tcx = fcx.ccx.tcx;
|
||||
@ -1248,9 +1240,11 @@ fn check_expr_fn(fcx: @fn_ctxt,
|
||||
};
|
||||
alt expr_opt {
|
||||
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,
|
||||
"ret; in function returning non-nil");
|
||||
"ret; in function returning non-nil"); }
|
||||
}
|
||||
}
|
||||
some(e) { check_expr_with(fcx, e, ret_ty); }
|
||||
@ -2305,7 +2299,7 @@ fn arg(m: ast::rmode, ty: ty::t) -> ty::arg {
|
||||
expected %u", i_n_tps, n_tps));
|
||||
} else {
|
||||
require_same_types(
|
||||
tcx, it.span, i_ty.ty, fty,
|
||||
tcx, none, it.span, i_ty.ty, fty,
|
||||
{|| #fmt["intrinsic has wrong type. \
|
||||
expected %s",
|
||||
ty_to_str(ccx.tcx, fty)]});
|
||||
|
@ -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));
|
||||
#debug["pat_range beginning type: %?", b_ty];
|
||||
#debug["pat_range ending type: %?", e_ty];
|
||||
if !require_same_types_in_infcx(
|
||||
fcx.infcx, pat.span, b_ty, e_ty,
|
||||
if !require_same_types(
|
||||
tcx, some(fcx.infcx), pat.span, b_ty, e_ty,
|
||||
{|| "mismatched types in range" }) {
|
||||
// no-op
|
||||
} else if !ty::type_is_numeric(b_ty) {
|
||||
|
@ -210,7 +210,7 @@ fn compare_impl_method(tcx: ty::ctxt, sp: span,
|
||||
ty::subst(tcx, substs, if_fty)
|
||||
};
|
||||
require_same_types(
|
||||
tcx, sp, impl_fty, if_fty,
|
||||
tcx, none, sp, impl_fty, if_fty,
|
||||
{|| "method `" + *if_m.ident + "` has an incompatible type"});
|
||||
ret;
|
||||
|
||||
|
@ -193,8 +193,6 @@ fn bar() {
|
||||
export resolve_deep_var;
|
||||
export 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 assignment;
|
||||
export root, to_str;
|
||||
@ -399,15 +397,6 @@ fn can_mk_assignty(cx: infer_ctxt, anmnt: assignment,
|
||||
} }.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
|
||||
fn resolve_shallow(cx: infer_ctxt, a: ty::t,
|
||||
force_vars: force_level) -> fres<ty::t> {
|
||||
|
Loading…
Reference in New Issue
Block a user