From c2ecfb32d9c64c782271b85fc214126e08ee42b3 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 28 Apr 2022 19:43:20 +0800 Subject: [PATCH] cgen: fix go anon fn call with ref argument (fix #14192) (#14197) --- vlib/v/gen/c/fn.v | 7 +++++++ vlib/v/tests/go_anon_fn_call_with_ref_arg_test.v | 15 +++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 vlib/v/tests/go_anon_fn_call_with_ref_arg_test.v diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 5bd571cc8b..5944d7fc77 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -1922,6 +1922,13 @@ fn (mut g Gen) go_expr(node ast.GoExpr) { g.gowrappers.write_string(call_args_str) } else { for i in 0 .. expr.args.len { + expected_nr_muls := expr.expected_arg_types[i].nr_muls() + arg_nr_muls := expr.args[i].typ.nr_muls() + if arg_nr_muls > expected_nr_muls { + g.gowrappers.write_string('*'.repeat(arg_nr_muls - expected_nr_muls)) + } else if arg_nr_muls < expected_nr_muls { + g.gowrappers.write_string('&'.repeat(expected_nr_muls - arg_nr_muls)) + } g.gowrappers.write_string('arg->arg${i + 1}') if i != expr.args.len - 1 { g.gowrappers.write_string(', ') diff --git a/vlib/v/tests/go_anon_fn_call_with_ref_arg_test.v b/vlib/v/tests/go_anon_fn_call_with_ref_arg_test.v new file mode 100644 index 0000000000..4caf394279 --- /dev/null +++ b/vlib/v/tests/go_anon_fn_call_with_ref_arg_test.v @@ -0,0 +1,15 @@ +struct Foo { + bar string +} + +fn test_go_anon_fn_call_with_ref_arg() { + foo := &Foo{ + bar: 'hello' + } + g := go fn (foo Foo) string { + return foo.bar + }(foo) + ret := g.wait() + println(ret) + assert ret == 'hello' +}