cgen: fix dump fixed array on array append (fix #22935) (#22940)

This commit is contained in:
yuyi 2024-11-22 19:06:52 +08:00 committed by GitHub
parent 05377f3c03
commit 7d5e513c69
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View File

@ -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()
@ -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)
}

View File

@ -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]!
}