ref: ae909b89c7b655a8bf943992c4fdd74707860140
parent: e6348f2c9c194b15960dab8695ddc1f250655ebe
author: Ori Bernstein <[email protected]>
date: Thu Sep 26 08:55:28 EDT 2013
Add proper type checks to arithmetic operators. We didn't respect traits. Fix.
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -981,8 +981,14 @@
case Osub: /* @a - @a -> @a */
case Omul: /* @a * @a -> @a */
case Odiv: /* @a / @a -> @a */
- case Omod: /* @a % @a -> @a */
case Oneg: /* -@a -> @a */
+ t = type(st, args[0]);
+ constrain(st, n, type(st, args[0]), cstrtab[Tcnum]);
+ for (i = 1; i < nargs; i++)
+ t = unify(st, n, t, type(st, args[i]));
+ settype(st, n, tf(st, t));
+ break;
+ case Omod: /* @a % @a -> @a */
case Obor: /* @a | @a -> @a */
case Oband: /* @a & @a -> @a */
case Obxor: /* @a ^ @a -> @a */
@@ -993,7 +999,6 @@
case Opredec: /* --@a -> @a */
case Opostinc: /* @a++ -> @a */
case Opostdec: /* @a-- -> @a */
- case Oasn: /* @a = @a -> @a */
case Oaddeq: /* @a += @a -> @a */
case Osubeq: /* @a -= @a -> @a */
case Omuleq: /* @a *= @a -> @a */
@@ -1004,6 +1009,14 @@
case Obxoreq: /* @a ^= @a -> @a */
case Obsleq: /* @a <<= @a -> @a */
case Obsreq: /* @a >>= @a -> @a */
+ t = type(st, args[0]);
+ constrain(st, n, type(st, args[0]), cstrtab[Tcnum]);
+ constrain(st, n, type(st, args[0]), cstrtab[Tcint]);
+ for (i = 1; i < nargs; i++)
+ t = unify(st, n, t, type(st, args[i]));
+ settype(st, n, tf(st, t));
+ break;
+ case Oasn: /* @a = @a -> @a */
t = type(st, args[0]);
for (i = 1; i < nargs; i++)
t = unify(st, n, t, type(st, args[i]));
--- /dev/null
+++ b/test/badop.myr
@@ -1,0 +1,6 @@
+use std
+
+const main = {
+ var x
+ x = "a" + "b"
+}
--- a/test/tests
+++ b/test/tests
@@ -103,3 +103,4 @@
F generic-in-const
F aritylow
F arityhigh
+F badop