Skip to content

Commit f33caca

Browse files
authored
cgen: correct function definitions for callbacks in imported modules (fix #25700) (#25719)
1 parent 6a590f1 commit f33caca

File tree

7 files changed

+48
-3
lines changed

7 files changed

+48
-3
lines changed

vlib/v/gen/c/cgen.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1980,7 +1980,7 @@ pub fn (mut g Gen) write_fn_typesymbol_declaration(sym ast.TypeSymbol) {
19801980
''
19811981
}
19821982
ret_typ :=
1983-
if !func.return_type.has_flag(.option) && g.table.sym(func.return_type).kind == .array_fixed { '_v_' } else { '' } +
1983+
if !func.return_type.has_flag(.option) && !func.return_type.has_flag(.result) && g.table.sym(func.return_type).kind == .array_fixed { '_v_' } else { '' } +
19841984
g.styp(func.return_type)
19851985
g.type_definitions.write_string('typedef ${ret_typ} (${msvc_call_conv}*${fn_name})(')
19861986
for i, param in func.params {

vlib/v/gen/c/fn.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,9 @@ fn (mut g Gen) fn_decl_params(params []ast.Param, scope &ast.Scope, is_variadic
791791
info := param_type_sym.info as ast.FnType
792792
func := info.func
793793
if !g.inside_c_extern {
794-
g.write('${g.styp(func.return_type)} (*${caname})(')
794+
g.write('${g.ret_styp(func.return_type)} (*${caname})(')
795795
}
796-
g.definitions.write_string('${g.styp(func.return_type)} (*${caname})(')
796+
g.definitions.write_string('${g.ret_styp(func.return_type)} (*${caname})(')
797797
g.fn_decl_params(func.params, unsafe { nil }, func.is_variadic, func.is_c_variadic)
798798
if !g.inside_c_extern {
799799
g.write(')')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import mod1
2+
import mod2
3+
4+
fn test_main() {
5+
mod1.function()!
6+
mod2.function()
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module main
2+
3+
import mod1
4+
import mod2
5+
6+
fn main() {
7+
dump('main')
8+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module mod1
2+
3+
pub fn function() ! {
4+
callback_consumer(callback_fn)!
5+
}
6+
7+
type CallbackType = fn () ![4]u8
8+
9+
fn callback_consumer(callback CallbackType) ! {
10+
callback()!
11+
}
12+
13+
fn callback_fn() ![4]u8 {
14+
return [u8(1), 2, 3, 255]!
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module mod2
2+
3+
pub fn function() {
4+
callback_consumer(callback_fn)
5+
}
6+
7+
type CallbackType = fn () [4]u8
8+
9+
fn callback_consumer(callback CallbackType) {
10+
callback()
11+
}
12+
13+
fn callback_fn() [4]u8 {
14+
return [u8(1), 2, 3, 255]!
15+
}

vlib/v/tests/modules/callback_consumer_module/v.mod

Whitespace-only changes.

0 commit comments

Comments
 (0)