cgen: fix array of array/map init (fix #7597) (#7671)

pull/7683/head
yuyi 2020-12-29 18:50:25 +08:00 committed by GitHub
parent d094baf107
commit 34c89258a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View File

@ -1291,3 +1291,19 @@ fn test_array_struct_ref_index() {
println(coords.index(coord_1))
assert coords.index(coord_1) == 0
}
fn test_array_of_array_append() {
mut x := [][]int{len: 4}
println(x) // OK
x[2] << 123 // RTE
println(x)
assert '$x' == '[[], [], [123], []]'
}
fn test_array_of_map_insert() {
mut x := []map[string]int{len: 4}
println(x) // OK
x[2]['123'] = 123 // RTE
println(x)
assert '$x' == "[{}, {}, {'123': 123}, {}]"
}

View File

@ -79,6 +79,10 @@ fn (mut g Gen) array_init(it ast.ArrayInit) {
g.write('&($elem_type_str[]){')
g.write('_SLIT("")')
g.write('})')
} else if it.has_len && elem_sym.kind in [.array, .map] {
g.write('(voidptr)&($elem_type_str[]){')
g.write(g.type_default(it.elem_type))
g.write('}[0])')
} else {
g.write('0)')
}