From 4f2d67bf03d0b83ab52a35d1cc8810f4e31be855 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 24 Jul 2024 13:50:03 +0800 Subject: [PATCH] cgen: fix enum with const value --- vlib/v/gen/c/cgen.v | 14 ++++++++++++-- vlib/v/tests/enum_with_const_test.v | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/enum_with_const_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index a45e239b7883c4..6f0aef9b357587 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -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:') @@ -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() diff --git a/vlib/v/tests/enum_with_const_test.v b/vlib/v/tests/enum_with_const_test.v new file mode 100644 index 00000000000000..6540fd1637a35e --- /dev/null +++ b/vlib/v/tests/enum_with_const_test.v @@ -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' +}