Skip to content

Commit

Permalink
cgen: fix enum with comptime const value (fix #22386) (#22388)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Oct 2, 2024
1 parent cf2ccbc commit c01186c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -4406,6 +4406,8 @@ fn (mut g Gen) enum_decl(node ast.EnumDecl) {
const_def := g.global_const_defs[util.no_dots(field.expr.name)]
if const_def.def.starts_with('#define') {
g.enum_typedefs.write_string(const_def.def.all_after_last(' '))
} else if const_def.def.contains('const') {
g.enum_typedefs.write_string(const_def.def.all_after_last('=').all_before_last(';'))
} else {
g.enum_typedefs.write_string(expr_str)
}
Expand Down
10 changes: 10 additions & 0 deletions vlib/v/tests/enums/enum_with_comptime_const_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const enum_value = $if linux { 1 } $else { 2 }

pub enum Test {
a = enum_value
}

fn test_enum_with_comptime_const() {
println(Test.a)
assert true
}

0 comments on commit c01186c

Please sign in to comment.