Skip to content

Commit

Permalink
cgen: fix enum with const value
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jul 24, 2024
1 parent 4c30d35 commit 4f2d67b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
14 changes: 12 additions & 2 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,14 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) (str
}
b.writeln('\n// V includes:')
b.write_string(g.includes.str())
b.writeln('\n// V global/const #define ... :')
for var_name in g.sorted_global_const_names {
if var := g.global_const_defs[var_name] {
if var.def.starts_with('#define') {
b.writeln(var.def)
}
}
}
b.writeln('\n// Enum definitions:')
b.write_string(g.enum_typedefs.str())
b.writeln('\n// Thread definitions:')
Expand All @@ -579,10 +587,12 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) (str
b.write_string(g.json_forward_decls.str())
b.writeln('\n// V definitions:')
b.write_string(g.definitions.str())
b.writeln('\n// V global/const definitions:')
b.writeln('\n// V global/const non-precomputed definitions:')
for var_name in g.sorted_global_const_names {
if var := g.global_const_defs[var_name] {
b.writeln(var.def)
if !var.def.starts_with('#define') {
b.writeln(var.def)
}
}
}
interface_table := g.interface_table()
Expand Down
19 changes: 19 additions & 0 deletions vlib/v/tests/enum_with_const_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
enum Foo {
a = c
b = 1
c = 2
}

const c = 0

fn test_enum_with_const() {
mut foo := Foo.c
foo = .a
ret := match foo {
.a { 'a' }
.b { 'b' }
.c { 'c' }
}
println(ret)
assert ret == 'a'
}

0 comments on commit 4f2d67b

Please sign in to comment.