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

cgen: fix nested map of fn call (#18142)

This commit is contained in:
yuyi 2023-05-09 21:19:25 +08:00 committed by GitHub
parent e2e6c9660c
commit 89f3288fb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -474,6 +474,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) {
}
tmp_opt := if gen_or { g.new_tmp_var() } else { '' }
tmp_opt_ptr := if gen_or { g.new_tmp_var() } else { '' }
mut is_fn_last_index_call := false
if gen_or {
g.write('${elem_type_str}* ${tmp_opt_ptr} = (${elem_type_str}*)(map_get_check(')
} else {
@ -482,6 +483,8 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) {
g.write('((')
g.write_fn_ptr_decl(&elem_sym.info, '')
g.write(')(*(voidptr*)map_get(')
is_fn_last_index_call = true
g.is_fn_index_call = false
}
} else {
g.write('(*(${elem_type_str}*)map_get(')
@ -506,7 +509,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) {
g.write('}')
if gen_or {
g.write('))')
} else if g.is_fn_index_call {
} else if is_fn_last_index_call {
g.write(', &(voidptr[]){ ${zero} })))')
} else {
g.write(', &(${elem_type_str}[]){ ${zero} }))')

View File

@ -0,0 +1,9 @@
fn test_nested_map_of_fn_call() {
mut a := map[string]map[string]fn () int{}
a['a']['a'] = fn () int {
return 0
}
b := a['a']['a']()
println(b)
assert b == 0
}