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:
parent
f23ebb6815
commit
72056f36d8
@ -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)
|
||||
|
25
vlib/v/tests/cross_assign_fixed_array_test.v
Normal file
25
vlib/v/tests/cross_assign_fixed_array_test.v
Normal 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]
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user