Skip to content

Commit

Permalink
cgen: fix dump fixed array on array append
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Nov 22, 2024
1 parent 05377f3 commit 20265fd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 6 additions & 1 deletion vlib/v/gen/c/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) {
} else {
// push a single element
elem_type_str := g.styp(array_info.elem_type)
elem_sym := g.table.sym(array_info.elem_type)
elem_sym := g.table.final_sym(array_info.elem_type)
elem_is_array_var := elem_sym.kind in [.array, .array_fixed] && node.right is ast.Ident
g.write('array_push${noscan}((array*)')
if !left.typ.is_ptr()
Expand Down Expand Up @@ -1029,6 +1029,11 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) {
}
if node.right is ast.CastExpr && node.right.expr is ast.ArrayInit {
g.expr(node.right.expr)
} else if elem_sym.kind == .array_fixed
&& node.right in [ast.CallExpr, ast.DumpExpr] {
info := elem_sym.info as ast.ArrayFixed
tmpvar := g.expr_with_var(node.right, array_info.elem_type)
g.fixed_array_var_init(tmpvar, false, info.elem_type, info.size)
} else {
g.expr_with_cast(node.right, right.typ, array_info.elem_type)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
fn test_dump_fixed_array_on_array_append() {
mut myarr := [][3]u8{}
myarr << [u8(1), 2, 3]!
myarr << [u8(4), 5, 6]!
myarr << [u8(7), 8, 9]!
println(myarr)
assert myarr[0] == [u8(1), 2, 3]!
assert myarr[1] == [u8(4), 5, 6]!
assert myarr[2] == [u8(7), 8, 9]!

mut myarr2 := [][3]u8{}
myarr2 << dump([u8(1), 2, 3]!)
myarr2 << dump([u8(4), 5, 6]!)
myarr2 << dump([u8(7), 8, 9]!)
println(myarr2)
assert myarr2[0] == [u8(1), 2, 3]!
assert myarr2[1] == [u8(4), 5, 6]!
assert myarr2[2] == [u8(7), 8, 9]!
}

0 comments on commit 20265fd

Please sign in to comment.