cgen: fix enum with const value (#21919)

This commit is contained in:
yuyi 2024-07-24 19:25:45 +08:00 committed by GitHub
parent 4c30d357fe
commit 38ea5dcc44
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 2 deletions

View File

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

View File

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