aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/compiler
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2017-11-28 14:27:28 +0100
committerDmitry Vyukov <dvyukov@google.com>2017-11-28 14:55:21 +0100
commitac93d7e1d83ffac19983e9d754a47e7808f1230e (patch)
tree3fa594e98d47bd7e23d64594e196bc6c6a4bf408 /pkg/compiler
parentafba0b55e6d3692ac33c71553cb4cf61fc6a53e4 (diff)
pkg/compiler: add check that len does not refer to array with varlen elements
This [almost] always means a bug in descriptions. Fix all bugs identified by the check.
Diffstat (limited to 'pkg/compiler')
-rw-r--r--pkg/compiler/check.go15
-rw-r--r--pkg/compiler/testdata/errors2.txt2
2 files changed, 17 insertions, 0 deletions
diff --git a/pkg/compiler/check.go b/pkg/compiler/check.go
index 5fefeaf68..349dd9017 100644
--- a/pkg/compiler/check.go
+++ b/pkg/compiler/check.go
@@ -221,6 +221,21 @@ func (comp *compiler) checkLenTarget(t *ast.Type, name, target string, fields []
if fld.Type == t {
comp.error(t.Pos, "%v target %v refer to itself", t.Ident, target)
}
+ if t.Ident == "len" {
+ inner := fld.Type
+ desc, args, _ := comp.getArgsBase(inner, "", prog.DirIn, false)
+ for desc == typePtr {
+ if desc != typePtr {
+ break
+ }
+ inner = args[1]
+ desc, args, _ = comp.getArgsBase(inner, "", prog.DirIn, false)
+ }
+ if desc == typeArray && comp.isVarlen(args[0]) {
+ comp.error(t.Pos, "len target %v refer to an array with"+
+ " variable-size elements (do you mean bytesize?)", target)
+ }
+ }
return
}
for _, parent := range parents {
diff --git a/pkg/compiler/testdata/errors2.txt b/pkg/compiler/testdata/errors2.txt
index 0292aca5e..5b418ab55 100644
--- a/pkg/compiler/testdata/errors2.txt
+++ b/pkg/compiler/testdata/errors2.txt
@@ -52,6 +52,8 @@ foo$104(a len[parent]) ### len target parent does not exist
foo$105(a ptr[in, int32], b ptr[in, array[len[a, int32]]])
foo$106(a int32, b ptr[in, csum[a, inet, int32]])
foo$107(a int32, b ptr[in, csum[c, inet, int32]]) ### csum target c does not exist
+foo$108(a ptr[in, array[string]], b len[a]) ### len target a refer to an array with variable-size elements (do you mean bytesize?)
+foo$109(a ptr[in, array[string]], b ptr[in, len[a, int32]]) ### len target a refer to an array with variable-size elements (do you mean bytesize?)
s1 {
f1 len[s2, int32] ### len target s2 does not exist