From 56135dbdbcc07b7a0b8f4c2d2bbb685853a1a782 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 30 Aug 2022 19:24:48 +0800 Subject: [PATCH] cgen: fix printing reference enum (#15606) --- vlib/v/gen/c/auto_str_methods.v | 4 ++-- vlib/v/gen/c/str.v | 3 +++ .../v/tests/inout/printing_reference_enum.out | 4 ++++ vlib/v/tests/inout/printing_reference_enum.vv | 23 +++++++++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/inout/printing_reference_enum.out create mode 100644 vlib/v/tests/inout/printing_reference_enum.vv diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index 4cb058de6..574d8dbdc 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -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 { diff --git a/vlib/v/gen/c/str.v b/vlib/v/gen/c/str.v index 2d5c8ad1f..e301b2987 100644 --- a/vlib/v/gen/c/str.v +++ b/vlib/v/gen/c/str.v @@ -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 { diff --git a/vlib/v/tests/inout/printing_reference_enum.out b/vlib/v/tests/inout/printing_reference_enum.out new file mode 100644 index 000000000..830c40b06 --- /dev/null +++ b/vlib/v/tests/inout/printing_reference_enum.out @@ -0,0 +1,4 @@ +on +Foo{ + i: &on +} diff --git a/vlib/v/tests/inout/printing_reference_enum.vv b/vlib/v/tests/inout/printing_reference_enum.vv new file mode 100644 index 000000000..817a16e69 --- /dev/null +++ b/vlib/v/tests/inout/printing_reference_enum.vv @@ -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) +} -- 2.30.2