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:
parent
ba1045e5fd
commit
56135dbdbc
|
@ -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 += 'isnil(it.${c_name(field.name)})'
|
||||||
funcprefix += ' ? _SLIT("nil") : '
|
funcprefix += ' ? _SLIT("nil") : '
|
||||||
// struct, floats and ints have a special case through the _str function
|
// 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() {
|
&& !field.typ.is_float_valptr() {
|
||||||
funcprefix += '*'
|
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 { '' }
|
sufix := if field_type.has_flag(.shared_f) { '->val' } else { '' }
|
||||||
deref, _ := deref_kind(expects_ptr, field_type.is_ptr(), field_type)
|
deref, _ := deref_kind(expects_ptr, field_type.is_ptr(), field_type)
|
||||||
if sym.kind == .enum_ {
|
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) {
|
} else if should_use_indent_func(sym.kind) {
|
||||||
obj := '${deref}it.${c_name(field_name)}$sufix'
|
obj := '${deref}it.${c_name(field_name)}$sufix'
|
||||||
if has_custom_str {
|
if has_custom_str {
|
||||||
|
|
|
@ -86,6 +86,9 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
|
||||||
if expr !is ast.EnumVal {
|
if expr !is ast.EnumVal {
|
||||||
str_fn_name := g.get_str_fn(typ)
|
str_fn_name := g.get_str_fn(typ)
|
||||||
g.write('${str_fn_name}(')
|
g.write('${str_fn_name}(')
|
||||||
|
if typ.is_ptr() {
|
||||||
|
g.write('*')
|
||||||
|
}
|
||||||
g.enum_expr(expr)
|
g.enum_expr(expr)
|
||||||
g.write(')')
|
g.write(')')
|
||||||
} else {
|
} else {
|
||||||
|
|
4
vlib/v/tests/inout/printing_reference_enum.out
Normal file
4
vlib/v/tests/inout/printing_reference_enum.out
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
on
|
||||||
|
Foo{
|
||||||
|
i: &on
|
||||||
|
}
|
23
vlib/v/tests/inout/printing_reference_enum.vv
Normal file
23
vlib/v/tests/inout/printing_reference_enum.vv
Normal 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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user