Skip to content

Commit

Permalink
compiler: prohibit homonymous flags and consts
Browse files Browse the repository at this point in the history
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 <paul.chaignon@gmail.com>
  • Loading branch information
pchaigno authored and a-nogikh committed Nov 28, 2023
1 parent 450f17c commit 885f584
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
12 changes: 11 additions & 1 deletion pkg/compiler/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -34,6 +34,7 @@ func (comp *compiler) check() {
comp.checkConstructors()
comp.checkVarlens()
comp.checkDupConsts()
comp.checkConstsFlags(consts)
}

func (comp *compiler) checkComments() {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/compiler/testdata/errors2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 885f584

Please sign in to comment.