From 20265fdfc268801aa80464dda66050c736a0a59a Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 22 Nov 2024 11:36:27 +0800 Subject: [PATCH] cgen: fix dump fixed array on array append --- vlib/v/gen/c/infix.v | 7 ++++++- .../dump_fixed_array_on_array_append_test.v | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/builtin_arrays/dump_fixed_array_on_array_append_test.v diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index 8e6c53594b2d9a..b199806170ea4d 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -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) } diff --git a/vlib/v/tests/builtin_arrays/dump_fixed_array_on_array_append_test.v b/vlib/v/tests/builtin_arrays/dump_fixed_array_on_array_append_test.v new file mode 100644 index 00000000000000..0f6529f60b3596 --- /dev/null +++ b/vlib/v/tests/builtin_arrays/dump_fixed_array_on_array_append_test.v @@ -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]! +}