From 9887dd1fbfded6e81e702fa1ae626fe4e441fe59 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 17 Aug 2022 21:12:52 +0800 Subject: [PATCH] cgen: fix go call fn with anon fn argument (fix #10351, fix #10270) (#15446) --- vlib/v/gen/c/fn.v | 11 +++++++++-- vlib/v/tests/go_call_fn_with_anon_fn_arg_test.v | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/go_call_fn_with_anon_fn_arg_test.v diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 428098443a..fb2f154130 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -1826,8 +1826,15 @@ fn (mut g Gen) go_expr(node ast.GoExpr) { g.type_definitions.writeln('EMPTY_STRUCT_DECLARATION;') } else { for i, arg in expr.args { - styp := g.typ(arg.typ) - g.type_definitions.writeln('\t$styp arg${i + 1};') + arg_sym := g.table.sym(arg.typ) + if arg_sym.info is ast.FnType { + sig := g.fn_var_signature(arg_sym.info.func.return_type, arg_sym.info.func.params, + 'arg${i + 1}') + g.type_definitions.writeln('\t' + sig + ';') + } else { + styp := g.typ(arg.typ) + g.type_definitions.writeln('\t$styp arg${i + 1};') + } } } if need_return_ptr { diff --git a/vlib/v/tests/go_call_fn_with_anon_fn_arg_test.v b/vlib/v/tests/go_call_fn_with_anon_fn_arg_test.v new file mode 100644 index 0000000000..f86bb24942 --- /dev/null +++ b/vlib/v/tests/go_call_fn_with_anon_fn_arg_test.v @@ -0,0 +1,14 @@ +fn start(f fn ()) string { + f() + return 'ok!!' +} + +fn on_connect() { + println('fn ok!!') +} + +fn test_go_call_fn_with_anon_fn_arg() { + g := go start(on_connect) + ret := g.wait() + assert ret == 'ok!!' +}