From c8e33ad219487c4b4fa5bb750c55f46989066d1d Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 6 Mar 2022 18:15:31 +0800 Subject: [PATCH] cgen: fix error for array of reference auto_str (#13665) --- vlib/v/gen/c/auto_str_methods.v | 6 +++++- vlib/v/tests/str_array_of_reference_test.v | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/str_array_of_reference_test.v diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index b13af61b35..b149d52b4a 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -610,7 +610,11 @@ fn (mut g Gen) gen_str_for_array(info ast.Array, styp string, str_fn_name string // Rune are managed at this level as strings g.auto_str_funcs.writeln('\t\tstring x = str_intp(2, _MOV((StrIntpData[]){{_SLIT("\`"), $c.si_s_code, {.d_s = ${elem_str_fn_name}(it) }}, {_SLIT("\`"), 0, {.d_c = 0 }}}));\n') } else if sym.kind == .string { - g.auto_str_funcs.writeln('\t\tstring x = str_intp(2, _MOV((StrIntpData[]){{_SLIT("\'"), $c.si_s_code, {.d_s = it }}, {_SLIT("\'"), 0, {.d_c = 0 }}}));\n') + if is_elem_ptr { + g.auto_str_funcs.writeln('\t\tstring x = str_intp(2, _MOV((StrIntpData[]){{_SLIT("&\'"), $c.si_s_code, {.d_s = *it }}, {_SLIT("\'"), 0, {.d_c = 0 }}}));\n') + } else { + g.auto_str_funcs.writeln('\t\tstring x = str_intp(2, _MOV((StrIntpData[]){{_SLIT("\'"), $c.si_s_code, {.d_s = it }}, {_SLIT("\'"), 0, {.d_c = 0 }}}));\n') + } } else { // There is a custom .str() method, so use it. // NB: we need to take account of whether the user has defined diff --git a/vlib/v/tests/str_array_of_reference_test.v b/vlib/v/tests/str_array_of_reference_test.v new file mode 100644 index 0000000000..9e82eedcac --- /dev/null +++ b/vlib/v/tests/str_array_of_reference_test.v @@ -0,0 +1,7 @@ +fn test_str_array_of_reference() { + names := ['John', 'Paul', 'George', 'Ringo'] + a := unsafe { [&names[0], &names[1]] } + println(a[0]) + println(a) + assert '$a' == "[&'John', &'Paul']" +}