mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
autofree: fix cross var assigns with strings (#18147)
This commit is contained in:
parent
0be74aa613
commit
61a5fbea35
@ -838,6 +838,8 @@ fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) {
|
||||
if sym.kind == .array {
|
||||
info := sym.info as ast.Array
|
||||
elem_typ := g.table.sym(info.elem_type)
|
||||
needs_clone := info.elem_type == ast.string_type && g.is_autofree
|
||||
|
||||
if elem_typ.kind == .function {
|
||||
left_typ := node.left_types[i]
|
||||
left_sym := g.table.sym(left_typ)
|
||||
@ -845,21 +847,20 @@ fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) {
|
||||
g.write(' = *(voidptr*)array_get(')
|
||||
} else {
|
||||
styp := g.typ(info.elem_type)
|
||||
g.write('${styp} _var_${left.pos.pos} = *(${styp}*)array_get(')
|
||||
string_clone := if needs_clone { '/*1*/string_clone(' } else { '' }
|
||||
|
||||
g.write('${styp} _var_${left.pos.pos} = ${string_clone}*(${styp}*)array_get(')
|
||||
}
|
||||
|
||||
if left.left_type.is_ptr() {
|
||||
g.write('*')
|
||||
}
|
||||
needs_clone := info.elem_type == ast.string_type && g.is_autofree
|
||||
if needs_clone {
|
||||
g.write('/*1*/string_clone(')
|
||||
}
|
||||
g.expr(left.left)
|
||||
g.write(', ')
|
||||
g.expr(left.index)
|
||||
if needs_clone {
|
||||
g.write(')')
|
||||
}
|
||||
g.write(', ')
|
||||
g.expr(left.index)
|
||||
g.writeln(');')
|
||||
} else if sym.kind == .array_fixed {
|
||||
info := sym.info as ast.ArrayFixed
|
||||
@ -878,7 +879,7 @@ fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) {
|
||||
}
|
||||
needs_clone := info.elem_type == ast.string_type && g.is_autofree
|
||||
if needs_clone {
|
||||
g.write('/*1*/string_clone(')
|
||||
g.write('/*2*/string_clone(')
|
||||
}
|
||||
g.expr(left)
|
||||
if needs_clone {
|
||||
|
9
vlib/v/slow_tests/valgrind/cross_var_assign.v
Normal file
9
vlib/v/slow_tests/valgrind/cross_var_assign.v
Normal file
@ -0,0 +1,9 @@
|
||||
fn main() {
|
||||
mut a := []string{len: 2}
|
||||
a[0] = 'x'
|
||||
a[1] = 'y'
|
||||
|
||||
dump(a)
|
||||
a[0], a[1] = a[1], a[0]
|
||||
dump(a)
|
||||
}
|
Loading…
Reference in New Issue
Block a user