about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLindsey Kuper <lindsey@rockstargirl.org>2012-06-20 18:51:08 -0700
committerLindsey Kuper <lindsey@rockstargirl.org>2012-06-21 11:16:36 -0700
commite9d072ee89bb0b41c84801670ad02201b054d16b (patch)
treedf79bfef7af61997645d71a35af25ca483d14f7b
parent393f739990240914a6b147d6b642adc7ab9a939b (diff)
downloadrust-e9d072ee89bb0b41c84801670ad02201b054d16b.tar.gz
rust-e9d072ee89bb0b41c84801670ad02201b054d16b.zip
Consolidate "make sure types are the same" fns. Issue #2644.
-rw-r--r--src/rustc/middle/typeck.rs30
-rw-r--r--src/rustc/middle/typeck/check.rs16
-rw-r--r--src/rustc/middle/typeck/check/alt.rs4
-rw-r--r--src/rustc/middle/typeck/collect.rs2
-rw-r--r--src/rustc/middle/typeck/infer.rs11
5 files changed, 21 insertions, 42 deletions
diff --git a/src/rustc/middle/typeck.rs b/src/rustc/middle/typeck.rs
index 3d2dca700c2..3c7bddc9022 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<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
       }
     }
diff --git a/src/rustc/middle/typeck/check.rs b/src/rustc/middle/typeck/check.rs
index de12fcb389a..93fcfca31ea 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 bb9bc0a6653..7a1eca4fc25 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 490656eccbe..249c0105bea 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 b188f990a68..2a1f1360487 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<ty::t> {