Skip to content

Commit

Permalink
checker: fix empty array append multi dims (fix #23092) (#23096)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Dec 8, 2024
1 parent f9bb425 commit 2911f29
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
10 changes: 9 additions & 1 deletion vlib/v/checker/containers.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions vlib/v/tests/empty_array_push_test.v
Original file line number Diff line number Diff line change
@@ -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{}]
}

0 comments on commit 2911f29

Please sign in to comment.