From 47d8ff94e2d2dddb535ff355b5833fedb9977478 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Fri, 25 Apr 2025 00:37:59 -0300 Subject: [PATCH] cgen: fix codegen for multi return with aliased fixed array (fix #24280) (#24295) --- vlib/v/gen/c/cgen.v | 3 +++ .../return_multi_aliased_fixed_array_test.v | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 vlib/v/tests/fns/return_multi_aliased_fixed_array_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 5e80f29cd..355c5c351 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5930,6 +5930,9 @@ fn (mut g Gen) return_stmt(node ast.Return) { line := g.go_before_last_stmt().trim_space() expr_styp := g.styp(node.types[i]) g.write('memcpy(&${tmpvar}.arg${arg_idx}, ') + if expr is ast.StructInit { + g.write('(${expr_styp})') + } g.expr(expr) g.writeln(', sizeof(${expr_styp}));') final_assignments += g.go_before_last_stmt() + '\t' diff --git a/vlib/v/tests/fns/return_multi_aliased_fixed_array_test.v b/vlib/v/tests/fns/return_multi_aliased_fixed_array_test.v new file mode 100644 index 000000000..8352bd673 --- /dev/null +++ b/vlib/v/tests/fns/return_multi_aliased_fixed_array_test.v @@ -0,0 +1,24 @@ +type Bytes32 = [32]u8 + +pub fn a(x1 Bytes32, y1 Bytes32, z1 Bytes32, x2 Bytes32, y2 Bytes32, z2 Bytes32) (Bytes32, Bytes32, Bytes32) { + return Bytes32{}, Bytes32{}, Bytes32{} +} + +pub fn b(x Bytes32, y Bytes32, z Bytes32) (Bytes32, Bytes32) { + return Bytes32{}, Bytes32{} +} + +fn test_main() { + x1 := Bytes32{} + y1 := Bytes32{} + z1 := Bytes32{} + x2 := Bytes32{} + y2 := Bytes32{} + z2 := Bytes32{} + + x3, y3, z3 := a(x1, y1, z1, x2, y2, z2) + + xx, yy := b(x3, y3, z3) + + assert xx == yy +} -- 2.39.5