From 704e3c6e7275336b5b4f8da2743871f1ee7d5b25 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 9 Apr 2022 20:57:27 +0800 Subject: [PATCH] cgen: fix error for fn with fixed array argument (fix #13976) (#13982) --- vlib/v/gen/c/assign.v | 7 ++-- vlib/v/gen/c/infix_expr.v | 3 +- vlib/v/tests/fn_with_fixed_array_args_test.v | 44 ++++++++++++++++++++ 3 files changed, 50 insertions(+), 4 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/gen/c/infix_expr.v b/vlib/v/gen/c/infix_expr.v index 04140453e2..b7e4b76a2a 100644 --- a/vlib/v/gen/c/infix_expr.v +++ b/vlib/v/gen/c/infix_expr.v @@ -626,7 +626,8 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) { if elem_sym.kind == .function { g.write(', _MOV((voidptr[]){ ') } else if elem_is_array_var { - g.write(', &') + addr := if elem_sym.kind == .array_fixed { '' } else { '&' } + g.write(', $addr') } else { g.write(', _MOV(($elem_type_str[]){ ') } 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..e13996b290 --- /dev/null +++ b/vlib/v/tests/fn_with_fixed_array_args_test.v @@ -0,0 +1,44 @@ +struct Test1 { +mut: + value [4]int +} + +fn (mut t Test1) set(new_value [4]int) { + t.value = new_value +} + +fn test_fn_with_fixed_array_argument_1() { + mut t := Test1{} + + println(t) + assert '$t.value' == '[0, 0, 0, 0]' + + t.set([1, 2, 3, 4]!) + + println(t) + assert '$t.value' == '[1, 2, 3, 4]' +} + +struct Test2 { +mut: + fixed_value [2][4]int + dynamic_value [][4]int +} + +fn (mut t Test2) set(index int, new_value [4]int) { + t.fixed_value[index] = new_value + t.dynamic_value << new_value +} + +fn test_fn_with_fixed_array_argument_2() { + mut t := Test2{} + + println(t) + assert '$t.fixed_value' == '[[0, 0, 0, 0], [0, 0, 0, 0]]' + assert '$t.dynamic_value' == '[]' + + t.set(0, [1, 2, 3, 4]!) + println(t) + assert '$t.fixed_value' == '[[1, 2, 3, 4], [0, 0, 0, 0]]' + assert '$t.dynamic_value' == '[[1, 2, 3, 4]]' +}