From 885f5843663a1f3f2f2f059978d51725f8e803ef Mon Sep 17 00:00:00 2001 From: Paul Chaignon Date: Mon, 13 Nov 2023 12:09:36 +0100 Subject: [PATCH] compiler: prohibit homonymous flags and consts Since both flags and consts can be used as type-options for integers, we want to avoid ambiguity by preventing a flag and a const from having the same name. Signed-off-by: Paul Chaignon --- pkg/compiler/check.go | 12 +++++++++++- pkg/compiler/compiler.go | 2 +- pkg/compiler/testdata/errors2.txt | 3 +++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pkg/compiler/check.go b/pkg/compiler/check.go index 2369daae559f..645a73220331 100644 --- a/pkg/compiler/check.go +++ b/pkg/compiler/check.go @@ -25,7 +25,7 @@ func (comp *compiler) typecheck() { comp.checkTypes() } -func (comp *compiler) check() { +func (comp *compiler) check(consts map[string]uint64) { comp.checkTypeValues() comp.checkAttributeValues() comp.checkUnused() @@ -34,6 +34,7 @@ func (comp *compiler) check() { comp.checkConstructors() comp.checkVarlens() comp.checkDupConsts() + comp.checkConstsFlags(consts) } func (comp *compiler) checkComments() { @@ -660,6 +661,15 @@ func (comp *compiler) checkUnused() { } } +func (comp *compiler) checkConstsFlags(consts map[string]uint64) { + for name := range consts { + if flags, isFlag := comp.intFlags[name]; isFlag { + pos, _, _ := flags.Info() + comp.error(pos, "const %v is already a flag", name) + } + } +} + type structDir struct { Struct string Dir prog.Dir diff --git a/pkg/compiler/compiler.go b/pkg/compiler/compiler.go index 15502e70bfa1..c8eb03d74975 100644 --- a/pkg/compiler/compiler.go +++ b/pkg/compiler/compiler.go @@ -93,7 +93,7 @@ func Compile(desc *ast.Description, consts map[string]uint64, target *targets.Ta comp.assignSyscallNumbers(consts) } comp.patchConsts(consts) - comp.check() + comp.check(consts) if comp.errors != 0 { return nil } diff --git a/pkg/compiler/testdata/errors2.txt b/pkg/compiler/testdata/errors2.txt index e583094fad85..1120f55ade59 100644 --- a/pkg/compiler/testdata/errors2.txt +++ b/pkg/compiler/testdata/errors2.txt @@ -383,6 +383,9 @@ type type500 proc[C1, 8, int8] ### values starting from 1 with step 8 overflow b type type501 int8 ### unused type type501 type type502[C] const[C, int8] ### unused type type502 +C2 = 0, 1, 2 ### const C2 is already a flag +use_flags(a flags[C2]) + s405 { f1 int16:8[-256:0] ### int range [18446744073709551360:0] is too large for base type of size 8 f2 int16:8[0:256] ### int range [0:256] is too large for base type of size 8