From 09888fad7b7b54762131dd6c9ed04781273e50ef Mon Sep 17 00:00:00 2001 From: mewmew Date: Tue, 3 May 2016 23:29:06 +0200 Subject: [PATCH] sem: Check that array declarations have a size or an initializer. --- sem/sem_test.go | 7 ------- sem/typecheck/typecheck.go | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/sem/sem_test.go b/sem/sem_test.go index 2057b6a..a38c1c4 100644 --- a/sem/sem_test.go +++ b/sem/sem_test.go @@ -1,9 +1,7 @@ package sem_test import ( - "fmt" "io/ioutil" - "strings" "testing" "github.com/mewkiz/pkg/errutil" @@ -31,7 +29,6 @@ func TestCheckValid(t *testing.T) { errors.UseColor = false for _, g := range golden { - fmt.Println("path:", g.path) // TODO: Remove once the test cases mature. buf, err := ioutil.ReadFile(g.path) if err != nil { t.Errorf("%q: %v", g.path, err) @@ -364,7 +361,6 @@ void f(void, void) { errors.UseColor = false for _, g := range golden { - fmt.Println("path:", g.path) // TODO: Remove once the test cases mature. buf, err := ioutil.ReadFile(g.path) if err != nil { t.Errorf("%q: %v", g.path, err) @@ -397,9 +393,6 @@ void f(void, void) { } if got != g.want { t.Errorf("%q: error mismatch; expected `%v`, got `%v`", g.path, g.want, got) - } else if strings.Contains(g.path, "extra") { - // TODO: Remove once sem passes the extra tests. - fmt.Println("PASS:", g.path) } } } diff --git a/sem/typecheck/typecheck.go b/sem/typecheck/typecheck.go index ee56588..15775d2 100644 --- a/sem/typecheck/typecheck.go +++ b/sem/typecheck/typecheck.go @@ -36,6 +36,20 @@ func check(file *ast.File, exprTypes map[ast.Expr]types.Type) error { // check type-checks the given node. check := func(n ast.Node) error { switch n := n.(type) { + case *ast.BlockStmt: + // Verify that array declarations have an explicit size or an + // initializer. + for _, item := range n.Items { + switch item := item.(type) { + case *ast.VarDecl: + typ := item.Type() + if typ, ok := typ.(*types.Array); ok { + if typ.Len == 0 && item.Val == nil { + return errors.Newf(item.VarName.NamePos, "array size or initializer missing for %q", item.VarName) + } + } + } + } case *ast.VarDecl: typ := n.Type() // TODO: Evaluate if the type-checking of identifiers could be made