Check "x = 1 + x".

This commit is contained in:
Nigel Tao committed 2017-05-13 10:03:30 +10:00
1 parent bd5f222fb6
commit a5a5509a44
2 files changed
+25 -8

No files matched your search

+1 -1
View File
@@ -54,7 +54,7 @@ func TestCheck(t *testing.T) {
var b bool var b bool
x = 0 x = 0
x = 1 + 2 x = 1 + x
y = -y y = -y
b = not true b = not true
+24 -7
View File
@@ -295,12 +295,25 @@ func (c *typeChecker) checkExprBinaryOp(n *a.Expr) error {
c.idMap.ByID(op.AmbiguousForm()), rhs.String(c.idMap), rTyp.String(c.idMap)) c.idMap.ByID(op.AmbiguousForm()), rhs.String(c.idMap), rTyp.String(c.idMap))
} }
} }
if op != t.IDXBinaryShiftL && op != t.IDXBinaryShiftR && !lTyp.Eq(rTyp) {
return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q, does not have matching types", switch op {
c.idMap.ByID(op.AmbiguousForm()), default:
lhs.String(c.idMap), rhs.String(c.idMap), if !lTyp.Eq(rTyp) && lTyp != TypeExprIdealNumber && rTyp != TypeExprIdealNumber {
lTyp.String(c.idMap), rTyp.String(c.idMap), return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q, does not have compatible types",
) c.idMap.ByID(op.AmbiguousForm()),
lhs.String(c.idMap), rhs.String(c.idMap),
lTyp.String(c.idMap), rTyp.String(c.idMap),
)
}
case t.IDXBinaryShiftL, t.IDXBinaryShiftR:
if (lTyp == TypeExprIdealNumber) && (rTyp != TypeExprIdealNumber) {
return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q; "+
"cannot shift an ideal number by a non-ideal number",
c.idMap.ByID(op.AmbiguousForm()),
lhs.String(c.idMap), rhs.String(c.idMap),
lTyp.String(c.idMap), rTyp.String(c.idMap),
)
}
} }
if l, r := lhs.ConstValue(), rhs.ConstValue(); l != nil && r != nil { if l, r := lhs.ConstValue(), rhs.ConstValue(); l != nil && r != nil {
@@ -308,11 +321,15 @@ func (c *typeChecker) checkExprBinaryOp(n *a.Expr) error {
return err return err
} }
} }
if comparisonOps[0xFF&op.Key()] { if comparisonOps[0xFF&op.Key()] {
n.SetMType(TypeExprBoolean) n.SetMType(TypeExprBoolean)
} else { } else if lTyp != TypeExprIdealNumber {
n.SetMType(lTyp) n.SetMType(lTyp)
} else {
n.SetMType(rTyp)
} }
return nil return nil
} }