From f6a5c43da495c05d008a6ab4b66b53639f7f087c Mon Sep 17 00:00:00 2001 From: yuyi98 Date: Sat, 9 Apr 2022 09:42:49 +0800 Subject: [PATCH] cgen: fix error for fn with fixed array argument --- vlib/v/gen/c/assign.v | 7 ++++--- vlib/v/tests/fn_with_fixed_array_args_test.v | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/fn_with_fixed_array_args_test.v diff --git a/vlib/v/gen/c/assign.v b/vlib/v/gen/c/assign.v index ff7825263e..a167ffbbc1 100644 --- a/vlib/v/gen/c/assign.v +++ b/vlib/v/gen/c/assign.v @@ -238,11 +238,12 @@ fn (mut g Gen) gen_assign_stmt(node_ ast.AssignStmt) { } else { g.out.go_back_to(pos) is_var_mut := !is_decl && left.is_auto_deref_var() - addr := if is_var_mut { '' } else { '&' } + addr_left := if is_var_mut { '' } else { '&' } g.writeln('') - g.write('memcpy($addr') + g.write('memcpy($addr_left') g.expr(left) - g.writeln(', &$v_var, sizeof($arr_typ));') + addr_val := if is_fixed_array_var { '' } else { '&' } + g.writeln(', $addr_val$v_var, sizeof($arr_typ));') } g.is_assign_lhs = false } else { diff --git a/vlib/v/tests/fn_with_fixed_array_args_test.v b/vlib/v/tests/fn_with_fixed_array_args_test.v new file mode 100644 index 0000000000..7cf91daeb7 --- /dev/null +++ b/vlib/v/tests/fn_with_fixed_array_args_test.v @@ -0,0 +1,20 @@ +struct Test { +mut: + value [4]int +} + +fn (mut t Test) set(new_value [4]int) { + t.value = new_value +} + +fn test_fn_with_fixed_array_argument() { + mut t := Test{} + + println(t) + assert '$t.value' == '[0, 0, 0, 0]' + + t.set([1, 2, 3, 4]!) + + println(t) + assert '$t.value' == '[1, 2, 3, 4]' +}