From 03269f9854ad3e666b83e3683c2a15b1c059b8b0 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 4 Oct 2021 14:58:50 +0800 Subject: [PATCH] cgen: fix error of reference struct str() (#12061) --- vlib/v/gen/c/fn.v | 4 ++++ vlib/v/tests/str_reference_struct_test.v | 12 ++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 vlib/v/tests/str_reference_struct_test.v diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 2034d7734b..16fdd1f241 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -883,6 +883,10 @@ fn (mut g Gen) method_call(node ast.CallExpr) { // } if !node.receiver_type.is_ptr() && node.left_type.is_ptr() && node.name == 'str' { g.write('ptr_str(') + } else if node.receiver_type.is_ptr() && node.left_type.is_ptr() && node.name == 'str' + && !left_sym.has_method('str') { + g.gen_expr_to_string(node.left, node.left_type) + return } else { if left_sym.kind == .array { if array_depth >= 0 { diff --git a/vlib/v/tests/str_reference_struct_test.v b/vlib/v/tests/str_reference_struct_test.v new file mode 100644 index 0000000000..f60090c77a --- /dev/null +++ b/vlib/v/tests/str_reference_struct_test.v @@ -0,0 +1,12 @@ +struct Example { +} + +fn test_str_reference_struct() { + c1 := Example{} + println((&c1).str()) + assert (&c1).str() == '&Example{}' + + c2 := &Example{} + println(c2.str()) + assert c2.str() == '&Example{}' +}