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

cgen: fix fixed array of map (#18347)

This commit is contained in:
Felipe Pena 2023-06-05 10:46:48 -03:00 committed by GitHub
parent 254ed1766c
commit d3c679bdcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 1 deletions

View File

@ -2530,6 +2530,12 @@ pub fn (mut c Checker) expr(node_ ast.Expr) ast.Type {
unwrapped_expr_type := c.unwrap_generic(node.expr_type)
tsym := c.table.sym(unwrapped_expr_type)
if tsym.kind == .array_fixed {
info := tsym.info as ast.ArrayFixed
// for dumping fixed array we must registed the fixed array struct to return from function
c.table.find_or_register_array_fixed(info.elem_type, info.size, info.size_expr,
true)
}
type_cname := if node.expr_type.has_flag(.option) {
'_option_${tsym.cname}'
} else {

View File

@ -169,9 +169,26 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
g.write(', ')
g.expr(node.default_expr)
}
} else {
elem_sym := g.table.final_sym(node.elem_type)
if elem_sym.kind == .map {
info := array_type.unaliased_sym.info as ast.ArrayFixed
map_info := elem_sym.map_info()
g.expr(ast.MapInit{
key_type: map_info.key_type
value_type: map_info.value_type
})
for _ in 1 .. info.size {
g.write(', ')
g.expr(ast.MapInit{
key_type: map_info.key_type
value_type: map_info.value_type
})
}
} else {
g.write('0')
}
}
g.write('}')
if need_tmp_var {
g.writeln(';')

View File

@ -0,0 +1,13 @@
fn test_main() {
mut arr := [22]map[string]string{}
arr[0]['key'] = 'value'
arr[1]['key2'] = 'value2'
dump(arr[0])
assert dump(arr[0]) == {
'key': 'value'
}
assert dump(arr[1]) == {
'key2': 'value2'
}
assert dump(arr).len == 22
}