cgen: fix autofree error of array init with string variable (fix #10427) (#11041)

pull/11046/head
yuyi 2021-08-04 08:39:20 +08:00 committed by GitHub
parent 815c4b7420
commit 2eb11110d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -110,7 +110,14 @@ fn (mut g Gen) array_init(node ast.ArrayInit) {
g.write('\t\t')
}
for i, expr in node.exprs {
g.expr_with_cast(expr, node.expr_types[i], node.elem_type)
if node.expr_types[i] == ast.string_type && expr !is ast.StringLiteral
&& expr !is ast.StringInterLiteral {
g.write('string_clone(')
g.expr(expr)
g.write(')')
} else {
g.expr_with_cast(expr, node.expr_types[i], node.elem_type)
}
if i != len - 1 {
if i > 0 && i & 7 == 0 { // i > 0 && i % 8 == 0
g.writeln(',')

View File

@ -0,0 +1,11 @@
module main
fn main() {
a := 'Hello, '
b := 'World!'
c := a + b
d := [a, b, c]
println(d.len)
}