From e9d072ee89bb0b41c84801670ad02201b054d16b Mon Sep 17 00:00:00 2001 From: Lindsey Kuper Date: Wed, 20 Jun 2012 18:51:08 -0700 Subject: [PATCH] Consolidate "make sure types are the same" fns. Issue #2644. --- src/rustc/middle/typeck.rs | 30 ++++++++++++---------------- src/rustc/middle/typeck/check.rs | 16 +++++---------- src/rustc/middle/typeck/check/alt.rs | 4 ++-- src/rustc/middle/typeck/collect.rs | 2 +- src/rustc/middle/typeck/infer.rs | 11 ---------- 5 files changed, 21 insertions(+), 42 deletions(-) diff --git a/src/rustc/middle/typeck.rs b/src/rustc/middle/typeck.rs index 3d2dca700c2e..3c7bddc9022c 100644 --- a/src/rustc/middle/typeck.rs +++ b/src/rustc/middle/typeck.rs @@ -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, 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 } } diff --git a/src/rustc/middle/typeck/check.rs b/src/rustc/middle/typeck/check.rs index de12fcb389ab..93fcfca31ea8 100644 --- a/src/rustc/middle/typeck/check.rs +++ b/src/rustc/middle/typeck/check.rs @@ -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_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); } @@ -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)]}); diff --git a/src/rustc/middle/typeck/check/alt.rs b/src/rustc/middle/typeck/check/alt.rs index bb9bc0a6653c..7a1eca4fc257 100644 --- a/src/rustc/middle/typeck/check/alt.rs +++ b/src/rustc/middle/typeck/check/alt.rs @@ -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) { diff --git a/src/rustc/middle/typeck/collect.rs b/src/rustc/middle/typeck/collect.rs index 490656eccbe5..249c0105beac 100644 --- a/src/rustc/middle/typeck/collect.rs +++ b/src/rustc/middle/typeck/collect.rs @@ -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; diff --git a/src/rustc/middle/typeck/infer.rs b/src/rustc/middle/typeck/infer.rs index b188f990a681..2a1f13604870 100644 --- a/src/rustc/middle/typeck/infer.rs +++ b/src/rustc/middle/typeck/infer.rs @@ -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; @@ -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 {