cgen: fix using custom enum static from_string() (#19171)

This commit is contained in:
yuyi 2023-08-18 23:12:34 +08:00 committed by GitHub
parent 56644b2b52
commit f4eede8d23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -1720,7 +1720,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
g.write('panic_debug(${paline}, tos3("${pafile}"), tos3("${pamod}"), tos3("${pafn}"), ')
g.call_args(node)
g.write(')')
} else if node.name.ends_with('__static__from_string') {
} else if node.name.ends_with('__static__from_string') && !g.table.known_fn(node.name) {
if node.name !in g.str_fn_names {
g.gen_enum_static_from_string(node.name)
g.str_fn_names << node.name

View File

@ -0,0 +1,14 @@
enum Color {
red
green
blue
}
fn Color.from_string(x string) Color {
return Color.red
}
fn test_enum_custom_static_from_string() {
ret := Color.from_string('abc')
assert ret == Color.red
}