From 2911f29058fde59ddedd8f16cac03145646de6eb Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 7 Dec 2024 23:09:17 -0300 Subject: [PATCH] checker: fix empty array append multi dims (fix #23092) (#23096) --- vlib/v/checker/containers.v | 10 +++++++++- vlib/v/tests/empty_array_push_test.v | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/empty_array_push_test.v diff --git a/vlib/v/checker/containers.v b/vlib/v/checker/containers.v index b4765b875..596d28ee4 100644 --- a/vlib/v/checker/containers.v +++ b/vlib/v/checker/containers.v @@ -169,7 +169,15 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type { } } for i, mut expr in node.exprs { - mut typ := c.check_expr_option_or_result_call(expr, c.expr(mut expr)) + mut typ := ast.void_type + if expr is ast.ArrayInit { + old_expected_type := c.expected_type + c.expected_type = c.table.value_type(c.expected_type) + typ = c.check_expr_option_or_result_call(expr, c.expr(mut expr)) + c.expected_type = old_expected_type + } else { + typ = c.check_expr_option_or_result_call(expr, c.expr(mut expr)) + } if expr is ast.CallExpr { ret_sym := c.table.sym(typ) if ret_sym.kind == .array_fixed { diff --git a/vlib/v/tests/empty_array_push_test.v b/vlib/v/tests/empty_array_push_test.v new file mode 100644 index 000000000..0fdd16d9a --- /dev/null +++ b/vlib/v/tests/empty_array_push_test.v @@ -0,0 +1,21 @@ +fn test_3dims() { + mut array := [][][]int{} + array << [[1]] + dump(array) + array << [[[1]]] + dump(array) + array << [[[]]] + println(array) + assert array == [[[int(1)]], [[1]], [[]int{}]] +} + +fn test_2dims() { + mut array := [][]int{} + array << [1] + dump(array) + array << [[1]] + dump(array) + array << [[]] + println(array) + assert array == [[int(1)], [1], []int{}] +} -- 2.39.5