checker: prevent enum value duplication check bypass (#10287)

pull/10354/head
Lukas Neubert 2021-06-05 11:28:17 +02:00 committed by GitHub
parent e54af19b25
commit dc431a3c47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 2 deletions

View File

@ -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 {

View File

@ -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

View File

@ -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 | }

View File

@ -0,0 +1,5 @@
enum Color {
red // 0
green = -1
blue // -1 + 1 = 0
}