From d577e54152e9ca1503a1147aa7398e915c0c06cd Mon Sep 17 00:00:00 2001 From: shove Date: Wed, 22 Nov 2023 21:34:43 +0800 Subject: [PATCH] cgen: fix ref and deref when an interface is used as a function parameter (fix #19947) (#19966) --- vlib/v/gen/c/fn.v | 4 ++++ vlib/v/tests/fn_call_interface_args_test.v | 12 ++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 vlib/v/tests/fn_call_interface_args_test.v diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 1cf04ecb17..7bdc0308a5 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -2318,6 +2318,10 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as g.write(')') return } + } else if arg_sym.kind == .interface_ && exp_sym.kind == .interface_ { + if exp_is_ptr && !arg_is_ptr { + g.write('&') + } } } } else if arg_typ.has_flag(.shared_f) && !expected_type.has_flag(.shared_f) { diff --git a/vlib/v/tests/fn_call_interface_args_test.v b/vlib/v/tests/fn_call_interface_args_test.v new file mode 100644 index 0000000000..f5e44138fe --- /dev/null +++ b/vlib/v/tests/fn_call_interface_args_test.v @@ -0,0 +1,12 @@ +interface Foo {} + +fn has_interface_args(mut a Foo, b &Foo, c Foo) { + assert a == &Foo(1) + assert b == &Foo(1) + assert c == Foo(1) +} + +fn test_fn_call_interface_args() { + mut arg := Foo(1) + has_interface_args(mut arg, arg, arg) +}