cgen: fix array of fixed array

pull/13901/head
yuyi98 2022-04-02 14:30:08 +08:00
parent af79c1e6ef
commit 019f1a8c80
2 changed files with 23 additions and 3 deletions

View File

@ -613,9 +613,8 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) {
// push a single element
elem_type_str := g.typ(array_info.elem_type)
elem_sym := g.table.sym(array_info.elem_type)
elem_is_array := elem_sym.kind in [.array, .array_fixed] && node.right !is ast.ArrayInit
g.write('array_push${noscan}((array*)')
if node.left_type.has_flag(.shared_f) && !node.left_type.deref().is_ptr() {
}
if !left.typ.is_ptr()
|| (node.left_type.has_flag(.shared_f) && !node.left_type.deref().is_ptr()) {
g.write('&')
@ -626,6 +625,8 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) {
}
if elem_sym.kind == .function {
g.write(', _MOV((voidptr[]){ ')
} else if elem_is_array {
g.write(', &')
} else {
g.write(', _MOV(($elem_type_str[]){ ')
}
@ -638,7 +639,11 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) {
if needs_clone {
g.write(')')
}
g.write(' }))')
if elem_is_array {
g.write(')')
} else {
g.write(' }))')
}
}
} else {
g.gen_plain_infix_expr(node)

View File

@ -0,0 +1,15 @@
fn test_array_of_fixed_array() {
mut arr := [][3]int{}
fixed_arr1 := [3]int{}
arr << fixed_arr1
fixed_arr2 := [1, 2, 3]!
arr << fixed_arr2
println(arr)
assert arr.len == 2
assert '${arr[0]}' == '[0, 0, 0]'
assert '${arr[1]}' == '[1, 2, 3]'
}