1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix nested fixed array instantiation (#18357)

This commit is contained in:
Felipe Pena 2023-06-06 13:57:40 -03:00 committed by GitHub
parent e97aff8742
commit 125921db66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -172,6 +172,7 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
} else {
elem_sym := g.table.final_sym(node.elem_type)
if elem_sym.kind == .map {
// fixed array for map -- [N]map[key_type]value_type
info := array_type.unaliased_sym.info as ast.ArrayFixed
map_info := elem_sym.map_info()
g.expr(ast.MapInit{
@ -185,6 +186,21 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
value_type: map_info.value_type
})
}
} else if elem_sym.kind == .array_fixed {
// nested fixed array -- [N][N]type
info := array_type.unaliased_sym.info as ast.ArrayFixed
arr_info := elem_sym.array_fixed_info()
g.expr(ast.ArrayInit{
typ: node.elem_type
elem_type: arr_info.elem_type
})
for _ in 1 .. info.size {
g.write(', ')
g.expr(ast.ArrayInit{
typ: node.elem_type
elem_type: arr_info.elem_type
})
}
} else {
g.write('0')
}

View File

@ -0,0 +1,8 @@
fn test_main() {
mut arr := [2][3][2]map[string]string{}
arr[0][0][1]['key'] = 'value'
dump(arr)
assert arr[0][0][1] == {
'key': 'value'
}
}