Skip to content

Commit d649f5a

Browse files
authored
checker, cgen: fix go call fn using map value (#15665)
1 parent 90c2c5b commit d649f5a

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

vlib/v/checker/fn.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,8 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
617617
if elem_sym.info is ast.FnType {
618618
func = elem_sym.info.func
619619
found = true
620+
node.is_fn_var = true
621+
node.fn_var_type = sym.info.elem_type
620622
} else {
621623
c.error('cannot call the element of the array, it is not a function',
622624
node.pos)
@@ -626,6 +628,8 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
626628
if value_sym.info is ast.FnType {
627629
func = value_sym.info.func
628630
found = true
631+
node.is_fn_var = true
632+
node.fn_var_type = sym.info.value_type
629633
} else {
630634
c.error('cannot call the value of the map, it is not a function', node.pos)
631635
}
@@ -634,6 +638,8 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
634638
if elem_sym.info is ast.FnType {
635639
func = elem_sym.info.func
636640
found = true
641+
node.is_fn_var = true
642+
node.fn_var_type = sym.info.elem_type
637643
} else {
638644
c.error('cannot call the element of the array, it is not a function',
639645
node.pos)

vlib/v/gen/c/fn.v

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,17 @@ fn (mut g Gen) go_expr(node ast.GoExpr) {
17221722
g.gen_anon_fn_decl(mut expr.left)
17231723
name = expr.left.decl.name
17241724
}
1725+
} else if expr.left is ast.IndexExpr {
1726+
if expr.is_fn_var {
1727+
fn_sym := g.table.sym(expr.fn_var_type)
1728+
func := (fn_sym.info as ast.FnType).func
1729+
fn_var := g.fn_var_signature(func.return_type, func.params.map(it.typ), tmp_fn)
1730+
g.write('\t$fn_var = ')
1731+
g.expr(expr.left)
1732+
g.writeln(';')
1733+
name = fn_sym.cname
1734+
use_tmp_fn_var = true
1735+
}
17251736
}
17261737
name = util.no_dots(name)
17271738
if g.pref.obfuscate && g.cur_mod.name == 'main' && name.starts_with('main__') {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module main
2+
3+
fn test_go_call_fn_using_map_value() {
4+
sum := fn (x int, y int) int {
5+
return x + y
6+
}
7+
8+
mut fns := map[string]fn (int, int) int{}
9+
fns['sum'] = sum
10+
11+
g := go fns['sum'](2, 3)
12+
x := g.wait()
13+
14+
println('$x')
15+
assert x == 5
16+
}

0 commit comments

Comments
 (0)