From 18b821666e13bf6d0279f328d2575e1b10bf157f Mon Sep 17 00:00:00 2001 From: Jonathan Gautheron Date: Fri, 14 Mar 2025 22:04:42 +0100 Subject: [PATCH] std.zig.c_translation: fix function pointer casting --- lib/std/zig/c_translation.zig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/std/zig/c_translation.zig b/lib/std/zig/c_translation.zig index bc3e8935960610c9503a82b009d27f92a54e35ac..813b409c903cd4d39991bec8135099cdf1955370 100644 --- a/lib/std/zig/c_translation.zig +++ b/lib/std/zig/c_translation.zig @@ -88,6 +88,9 @@ fn castToPtr(comptime DestType: type, comptime SourceType: type, target: anytype .pointer => { return castPtr(DestType, target); }, + .@"fn" => { + return castPtr(DestType, &target); + }, .optional => |target_opt| { if (@typeInfo(target_opt.child) == .pointer) { return castPtr(DestType, target); @@ -686,3 +689,14 @@ test "Extended C ABI casting" { try testing.expect(@TypeOf(Macros.L_SUFFIX(math.maxInt(c_long) + 1)) == c_longlong); // comptime_int -> c_longlong } } + +// Function with complex signature for testing the SDL case +fn complexFunction(_: ?*anyopaque, _: c_uint, _: ?*const fn (?*anyopaque) callconv(.c) c_uint, _: ?*anyopaque, _: c_uint, _: [*c]c_uint) callconv(.c) usize { + return 0; +} + +test "function pointer casting" { + const SDL_FunctionPointer = ?*const fn () callconv(.c) void; + const fn_ptr = cast(SDL_FunctionPointer, complexFunction); + try testing.expect(fn_ptr != null); +} -- 2.54.0