From 5464de406ca90be353b021f3571e8749f43a51b0 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 24 Jul 2022 15:15:22 +0800 Subject: [PATCH] cgen: fix custom str on struct with too many fields (#15195) --- vlib/v/gen/c/str.v | 2 +- ..._str_on_struct_with_too_many_fields_test.v | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/custom_str_on_struct_with_too_many_fields_test.v diff --git a/vlib/v/gen/c/str.v b/vlib/v/gen/c/str.v index a1a64e6d5f..2d5c8ad1f3 100644 --- a/vlib/v/gen/c/str.v +++ b/vlib/v/gen/c/str.v @@ -106,7 +106,7 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) { g.write('${str_fn_name}(') if str_method_expects_ptr && !is_ptr { g.write('&') - } else if (!str_method_expects_ptr && is_ptr && !is_shared) || is_var_mut { + } else if !str_method_expects_ptr && !is_shared && (is_ptr || is_var_mut) { g.write('*') } if expr is ast.ArrayInit { diff --git a/vlib/v/tests/custom_str_on_struct_with_too_many_fields_test.v b/vlib/v/tests/custom_str_on_struct_with_too_many_fields_test.v new file mode 100644 index 0000000000..5c4c985284 --- /dev/null +++ b/vlib/v/tests/custom_str_on_struct_with_too_many_fields_test.v @@ -0,0 +1,29 @@ +struct Abc { + a string + b string + c string + d string + // + e string + f string + g string + h string + // + x int // number of fields must be > 8 +} + +fn (a Abc) str() string { + return 'abc' +} + +fn (a Abc) some_method() string { + println(a) + return '$a' +} + +fn test_custom_str_on_struct_with_too_many_fields() { + abc := Abc{} + ret := abc.some_method() + println(ret) + assert ret == 'abc' +}