checker: prevent enum value duplication check bypass (#10287)
parent
e54af19b25
commit
dc431a3c47
|
@ -3208,6 +3208,8 @@ pub fn (mut c Checker) enum_decl(decl ast.EnumDecl) {
|
|||
last := seen[seen.len - 1]
|
||||
if last == checker.int_max {
|
||||
c.error('enum value overflows', field.pos)
|
||||
} else if !decl.is_multi_allowed && last + 1 in seen {
|
||||
c.error('enum value `${last + 1}` already exists', field.pos)
|
||||
}
|
||||
seen << last + 1
|
||||
} else {
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
vlib/v/checker/tests/enum_field_value_duplicate_err.vv:3:10: error: enum value `0` already exists
|
||||
vlib/v/checker/tests/enum_field_value_duplicate_a.vv:3:10: error: enum value `0` already exists
|
||||
1 | enum Color {
|
||||
2 | red
|
||||
3 | green = 0
|
||||
| ^
|
||||
4 | blue = 1
|
||||
5 | alpha = 1
|
||||
vlib/v/checker/tests/enum_field_value_duplicate_err.vv:5:10: error: enum value `1` already exists
|
||||
vlib/v/checker/tests/enum_field_value_duplicate_a.vv:5:10: error: enum value `1` already exists
|
||||
3 | green = 0
|
||||
4 | blue = 1
|
||||
5 | alpha = 1
|
|
@ -0,0 +1,6 @@
|
|||
vlib/v/checker/tests/enum_field_value_duplicate_b.vv:4:2: error: enum value `0` already exists
|
||||
2 | red // 0
|
||||
3 | green = -1
|
||||
4 | blue // -1 + 1 = 0
|
||||
| ~~~~
|
||||
5 | }
|
|
@ -0,0 +1,5 @@
|
|||
enum Color {
|
||||
red // 0
|
||||
green = -1
|
||||
blue // -1 + 1 = 0
|
||||
}
|
Loading…
Reference in New Issue