From 05d482bcef6ee33e7adc56c5bdc1c88731c73328 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Fri, 31 Oct 2025 05:09:57 -0300 Subject: [PATCH] cgen: fix multi return of a fixed array (fix #25626) (#25628) --- vlib/v/gen/c/cgen.v | 16 +++++++++++++--- vlib/v/tests/fns/return_multi_fixed_arr_test.v | 11 +++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/fns/return_multi_fixed_arr_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 9b0b7e2ba..7b15104cc 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -6263,7 +6263,17 @@ fn (mut g Gen) return_stmt(node ast.Return) { if expr !is ast.ArrayInit && g.table.final_sym(node.types[i]).kind == .array_fixed { line := g.go_before_last_stmt().trim_space() expr_styp := g.styp(node.types[i]) - g.write('memcpy(&${tmpvar}.arg${arg_idx}, ') + g.write('memcpy(&') + if fn_return_is_result || fn_return_is_option { + g.write('((${styp}*)') + } + g.write('${tmpvar}') + if fn_return_is_result || fn_return_is_option { + g.write('.data)->') + } else { + g.write('.') + } + g.write('arg${arg_idx}, ') if expr is ast.StructInit { g.write('(${expr_styp})') } @@ -6292,11 +6302,9 @@ fn (mut g Gen) return_stmt(node ast.Return) { if fn_return_is_option { g.writeln(' }, (${option_name}*)(&${tmpvar}), sizeof(${styp}));') g.write_defer_stmts_when_needed() - g.write('return ${tmpvar}') } else if fn_return_is_result { g.writeln(' }, (${result_name}*)(&${tmpvar}), sizeof(${styp}));') g.write_defer_stmts_when_needed() - g.write('return ${tmpvar}') } // Make sure to add our unpacks if multi_unpack != '' { @@ -6313,6 +6321,8 @@ fn (mut g Gen) return_stmt(node ast.Return) { g.write_defer_stmts_when_needed() g.writeln('return ${tmpvar};') has_semicolon = true + } else if fn_return_is_option || fn_return_is_result { + g.write('return ${tmpvar}') } } else if exprs_len >= 1 { if node.types.len == 0 { diff --git a/vlib/v/tests/fns/return_multi_fixed_arr_test.v b/vlib/v/tests/fns/return_multi_fixed_arr_test.v new file mode 100644 index 000000000..31cb6e8a4 --- /dev/null +++ b/vlib/v/tests/fns/return_multi_fixed_arr_test.v @@ -0,0 +1,11 @@ +struct Abc {} + +fn bundle(x []u8) !([12]u8, Abc) { + x12 := [12]u8{} + return x12, Abc{} +} + +fn test_main() { + a, _ := bundle([])! + assert a.len == 12 +} -- 2.39.5