Skip to content

Commit

Permalink
sem: Check that array declarations have a size or an initializer.
Browse files Browse the repository at this point in the history
  • Loading branch information
mewmew committed May 3, 2016
1 parent 3e1e419 commit 09888fa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
7 changes: 0 additions & 7 deletions sem/sem_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package sem_test

import (
"fmt"
"io/ioutil"
"strings"
"testing"

"github.com/mewkiz/pkg/errutil"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions sem/typecheck/typecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 09888fa

Please sign in to comment.