cgen: fix map['xxx']() error (#7342)

pull/7351/head
yuyi 2020-12-15 23:07:24 +08:00 committed by GitHub
parent 239a8c8aa3
commit e40e1500a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 5 deletions

View File

@ -3765,14 +3765,14 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
if needs_clone {
g.write('/*2*/string_clone(')
}
if is_direct_array_access {
g.write('(($array_ptr_type_str)')
} else if g.is_fn_index_call {
if g.is_fn_index_call {
if elem_typ.info is table.FnType {
g.write('((')
g.write_fn_ptr_decl(&elem_typ.info, '')
g.write(')(*($array_ptr_type_str)/*ee elem_typ */array_get(')
}
} else if is_direct_array_access {
g.write('(($array_ptr_type_str)')
} else {
g.write('(*($array_ptr_type_str)/*ee elem_typ */array_get(')
if left_is_ptr && !node.left_type.has_flag(.shared_f) {
@ -3857,7 +3857,13 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
g.write(', &($elem_type_str[]){ $zero }))')
} else {
zero := g.type_default(info.value_type)
if elem_typ.kind == .function {
if g.is_fn_index_call {
if elem_typ.info is table.FnType {
g.write('((')
g.write_fn_ptr_decl(&elem_typ.info, '')
g.write(')(*(voidptr*)map_get(')
}
} else if elem_typ.kind == .function {
g.write('(*(voidptr*)map_get(')
} else {
g.write('(*($elem_type_str*)map_get(')
@ -3875,7 +3881,9 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
}
g.write(', ')
g.expr(node.index)
if elem_typ.kind == .function {
if g.is_fn_index_call {
g.write(', &(voidptr[]){ $zero })))')
} else if elem_typ.kind == .function {
g.write(', &(voidptr[]){ $zero }))')
} else {
g.write(', &($elem_type_str[]){ $zero }))')

View File

@ -13,5 +13,17 @@ fn test_fn_array_direct_call() {
return false
}
println(fs.array_of_fn[0](1, &Placeholder{name: 'Bob'}, 'Builder'))
assert fs.array_of_fn[0](1, &Placeholder{name: 'Bob'}, 'Builder') == false
}
fn test_fn_map_direct_call() {
a := {
'aaa': fn()string {return 'aaa'},
'bbb': fn()string {return 'bbb'},
}
println(a['aaa']())
println(a['bbb']())
assert a['aaa']() == 'aaa'
assert a['bbb']() == 'bbb'
}