1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix cross assign of fixed array (#15587)

This commit is contained in:
yuyi 2022-08-29 13:50:19 +08:00 committed by GitHub
parent f23ebb6815
commit 72056f36d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 0 deletions

View File

@ -658,6 +658,30 @@ fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) {
g.write(', ')
g.expr(left.index)
g.writeln(');')
} else if sym.kind == .array_fixed {
info := sym.info as ast.ArrayFixed
elem_typ := g.table.sym(info.elem_type)
if elem_typ.kind == .function {
left_typ := node.left_types[i]
left_sym := g.table.sym(left_typ)
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_$left.pos.pos')
g.write(' = *(voidptr*)')
} else {
styp := g.typ(info.elem_type)
g.write('$styp _var_$left.pos.pos = ')
}
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)
if needs_clone {
g.write(')')
}
g.writeln(';')
} else if sym.kind == .map {
info := sym.info as ast.Map
skeytyp := g.typ(info.key_type)

View File

@ -0,0 +1,25 @@
fn test_cross_assign_fixed_array() {
number := 5
ans := fib(number)
println(ans)
assert ans == 21
}
fn fib(n int) u64 {
if n <= 0 {
panic('Bad number')
}
return match n {
1 | 2 {
1
}
else {
mut pair := [1, 2]!
for _ in 0 .. n {
pair[0], pair[1] = pair[1], pair[0] + pair[1]
}
pair[1]
}
}
}