Skip to content

Commit 147e6e6

Browse files
committed
cgen: fix pushing to an array of string pointers (fix #14156)
1 parent 922cee9 commit 147e6e6

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

vlib/v/gen/c/infix.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,8 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) {
708708
g.write(', _MOV(($elem_type_str[]){ ')
709709
}
710710
// if g.autofree
711-
needs_clone := array_info.elem_type.idx() == ast.string_type_idx && !g.is_builtin_mod
711+
needs_clone := !g.is_builtin_mod && array_info.elem_type.idx() == ast.string_type_idx
712+
&& array_info.elem_type.nr_muls() == 0
712713
if needs_clone {
713714
g.write('string_clone(')
714715
}
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
fn test_str_array_of_reference() {
1+
fn test_creating_an_array_of_string_reference() {
22
names := ['John', 'Paul', 'George', 'Ringo']
33
a := unsafe { [&names[0], &names[1]] }
44
println(a[0])
55
println(a)
66
assert '$a' == "[&'John', &'Paul']"
7+
assert typeof(a[0]).name == '&string'
8+
}
9+
10+
fn test_pushing_to_an_array_of_string_references() {
11+
mut a := []&string{}
12+
v1 := 'abc'
13+
v2 := 'def'
14+
a << &v1
15+
a << &v2
16+
assert *(a[0]) == 'abc'
17+
assert *(a[1]) == 'def'
718
}

0 commit comments

Comments
 (0)