From 87dcd9ed55f40cd8d0b0b361a7aed824cc744fc4 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 13 May 2017 10:23:21 +1000 Subject: [PATCH] Check the for-loop condition has boolean type. --- lang/check/check_test.go | 3 +++ lang/check/type.go | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lang/check/check_test.go b/lang/check/check_test.go index cb9abaca..07ef46c8 100644 --- a/lang/check/check_test.go +++ b/lang/check/check_test.go @@ -59,6 +59,9 @@ func TestCheck(t *testing.T) { b = not true y = x as i32 + + for x > 0 { + } } `) + "\n" diff --git a/lang/check/type.go b/lang/check/type.go index add50d0e..a162731d 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -89,7 +89,10 @@ func (c *typeChecker) checkStatement(n *a.Node) error { if err := c.checkExpr(cond); err != nil { return err } - // TODO: check cond has type bool. + if !cond.MType().Eq(TypeExprBoolean) { + return fmt.Errorf("check: for-loop condition %q, of type %q, does not have a boolean type", + cond.String(c.idMap), cond.MType().String(c.idMap)) + } } for _, m := range o.Body() { if err := c.checkStatement(m); err != nil {