about summary refs log tree commit diff
path: root/src/comp
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2011-06-24 14:19:58 -0700
committerBrian Anderson <banderson@mozilla.com>2011-06-25 19:42:59 -0700
commitcfaa0f4b91b27abfb3b4fba4d0bb31a5e3da7c89 (patch)
treef975de28d5b1e8bb19c7ead52757a83ba5af1977 /src/comp
parent51d07830baddf3bbcee7352986050721abdca131 (diff)
Fail typechecking for bad binop/type combinations
Includes assignment operations. Add regression tests for lots of less useful,
less used or unexpected combinations, as well as a selection of compile-fail
tests. Closes #500 (again!)
Diffstat (limited to 'src/comp')
-rw-r--r--src/comp/middle/ty.rs91
-rw-r--r--src/comp/middle/typeck.rs39
2 files changed, 117 insertions, 13 deletions
diff --git a/src/comp/middle/ty.rs b/src/comp/middle/ty.rs
index e8b31aa3a63..69a59b42105 100644
--- a/src/comp/middle/ty.rs
+++ b/src/comp/middle/ty.rs
@@ -54,6 +54,7 @@ export get_element_type;
 export hash_ty;
 export idx_nil;
 export is_lval;
+export is_binopable;
 export item_table;
 export lookup_item_type;
 export method;
@@ -2792,6 +2793,96 @@ fn strip_boxes(&ctxt cx, &ty::t t) -> ty::t {
     }
     fail;
 }
+
+fn is_binopable(&ctxt cx, t ty, ast::binop op) -> bool {
+
+    const int tycat_other = 0;
+    const int tycat_bool = 1;
+    const int tycat_int = 2;
+    const int tycat_float = 3;
+    const int tycat_str = 4;
+    const int tycat_vec = 5;
+    const int tycat_struct = 6;
+
+    const int opcat_add = 0;
+    const int opcat_sub = 1;
+    const int opcat_mult = 2;
+    const int opcat_shift = 3;
+    const int opcat_rel = 4;
+    const int opcat_eq = 5;
+    const int opcat_bit = 6;
+    const int opcat_logic = 7;
+
+    fn opcat(ast::binop op) -> int {
+        alt (op) {
+            case (ast::add) { opcat_add }
+            case (ast::sub) { opcat_sub }
+            case (ast::mul) { opcat_mult }
+            case (ast::div) { opcat_mult }
+            case (ast::rem) { opcat_mult }
+            case (ast::and) { opcat_logic }
+            case (ast::or) { opcat_logic }
+            case (ast::bitxor) { opcat_bit }
+            case (ast::bitand) { opcat_bit }
+            case (ast::bitor) { opcat_bit }
+            case (ast::lsl) { opcat_shift }
+            case (ast::lsr) { opcat_shift }
+            case (ast::asr) { opcat_shift }
+            case (ast::eq) { opcat_eq }
+            case (ast::ne) { opcat_eq }
+            case (ast::lt) { opcat_rel }
+            case (ast::le) { opcat_rel }
+            case (ast::ge) { opcat_rel }
+            case (ast::gt) { opcat_rel }
+        }
+    }
+
+    fn tycat(&ctxt cx, t ty) -> int {
+        alt (struct(cx, strip_boxes(cx, ty))) {
+            case (ty_bool) { tycat_bool }
+            case (ty_int) { tycat_int }
+            case (ty_uint) { tycat_int }
+            case (ty_machine(ty_i8)) { tycat_int }
+            case (ty_machine(ty_i16)) { tycat_int }
+            case (ty_machine(ty_i32)) { tycat_int }
+            case (ty_machine(ty_i64)) { tycat_int }
+            case (ty_machine(ty_u8)) { tycat_int }
+            case (ty_machine(ty_u16)) { tycat_int }
+            case (ty_machine(ty_u32)) { tycat_int }
+            case (ty_machine(ty_u64)) { tycat_int }
+            case (ty_float) { tycat_float }
+            case (ty_machine(ty_f32)) { tycat_float }
+            case (ty_machine(ty_f64)) { tycat_float }
+            case (ty_char) { tycat_int }
+            case (ty_ptr(_)) { tycat_int }
+            case (ty_str) { tycat_str }
+            case (ty_istr) { tycat_str }
+            case (ty_vec(_)) { tycat_vec }
+            case (ty_ivec(_)) { tycat_vec }
+            case (ty_tup(_)) { tycat_struct }
+            case (ty_rec(_)) { tycat_struct }
+            case (ty_tag(_, _)) { tycat_struct }
+            case (_) { tycat_other }
+        }
+    }
+
+    const bool t = true;
+    const bool f = false;
+
+    /*.          add,     shift,   bit
+      .             sub,     rel,     logic
+      .                mult,    eq,         */
+    auto tbl = [[f, f, f, f, t, t, f, f], /*other*/
+                [f, f, f, f, t, t, t, t], /*bool*/
+                [t, t, t, t, t, t, t, f], /*int*/
+                [t, t, t, f, t, t, f, f], /*float*/
+                [t, f, f, f, t, t, f, f], /*str*/
+                [t, f, f, f, t, t, f, f], /*vec*/
+                [f, f, f, f, t, t, f, f]];/*struct*/
+
+    ret tbl.(tycat(cx, ty)).(opcat(op));
+}
+
 // Local Variables:
 // mode: rust
 // fill-column: 78;
diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs
index 4ad7400dc99..b851f5e9fb9 100644
--- a/src/comp/middle/typeck.rs
+++ b/src/comp/middle/typeck.rs
@@ -1476,6 +1476,19 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
         write::ty_only_fixup(fcx, id, if_t);
     }
 
+    // Checks the compatibility 
+    fn check_binop_type_compat(&@fn_ctxt fcx, common::span span,
+                               ty::t ty, ast::binop binop) {
+        auto resolved_t = resolve_type_vars_if_possible(fcx, ty);
+        if (!ty::is_binopable(fcx.ccx.tcx, resolved_t, binop)) {
+            auto binopstr = ast::binop_to_str(binop);
+            auto t_str = ty_to_str(fcx.ccx.tcx, resolved_t);
+            auto errmsg = "binary operation " + binopstr
+                + " cannot be applied to type `" + t_str + "`";
+            fcx.ccx.tcx.sess.span_fatal(span, errmsg);
+        }
+    }
+
     auto id = expr.id;
     alt (expr.node) {
         case (ast::expr_lit(?lit)) {
@@ -1490,19 +1503,17 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
             auto rhs_t = expr_ty(fcx.ccx.tcx, rhs);
 
             demand::autoderef(fcx, rhs.span, lhs_t, rhs_t, AUTODEREF_OK);
-
-            // FIXME: Binops have a bit more subtlety than this.
-
-            auto t = strip_boxes(fcx, expr.span, lhs_t);
-            alt (binop) {
-                case (ast::eq) { t = ty::mk_bool(fcx.ccx.tcx); }
-                case (ast::lt) { t = ty::mk_bool(fcx.ccx.tcx); }
-                case (ast::le) { t = ty::mk_bool(fcx.ccx.tcx); }
-                case (ast::ne) { t = ty::mk_bool(fcx.ccx.tcx); }
-                case (ast::ge) { t = ty::mk_bool(fcx.ccx.tcx); }
-                case (ast::gt) { t = ty::mk_bool(fcx.ccx.tcx); }
-                case (_) {/* fall through */ }
-            }
+            check_binop_type_compat(fcx, expr.span, lhs_t, binop);
+
+            auto t = alt (binop) {
+                case (ast::eq) { ty::mk_bool(fcx.ccx.tcx) }
+                case (ast::lt) { ty::mk_bool(fcx.ccx.tcx) }
+                case (ast::le) { ty::mk_bool(fcx.ccx.tcx) }
+                case (ast::ne) { ty::mk_bool(fcx.ccx.tcx) }
+                case (ast::ge) { ty::mk_bool(fcx.ccx.tcx) }
+                case (ast::gt) { ty::mk_bool(fcx.ccx.tcx) }
+                case (_) { strip_boxes(fcx, expr.span, lhs_t) }
+            };
             write::ty_only_fixup(fcx, id, t);
         }
         case (ast::expr_unary(?unop, ?oper)) {
@@ -1646,6 +1657,8 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
         case (ast::expr_assign_op(?op, ?lhs, ?rhs)) {
             require_impure(fcx.ccx.tcx.sess, fcx.purity, expr.span);
             check_assignment(fcx, expr.span, lhs, rhs, id);
+            check_binop_type_compat(fcx, expr.span,
+                                    expr_ty(fcx.ccx.tcx, lhs), op);
         }
         case (ast::expr_send(?lhs, ?rhs)) {
             require_impure(fcx.ccx.tcx.sess, fcx.purity, expr.span);