Skip to content

Commit

Permalink
Consolidate "make sure types are the same" fns. Issue #2644.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkuper committed Jun 21, 2012
1 parent 393f739 commit e9d072e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 42 deletions.
30 changes: 13 additions & 17 deletions src/rustc/middle/typeck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
16 changes: 5 additions & 11 deletions src/rustc/middle/typeck/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1248,9 +1240,11 @@ fn check_expr_with_unifier(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); }
Expand Down Expand Up @@ -2305,7 +2299,7 @@ fn check_intrinsic_type(ccx: @crate_ctxt, it: @ast::native_item) {
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)]});
Expand Down
4 changes: 2 additions & 2 deletions src/rustc/middle/typeck/check/alt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/rustc/middle/typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
11 changes: 0 additions & 11 deletions src/rustc/middle/typeck/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ export resolve_deep;
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;
Expand Down Expand Up @@ -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> {
Expand Down

0 comments on commit e9d072e

Please sign in to comment.