Check "x = 0".

This commit is contained in:
Nigel Tao committed 2017-05-12 15:18:38 +10:00
1 parent 1dd51ac30f
commit 188f0e9e3b
4 files changed
+18 -40

No files matched your search

+1
View File
@@ -355,6 +355,7 @@ func NewContinue() *Continue {
type TypeExpr Node
func (n *TypeExpr) Node() *Node { return (*Node)(n) }
func (n *TypeExpr) IsNumType() bool { return n.id0 == 0 && n.id1.IsNumType() }
func (n *TypeExpr) PackageOrDecorator() t.ID { return n.id0 }
func (n *TypeExpr) Name() t.ID { return n.id1 }
func (n *TypeExpr) ArrayLength() *Expr { return n.lhs.Expr() }
+2
View File
@@ -138,6 +138,8 @@ func (c *Checker) checkFuncBody(n *a.Node) error {
return nil
}
// TODO: use numTypeRanges.
var numTypeRanges = [256][2]*big.Int{
t.IDI8 >> t.KeyShift: {big.NewInt(-1 << 7), big.NewInt(1<<7 - 1)},
t.IDI16 >> t.KeyShift: {big.NewInt(-1 << 15), big.NewInt(1<<15 - 1)},
+1
View File
@@ -21,6 +21,7 @@ func TestCheck(t *testing.T) {
var y i32 = 2
var z u64[:123]
var a [4]u8
x = 0
y = x as i32
}
`
+14 -40
View File
@@ -67,11 +67,11 @@ func (c *typeChecker) checkStatement(n *a.Node) error {
lTyp := l.MType()
rTyp := r.MType()
// TODO, look at o.Operator().
if !lTyp.Eq(rTyp) {
return fmt.Errorf("check: cannot assign %q of type %q to %q of type %q",
r.String(c.idMap), rTyp.String(c.idMap), l.String(c.idMap), lTyp.String(c.idMap))
if (rTyp == DummyTypeIdealNumber && lTyp.IsNumType()) || lTyp.Eq(rTyp) {
return nil
}
return nil
return fmt.Errorf("check: cannot assign %q of type %q to %q of type %q",
r.String(c.idMap), rTyp.String(c.idMap), l.String(c.idMap), lTyp.String(c.idMap))
case a.KVar:
o := n.Var()
@@ -202,22 +202,25 @@ func (c *typeChecker) checkExprUnaryOp(n *a.Expr) error {
}
func (c *typeChecker) checkExprBinaryOp(n *a.Expr) error {
if err := c.checkExpr(n.LHS().Expr()); err != nil {
lhs := n.LHS().Expr()
if err := c.checkExpr(lhs); err != nil {
return err
}
if n.ID0() == t.IDXBinaryAs {
lhs := n.LHS().Expr()
rhs := n.RHS().TypeExpr()
if err := c.checkTypeExpr(rhs); err != nil {
return err
}
if err := c.typeConvertible(lhs, rhs); err != nil {
return err
lTyp := lhs.MType()
if (lTyp == DummyTypeIdealNumber || lTyp.IsNumType()) && rhs.IsNumType() {
n.SetMType(rhs)
return nil
}
n.SetMType(rhs)
return nil
return fmt.Errorf("check: cannot convert expression %q, of type %q, as type %q",
lhs.String(c.idMap), lTyp.String(c.idMap), rhs.String(c.idMap))
}
if err := c.checkExpr(n.RHS().Expr()); err != nil {
rhs := n.RHS().Expr()
if err := c.checkExpr(rhs); err != nil {
return err
}
// TODO: check lhs and rhs have compatible types, then call n.SetMType.
@@ -273,32 +276,3 @@ func (c *typeChecker) checkTypeExpr(n *a.TypeExpr) error {
}
return nil
}
func (c *typeChecker) typeConvertible(e *a.Expr, typ *a.TypeExpr) error {
eTyp := e.MType()
if eTyp == nil {
return fmt.Errorf("check: expression %q has no inferred type", e.String(c.idMap))
}
if typ.PackageOrDecorator() == 0 {
if name := typ.Name(); name.IsBuiltIn() && name.IsNumType() {
if eTyp == DummyTypeIdealNumber {
minMax := numTypeRanges[0xFF&(name>>t.KeyShift)]
if minMax[0] == nil {
return fmt.Errorf("check: unknown range for built-in numeric type %q", typ.String(c.idMap))
}
// TODO: update minMax for typ.Bounds().
//
// TODO: compare val to minMax. Or does that belong in a
// separate bounds checking phase instead of type checking?
return nil
} else {
// TODO: check that eTyp is a numeric type, not e.g. a struct.
return nil
}
}
}
return fmt.Errorf("check: cannot convert expression %q of type %q to type %q",
e.String(c.idMap), eTyp.String(c.idMap), typ.String(c.idMap))
}