Skip to content

Commit 72056f3

Browse files
authored
cgen: fix cross assign of fixed array (#15587)
1 parent f23ebb6 commit 72056f3

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

vlib/v/gen/c/assign.v

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,30 @@ fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) {
658658
g.write(', ')
659659
g.expr(left.index)
660660
g.writeln(');')
661+
} else if sym.kind == .array_fixed {
662+
info := sym.info as ast.ArrayFixed
663+
elem_typ := g.table.sym(info.elem_type)
664+
if elem_typ.kind == .function {
665+
left_typ := node.left_types[i]
666+
left_sym := g.table.sym(left_typ)
667+
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_$left.pos.pos')
668+
g.write(' = *(voidptr*)')
669+
} else {
670+
styp := g.typ(info.elem_type)
671+
g.write('$styp _var_$left.pos.pos = ')
672+
}
673+
if left.left_type.is_ptr() {
674+
g.write('*')
675+
}
676+
needs_clone := info.elem_type == ast.string_type && g.is_autofree
677+
if needs_clone {
678+
g.write('/*1*/string_clone(')
679+
}
680+
g.expr(left)
681+
if needs_clone {
682+
g.write(')')
683+
}
684+
g.writeln(';')
661685
} else if sym.kind == .map {
662686
info := sym.info as ast.Map
663687
skeytyp := g.typ(info.key_type)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
fn test_cross_assign_fixed_array() {
2+
number := 5
3+
ans := fib(number)
4+
5+
println(ans)
6+
assert ans == 21
7+
}
8+
9+
fn fib(n int) u64 {
10+
if n <= 0 {
11+
panic('Bad number')
12+
}
13+
return match n {
14+
1 | 2 {
15+
1
16+
}
17+
else {
18+
mut pair := [1, 2]!
19+
for _ in 0 .. n {
20+
pair[0], pair[1] = pair[1], pair[0] + pair[1]
21+
}
22+
pair[1]
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)