cgen: fix array of fixed array (fix #13895) (#13901)

pull/13911/head
yuyi 2022-04-02 23:00:59 +08:00 committed by GitHub
parent 0bf0c73a49
commit faa55b46de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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_var := elem_sym.kind in [.array, .array_fixed] && node.right is ast.Ident
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_var {
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_var {
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]'
}