From 80477e3acfd13f169d1ed130190eb23c4b2b188f Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Fri, 28 Feb 2025 17:15:06 -0300 Subject: [PATCH] cgen: fix array fixed assignment for `@[keep_args_alive]` (partial fix for #23804) (#23805) --- vlib/v/gen/c/fn.v | 14 +++++++++++--- vlib/v/tests/c_function/code.c | 3 +++ vlib/v/tests/c_function/code_test.c.v | 10 ++++++++++ vlib/v/tests/c_function/v.mod | 7 +++++++ 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/c_function/code.c create mode 100644 vlib/v/tests/c_function/code_test.c.v create mode 100644 vlib/v/tests/c_function/v.mod diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 506576916..f7461b319 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -2579,9 +2579,17 @@ fn (mut g Gen) keep_alive_call_pregen(node ast.CallExpr) int { // save all arguments in temp vars (not only pointers) to make sure the // evaluation order is preserved expected_type := node.expected_arg_types[i] - typ := g.table.sym(expected_type).cname - g.write('${typ} __tmp_arg_${tmp_cnt_save + i} = ') - g.ref_or_deref_arg(arg, expected_type, node.language, false) + typ_sym := g.table.sym(expected_type) + typ := typ_sym.cname + if typ_sym.kind != .array_fixed { + g.write('${typ} __tmp_arg_${tmp_cnt_save + i} = ') + g.ref_or_deref_arg(arg, expected_type, node.language, false) + } else { + g.writeln('${typ} __tmp_arg_${tmp_cnt_save + i} = {0};') + g.write('memcpy(&__tmp_arg_${tmp_cnt_save + i}, ') + g.ref_or_deref_arg(arg, expected_type, node.language, false) + g.writeln(', sizeof(${typ}));') + } g.writeln(';') } g.empty_line = false diff --git a/vlib/v/tests/c_function/code.c b/vlib/v/tests/c_function/code.c new file mode 100644 index 000000000..72cad6343 --- /dev/null +++ b/vlib/v/tests/c_function/code.c @@ -0,0 +1,3 @@ +void foo(int arg[1]) { + +} \ No newline at end of file diff --git a/vlib/v/tests/c_function/code_test.c.v b/vlib/v/tests/c_function/code_test.c.v new file mode 100644 index 000000000..6aa1606a0 --- /dev/null +++ b/vlib/v/tests/c_function/code_test.c.v @@ -0,0 +1,10 @@ +module main + +#include "@VMODROOT/code.c" + +@[keep_args_alive] +fn C.foo(arg [1]int) + +fn test_main() { + C.foo([1]!) +} diff --git a/vlib/v/tests/c_function/v.mod b/vlib/v/tests/c_function/v.mod new file mode 100644 index 000000000..dc270208c --- /dev/null +++ b/vlib/v/tests/c_function/v.mod @@ -0,0 +1,7 @@ +Module { + name: 'c_function' + description: '' + version: '' + license: '' + dependencies: [] +} -- 2.39.5