Skip to content

Commit 2911f29

Browse files
authored
checker: fix empty array append multi dims (fix #23092) (#23096)
1 parent f9bb425 commit 2911f29

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

vlib/v/checker/containers.v

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,15 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
169169
}
170170
}
171171
for i, mut expr in node.exprs {
172-
mut typ := c.check_expr_option_or_result_call(expr, c.expr(mut expr))
172+
mut typ := ast.void_type
173+
if expr is ast.ArrayInit {
174+
old_expected_type := c.expected_type
175+
c.expected_type = c.table.value_type(c.expected_type)
176+
typ = c.check_expr_option_or_result_call(expr, c.expr(mut expr))
177+
c.expected_type = old_expected_type
178+
} else {
179+
typ = c.check_expr_option_or_result_call(expr, c.expr(mut expr))
180+
}
173181
if expr is ast.CallExpr {
174182
ret_sym := c.table.sym(typ)
175183
if ret_sym.kind == .array_fixed {

vlib/v/tests/empty_array_push_test.v

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
fn test_3dims() {
2+
mut array := [][][]int{}
3+
array << [[1]]
4+
dump(array)
5+
array << [[[1]]]
6+
dump(array)
7+
array << [[[]]]
8+
println(array)
9+
assert array == [[[int(1)]], [[1]], [[]int{}]]
10+
}
11+
12+
fn test_2dims() {
13+
mut array := [][]int{}
14+
array << [1]
15+
dump(array)
16+
array << [[1]]
17+
dump(array)
18+
array << [[]]
19+
println(array)
20+
assert array == [[int(1)], [1], []int{}]
21+
}

0 commit comments

Comments
 (0)