Skip to content

Commit

Permalink
checker: check enum field value duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jul 24, 2024
1 parent 4c30d35 commit d07fc55
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,14 @@ fn (mut c Checker) enum_decl(mut node ast.EnumDecl) {
}
if field.expr.kind == .constant && field.expr.obj.typ.is_int() {
// accepts int constants as enum value
if mut field.expr.obj is ast.ConstField {
if mut field.expr.obj.expr is ast.IntegerLiteral {
c.check_enum_field_integer_literal(field.expr.obj.expr,
signed, node.is_multi_allowed, senum_type, field.expr.pos, mut
useen, enum_umin, enum_umax, mut iseen, enum_imin,
enum_imax)
}
}
continue
}
}
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/enum_field_value_duplicate_d.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vlib/v/checker/tests/enum_field_value_duplicate_d.vv:6:6: error: enum value `1` already exists
4 | a = one
5 | b
6 | c = one
| ~~~
7 | }
8 |
13 changes: 13 additions & 0 deletions vlib/v/checker/tests/enum_field_value_duplicate_d.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const one = 1

enum MyEnum {
a = one
b
c = one
}

fn main() {
$for val in MyEnum.values {
println('> name: ${val.name} | value: ${val.value}')
}
}

0 comments on commit d07fc55

Please sign in to comment.