1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix printing reference enum (#15606)

This commit is contained in:
yuyi 2022-08-30 19:24:48 +08:00 committed by GitHub
parent ba1045e5fd
commit 56135dbdbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 2 deletions

View File

@ -921,7 +921,7 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
funcprefix += 'isnil(it.${c_name(field.name)})'
funcprefix += ' ? _SLIT("nil") : '
// struct, floats and ints have a special case through the _str function
if sym.kind !in [.struct_, .alias] && !field.typ.is_int_valptr()
if sym.kind !in [.struct_, .alias, .enum_] && !field.typ.is_int_valptr()
&& !field.typ.is_float_valptr() {
funcprefix += '*'
}
@ -963,7 +963,7 @@ fn struct_auto_str_func(sym &ast.TypeSymbol, _field_type ast.Type, fn_name strin
sufix := if field_type.has_flag(.shared_f) { '->val' } else { '' }
deref, _ := deref_kind(expects_ptr, field_type.is_ptr(), field_type)
if sym.kind == .enum_ {
return '${fn_name}(${deref}it.${c_name(field_name)})', true
return '${fn_name}(${deref}(it.${c_name(field_name)}))', true
} else if should_use_indent_func(sym.kind) {
obj := '${deref}it.${c_name(field_name)}$sufix'
if has_custom_str {

View File

@ -86,6 +86,9 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
if expr !is ast.EnumVal {
str_fn_name := g.get_str_fn(typ)
g.write('${str_fn_name}(')
if typ.is_ptr() {
g.write('*')
}
g.enum_expr(expr)
g.write(')')
} else {

View File

@ -0,0 +1,4 @@
on
Foo{
i: &on
}

View File

@ -0,0 +1,23 @@
module main
enum State {
undef
off
on
}
struct Foo {
i &State
}
fn main() {
mut i := State.on
mut r := &i
println(r)
mut f := Foo{
i: &i
}
println(f)
}