Commit ea57fb55ea

Jonathan Gautheron <gautheronjonathan@gmail.com>
2025-03-14 22:04:42
std.zig.c_translation: fix function pointer casting
1 parent 37bbe7e
Changed files (1)
lib
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);
+}