aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/compiler
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2020-11-13 13:49:27 +0100
committerDmitry Vyukov <dvyukov@google.com>2020-11-13 14:27:16 +0100
commit857a060fe3a415c1909ad8a342d9ca21019ab2fc (patch)
treee3dbcf413b8fe04b79b7f1f36962ec2733d7f704 /pkg/compiler
parente32b6fa920e7300d16c190c1138dd042a9df7ea2 (diff)
pkg/compiler: check for flags with all equal values
There is no point in having flags when values are equal. This can only mean a typo or other bug. Check for such cases and fix 3 existing precedents.
Diffstat (limited to 'pkg/compiler')
-rw-r--r--pkg/compiler/check.go12
-rw-r--r--pkg/compiler/testdata/errors2.txt6
2 files changed, 17 insertions, 1 deletions
diff --git a/pkg/compiler/check.go b/pkg/compiler/check.go
index 468b6e35e..022581771 100644
--- a/pkg/compiler/check.go
+++ b/pkg/compiler/check.go
@@ -248,7 +248,7 @@ func (comp *compiler) checkTypes() {
func (comp *compiler) checkTypeValues() {
for _, decl := range comp.desc.Nodes {
- switch decl.(type) {
+ switch n := decl.(type) {
case *ast.Call, *ast.Struct, *ast.Resource, *ast.TypeDef:
comp.foreachType(decl, func(t *ast.Type, desc *typeDesc,
args []*ast.Type, base prog.IntTypeCommon) {
@@ -261,6 +261,16 @@ func (comp *compiler) checkTypeValues() {
}
}
})
+ case *ast.IntFlags:
+ allEqual := len(n.Values) >= 2
+ for _, val := range n.Values {
+ if val.Value != n.Values[0].Value {
+ allEqual = false
+ }
+ }
+ if allEqual {
+ comp.error(n.Pos, "all %v values are equal %v", n.Name.Name, n.Values[0].Value)
+ }
}
}
}
diff --git a/pkg/compiler/testdata/errors2.txt b/pkg/compiler/testdata/errors2.txt
index c862358cf..111db587f 100644
--- a/pkg/compiler/testdata/errors2.txt
+++ b/pkg/compiler/testdata/errors2.txt
@@ -370,6 +370,12 @@ foo$534(a ptr[in, flags[large_flags, int8]]) ### large_flags U16_MAX=0xffff does
large_flags = U8_MAX, U16_MAX
+equal_flags_0 = 0, 0, 0, 0 ### all equal_flags_0 values are equal 0
+equal_flags_1 = 42, 42, 42, 42 ### all equal_flags_1 values are equal 42
+non_equal_flags = 0, 0, 0, 1
+
+equal_flags_call(a flags[equal_flags_0], b flags[equal_flags_1], c flags[non_equal_flags])
+
type type500 proc[C1, 8, int8] ### values starting from 1 with step 8 overflow base type for 32 procs
type type501 int8 ### unused type type501
type type502[C] const[C, int8] ### unused type type502