From f33caca7eb611a388f1e852e67556612a0657b51 Mon Sep 17 00:00:00 2001 From: CreeperFace <165158232+dy-tea@users.noreply.github.com> Date: Thu, 13 Nov 2025 08:07:29 +0000 Subject: [PATCH] cgen: correct function definitions for callbacks in imported modules (fix #25700) (#25719) --- vlib/v/gen/c/cgen.v | 2 +- vlib/v/gen/c/fn.v | 4 ++-- .../callback_consumer_test.v | 7 +++++++ .../tests/modules/callback_consumer_module/main.v | 8 ++++++++ .../callback_consumer_module/mod1/faulty.v | 15 +++++++++++++++ .../callback_consumer_module/mod2/faulty.v | 15 +++++++++++++++ .../tests/modules/callback_consumer_module/v.mod | 0 7 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/modules/callback_consumer_module/callback_consumer_test.v create mode 100644 vlib/v/tests/modules/callback_consumer_module/main.v create mode 100644 vlib/v/tests/modules/callback_consumer_module/mod1/faulty.v create mode 100644 vlib/v/tests/modules/callback_consumer_module/mod2/faulty.v create mode 100644 vlib/v/tests/modules/callback_consumer_module/v.mod diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index d8d542505..18cee342e 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -1980,7 +1980,7 @@ pub fn (mut g Gen) write_fn_typesymbol_declaration(sym ast.TypeSymbol) { '' } ret_typ := - if !func.return_type.has_flag(.option) && g.table.sym(func.return_type).kind == .array_fixed { '_v_' } else { '' } + + if !func.return_type.has_flag(.option) && !func.return_type.has_flag(.result) && g.table.sym(func.return_type).kind == .array_fixed { '_v_' } else { '' } + g.styp(func.return_type) g.type_definitions.write_string('typedef ${ret_typ} (${msvc_call_conv}*${fn_name})(') for i, param in func.params { diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 07e51a1a2..eafb592f9 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -791,9 +791,9 @@ fn (mut g Gen) fn_decl_params(params []ast.Param, scope &ast.Scope, is_variadic info := param_type_sym.info as ast.FnType func := info.func if !g.inside_c_extern { - g.write('${g.styp(func.return_type)} (*${caname})(') + g.write('${g.ret_styp(func.return_type)} (*${caname})(') } - g.definitions.write_string('${g.styp(func.return_type)} (*${caname})(') + g.definitions.write_string('${g.ret_styp(func.return_type)} (*${caname})(') g.fn_decl_params(func.params, unsafe { nil }, func.is_variadic, func.is_c_variadic) if !g.inside_c_extern { g.write(')') diff --git a/vlib/v/tests/modules/callback_consumer_module/callback_consumer_test.v b/vlib/v/tests/modules/callback_consumer_module/callback_consumer_test.v new file mode 100644 index 000000000..353ba9360 --- /dev/null +++ b/vlib/v/tests/modules/callback_consumer_module/callback_consumer_test.v @@ -0,0 +1,7 @@ +import mod1 +import mod2 + +fn test_main() { + mod1.function()! + mod2.function() +} diff --git a/vlib/v/tests/modules/callback_consumer_module/main.v b/vlib/v/tests/modules/callback_consumer_module/main.v new file mode 100644 index 000000000..a4c4f84aa --- /dev/null +++ b/vlib/v/tests/modules/callback_consumer_module/main.v @@ -0,0 +1,8 @@ +module main + +import mod1 +import mod2 + +fn main() { + dump('main') +} diff --git a/vlib/v/tests/modules/callback_consumer_module/mod1/faulty.v b/vlib/v/tests/modules/callback_consumer_module/mod1/faulty.v new file mode 100644 index 000000000..dd68f3181 --- /dev/null +++ b/vlib/v/tests/modules/callback_consumer_module/mod1/faulty.v @@ -0,0 +1,15 @@ +module mod1 + +pub fn function() ! { + callback_consumer(callback_fn)! +} + +type CallbackType = fn () ![4]u8 + +fn callback_consumer(callback CallbackType) ! { + callback()! +} + +fn callback_fn() ![4]u8 { + return [u8(1), 2, 3, 255]! +} diff --git a/vlib/v/tests/modules/callback_consumer_module/mod2/faulty.v b/vlib/v/tests/modules/callback_consumer_module/mod2/faulty.v new file mode 100644 index 000000000..8bf4ad227 --- /dev/null +++ b/vlib/v/tests/modules/callback_consumer_module/mod2/faulty.v @@ -0,0 +1,15 @@ +module mod2 + +pub fn function() { + callback_consumer(callback_fn) +} + +type CallbackType = fn () [4]u8 + +fn callback_consumer(callback CallbackType) { + callback() +} + +fn callback_fn() [4]u8 { + return [u8(1), 2, 3, 255]! +} diff --git a/vlib/v/tests/modules/callback_consumer_module/v.mod b/vlib/v/tests/modules/callback_consumer_module/v.mod new file mode 100644 index 000000000..e69de29bb -- 2.39.5