From e4973782b11cd72888be8220cb1d802a04b2e2ff Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 19 Dec 2020 14:28:15 +0800 Subject: [PATCH] cgen: fix multi_array prepend/insert (#7381) --- vlib/builtin/array_test.v | 20 ++++++++++++++++++++ vlib/v/gen/cgen.v | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index 19542a1424..e8b9177591 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -1181,3 +1181,23 @@ fn test_voidptr_vbytes() { println(bytes) } } + +fn test_multi_array_prepend() { + mut a := [][]int{} + a.prepend([1, 2, 3]) + assert a == [[1, 2, 3]] + + mut b := [][]int{} + b.prepend([[1, 2, 3]]) + assert b == [[1, 2, 3]] +} + +fn test_multi_array_insert() { + mut a := [][]int{} + a.insert(0, [1, 2, 3]) + assert a == [[1, 2, 3]] + + mut b := [][]int{} + b.insert(0, [[1, 2, 3]]) + assert b == [[1, 2, 3]] +} diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index cf2d3ac343..cc88eab10e 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -5041,7 +5041,7 @@ fn (mut g Gen) gen_array_insert(node ast.CallExpr) { left_info := left_sym.info as table.Array elem_type_str := g.typ(left_info.elem_type) arg2_sym := g.table.get_type_symbol(node.args[1].typ) - is_arg2_array := arg2_sym.kind == .array + is_arg2_array := arg2_sym.kind == .array && node.args[1].typ == node.left_type if is_arg2_array { g.write('array_insert_many(&') } else { @@ -5075,7 +5075,7 @@ fn (mut g Gen) gen_array_prepend(node ast.CallExpr) { left_info := left_sym.info as table.Array elem_type_str := g.typ(left_info.elem_type) arg_sym := g.table.get_type_symbol(node.args[0].typ) - is_arg_array := arg_sym.kind == .array + is_arg_array := arg_sym.kind == .array && node.args[0].typ == node.left_type if is_arg_array { g.write('array_prepend_many(&') } else {