Skip to content

Commit 05d482b

Browse files
authored
cgen: fix multi return of a fixed array (fix #25626) (#25628)
1 parent 180253f commit 05d482b

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

vlib/v/gen/c/cgen.v

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6263,7 +6263,17 @@ fn (mut g Gen) return_stmt(node ast.Return) {
62636263
if expr !is ast.ArrayInit && g.table.final_sym(node.types[i]).kind == .array_fixed {
62646264
line := g.go_before_last_stmt().trim_space()
62656265
expr_styp := g.styp(node.types[i])
6266-
g.write('memcpy(&${tmpvar}.arg${arg_idx}, ')
6266+
g.write('memcpy(&')
6267+
if fn_return_is_result || fn_return_is_option {
6268+
g.write('((${styp}*)')
6269+
}
6270+
g.write('${tmpvar}')
6271+
if fn_return_is_result || fn_return_is_option {
6272+
g.write('.data)->')
6273+
} else {
6274+
g.write('.')
6275+
}
6276+
g.write('arg${arg_idx}, ')
62676277
if expr is ast.StructInit {
62686278
g.write('(${expr_styp})')
62696279
}
@@ -6292,11 +6302,9 @@ fn (mut g Gen) return_stmt(node ast.Return) {
62926302
if fn_return_is_option {
62936303
g.writeln(' }, (${option_name}*)(&${tmpvar}), sizeof(${styp}));')
62946304
g.write_defer_stmts_when_needed()
6295-
g.write('return ${tmpvar}')
62966305
} else if fn_return_is_result {
62976306
g.writeln(' }, (${result_name}*)(&${tmpvar}), sizeof(${styp}));')
62986307
g.write_defer_stmts_when_needed()
6299-
g.write('return ${tmpvar}')
63006308
}
63016309
// Make sure to add our unpacks
63026310
if multi_unpack != '' {
@@ -6313,6 +6321,8 @@ fn (mut g Gen) return_stmt(node ast.Return) {
63136321
g.write_defer_stmts_when_needed()
63146322
g.writeln('return ${tmpvar};')
63156323
has_semicolon = true
6324+
} else if fn_return_is_option || fn_return_is_result {
6325+
g.write('return ${tmpvar}')
63166326
}
63176327
} else if exprs_len >= 1 {
63186328
if node.types.len == 0 {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct Abc {}
2+
3+
fn bundle(x []u8) !([12]u8, Abc) {
4+
x12 := [12]u8{}
5+
return x12, Abc{}
6+
}
7+
8+
fn test_main() {
9+
a, _ := bundle([])!
10+
assert a.len == 12
11+
}

0 commit comments

Comments
 (0)