mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker, cgen: fix go call fn using map value (#15665)
This commit is contained in:
parent
90c2c5b8a4
commit
d649f5aff4
@ -617,6 +617,8 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
|
|||||||
if elem_sym.info is ast.FnType {
|
if elem_sym.info is ast.FnType {
|
||||||
func = elem_sym.info.func
|
func = elem_sym.info.func
|
||||||
found = true
|
found = true
|
||||||
|
node.is_fn_var = true
|
||||||
|
node.fn_var_type = sym.info.elem_type
|
||||||
} else {
|
} else {
|
||||||
c.error('cannot call the element of the array, it is not a function',
|
c.error('cannot call the element of the array, it is not a function',
|
||||||
node.pos)
|
node.pos)
|
||||||
@ -626,6 +628,8 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
|
|||||||
if value_sym.info is ast.FnType {
|
if value_sym.info is ast.FnType {
|
||||||
func = value_sym.info.func
|
func = value_sym.info.func
|
||||||
found = true
|
found = true
|
||||||
|
node.is_fn_var = true
|
||||||
|
node.fn_var_type = sym.info.value_type
|
||||||
} else {
|
} else {
|
||||||
c.error('cannot call the value of the map, it is not a function', node.pos)
|
c.error('cannot call the value of the map, it is not a function', node.pos)
|
||||||
}
|
}
|
||||||
@ -634,6 +638,8 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
|
|||||||
if elem_sym.info is ast.FnType {
|
if elem_sym.info is ast.FnType {
|
||||||
func = elem_sym.info.func
|
func = elem_sym.info.func
|
||||||
found = true
|
found = true
|
||||||
|
node.is_fn_var = true
|
||||||
|
node.fn_var_type = sym.info.elem_type
|
||||||
} else {
|
} else {
|
||||||
c.error('cannot call the element of the array, it is not a function',
|
c.error('cannot call the element of the array, it is not a function',
|
||||||
node.pos)
|
node.pos)
|
||||||
|
@ -1722,6 +1722,17 @@ fn (mut g Gen) go_expr(node ast.GoExpr) {
|
|||||||
g.gen_anon_fn_decl(mut expr.left)
|
g.gen_anon_fn_decl(mut expr.left)
|
||||||
name = expr.left.decl.name
|
name = expr.left.decl.name
|
||||||
}
|
}
|
||||||
|
} else if expr.left is ast.IndexExpr {
|
||||||
|
if expr.is_fn_var {
|
||||||
|
fn_sym := g.table.sym(expr.fn_var_type)
|
||||||
|
func := (fn_sym.info as ast.FnType).func
|
||||||
|
fn_var := g.fn_var_signature(func.return_type, func.params.map(it.typ), tmp_fn)
|
||||||
|
g.write('\t$fn_var = ')
|
||||||
|
g.expr(expr.left)
|
||||||
|
g.writeln(';')
|
||||||
|
name = fn_sym.cname
|
||||||
|
use_tmp_fn_var = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
name = util.no_dots(name)
|
name = util.no_dots(name)
|
||||||
if g.pref.obfuscate && g.cur_mod.name == 'main' && name.starts_with('main__') {
|
if g.pref.obfuscate && g.cur_mod.name == 'main' && name.starts_with('main__') {
|
||||||
|
16
vlib/v/tests/go_call_fn_using_map_value_test.v
Normal file
16
vlib/v/tests/go_call_fn_using_map_value_test.v
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
module main
|
||||||
|
|
||||||
|
fn test_go_call_fn_using_map_value() {
|
||||||
|
sum := fn (x int, y int) int {
|
||||||
|
return x + y
|
||||||
|
}
|
||||||
|
|
||||||
|
mut fns := map[string]fn (int, int) int{}
|
||||||
|
fns['sum'] = sum
|
||||||
|
|
||||||
|
g := go fns['sum'](2, 3)
|
||||||
|
x := g.wait()
|
||||||
|
|
||||||
|
println('$x')
|
||||||
|
assert x == 5
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user