From 147e6e669fe56b676d5dd87cb9b9d979ba591207 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 25 Apr 2022 10:57:35 +0300 Subject: [PATCH] cgen: fix pushing to an array of string pointers (fix #14156) --- vlib/v/gen/c/infix.v | 3 ++- vlib/v/tests/str_array_of_reference_test.v | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index d76eb3c56d..1a717e875a 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -708,7 +708,8 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) { g.write(', _MOV(($elem_type_str[]){ ') } // if g.autofree - needs_clone := array_info.elem_type.idx() == ast.string_type_idx && !g.is_builtin_mod + needs_clone := !g.is_builtin_mod && array_info.elem_type.idx() == ast.string_type_idx + && array_info.elem_type.nr_muls() == 0 if needs_clone { g.write('string_clone(') } diff --git a/vlib/v/tests/str_array_of_reference_test.v b/vlib/v/tests/str_array_of_reference_test.v index 9e82eedcac..982f542cf8 100644 --- a/vlib/v/tests/str_array_of_reference_test.v +++ b/vlib/v/tests/str_array_of_reference_test.v @@ -1,7 +1,18 @@ -fn test_str_array_of_reference() { +fn test_creating_an_array_of_string_reference() { names := ['John', 'Paul', 'George', 'Ringo'] a := unsafe { [&names[0], &names[1]] } println(a[0]) println(a) assert '$a' == "[&'John', &'Paul']" + assert typeof(a[0]).name == '&string' +} + +fn test_pushing_to_an_array_of_string_references() { + mut a := []&string{} + v1 := 'abc' + v2 := 'def' + a << &v1 + a << &v2 + assert *(a[0]) == 'abc' + assert *(a[1]) == 'def' }