From 58f7342465309c1f418861c058269db64ec088a1 Mon Sep 17 00:00:00 2001 From: shove Date: Sun, 25 Sep 2022 18:05:54 +0800 Subject: [PATCH] cgen: fix array appending when array is used as fn parameter (fix: #15855) (#15860) --- vlib/v/gen/c/infix.v | 6 +++++- vlib/v/tests/array_test.v | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index c2e9220e64..4fe8e30812 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -722,7 +722,11 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) { if node.left_type.has_flag(.shared_f) { g.write('->val') } - g.write(', (') + if left.typ.is_ptr() && right.typ.is_ptr() { + g.write(', *(') + } else { + g.write(', (') + } g.expr_with_cast(node.right, node.right_type, left.unaliased.clear_flag(.shared_f)) styp := g.typ(expected_push_many_atype) g.write('), $tmp_var, $styp)') diff --git a/vlib/v/tests/array_test.v b/vlib/v/tests/array_test.v index d872ce384c..0fb01fcf01 100644 --- a/vlib/v/tests/array_test.v +++ b/vlib/v/tests/array_test.v @@ -25,3 +25,29 @@ fn test_fixed_array_to_dynamic_array() { x << 4 assert x.len == 4 } + +fn test_append_array_used_as_fn_param() { + mut arr1 := [][]string{} + mut arr2 := [][]string{} + mut arr3 := []string{} + arr4 := []string{} + arr1 << arr2 + arr1 << arr3 + arr3 << arr4 + append_2d_2d(mut arr1, arr2) + append_2d_1d(mut arr1, arr3) + append_1d_1d(mut arr3, arr4) + assert true +} + +fn append_2d_2d(mut arr1 [][]string, arr2 [][]string) { + arr1 << arr2 +} + +fn append_2d_1d(mut arr1 [][]string, arr2 []string) { + arr1 << arr2 +} + +fn append_1d_1d(mut arr1 []string, arr2 []string) { + arr1 << arr2 +}