aboutsummaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2017-09-04 19:53:02 +0200
committerDmitry Vyukov <dvyukov@google.com>2017-09-04 20:25:22 +0200
commitdbb49d02117ee51e0a77de6e935600eb005cb804 (patch)
treedc4db66dfdc72b4cb9d92a8cfdd878d7a0e8bbfb /pkg
parent622a1ffd7236529d9774498742810ce1102b009f (diff)
pkg/compiler: prohibit arrays of size 0
This is pointless and the only case that can yield 0 static type size.
Diffstat (limited to 'pkg')
-rw-r--r--pkg/compiler/testdata/errors.txt3
-rw-r--r--pkg/compiler/types.go6
2 files changed, 9 insertions, 0 deletions
diff --git a/pkg/compiler/testdata/errors.txt b/pkg/compiler/testdata/errors.txt
index c339dcb80..629c2c864 100644
--- a/pkg/compiler/testdata/errors.txt
+++ b/pkg/compiler/testdata/errors.txt
@@ -110,6 +110,9 @@ foo$45(a int32) len[b] ### len can't be syscall return
foo$46(a ptr[in, in]) ### unknown type in
foo$47(a int32:2) ### unexpected ':', only struct fields can be bitfields
foo$48(a ptr[in, int32:7]) ### unexpected ':', only struct fields can be bitfields
+foo$49(a ptr[in, array[int32, 0:1]])
+foo$50(a ptr[in, array[int32, 0]]) ### arrays of size 0 are not supported
+foo$51(a ptr[in, array[int32, 0:0]]) ### arrays of size 0 are not supported
opt { ### struct uses reserved name opt
f1 int32
diff --git a/pkg/compiler/types.go b/pkg/compiler/types.go
index 29ef4dbf5..9f8ec7754 100644
--- a/pkg/compiler/types.go
+++ b/pkg/compiler/types.go
@@ -97,6 +97,12 @@ var typeArray = &typeDesc{
CantBeOpt: true,
OptArgs: 1,
Args: []namedArg{{"type", typeArgType}, {"size", typeArgRange}},
+ Check: func(comp *compiler, t *ast.Type, args []*ast.Type, base sys.IntTypeCommon) {
+ if len(args) > 1 && args[1].Value == 0 && args[1].Value2 == 0 {
+ // This is the only case that can yield 0 static type size.
+ comp.error(args[1].Pos, "arrays of size 0 are not supported")
+ }
+ },
Gen: func(comp *compiler, t *ast.Type, args []*ast.Type, base sys.IntTypeCommon) sys.Type {
elemType := comp.genType(args[0], "", base.ArgDir, false)
kind, begin, end := sys.ArrayRandLen, uint64(0), uint64(0)