cgen: fix pushing to an array of string pointers (fix #14156)

Delyan Angelov 2022-04-25 10:57:35 +03:00 committed by Jef Roosens
parent 47e7582af8
commit 5b58f4efbf
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 14 additions and 2 deletions

View File

@ -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(')
}

View File

@ -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'
}