Skip to content

Commit 19900fd

Browse files
authored
cgen: fix codegen for assigning fixed array on defer var (fix #24300) (#24305)
1 parent 72f45e4 commit 19900fd

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

vlib/v/gen/c/assign.v

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,17 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
938938
|| gen_or {
939939
g.expr_with_opt_or_block(val, val_type, left, var_type, is_option_auto_heap)
940940
} else if val is ast.ArrayInit {
941-
g.array_init(val, c_name(ident.name))
941+
cvar_name := c_name(ident.name)
942+
if val.is_fixed && ident.name in g.defer_vars {
943+
g.go_before_last_stmt()
944+
g.empty_line = true
945+
g.write('memcpy(${cvar_name}, ')
946+
g.write('(${styp})')
947+
g.array_init(val, cvar_name)
948+
g.write(', sizeof(${styp}))')
949+
} else {
950+
g.array_init(val, cvar_name)
951+
}
942952
} else if val_type.has_flag(.shared_f) {
943953
g.expr_with_cast(val, val_type, var_type)
944954
} else if val in [ast.MatchExpr, ast.IfExpr]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn foo() {
2+
a := [u8(1), 2, 3]!
3+
defer {
4+
println('deffered println: ${a}')
5+
assert a == [u8(1), 2, 3]!
6+
}
7+
}
8+
9+
fn test_main() {
10+
foo()
11+
}

0 commit comments

Comments
 (0)