Skip to content

Commit ea57fb5

Browse files
jgautheronVexu
authored andcommitted
std.zig.c_translation: fix function pointer casting
1 parent 37bbe7e commit ea57fb5

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lib/std/zig/c_translation.zig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ fn castToPtr(comptime DestType: type, comptime SourceType: type, target: anytype
8888
.pointer => {
8989
return castPtr(DestType, target);
9090
},
91+
.@"fn" => {
92+
return castPtr(DestType, &target);
93+
},
9194
.optional => |target_opt| {
9295
if (@typeInfo(target_opt.child) == .pointer) {
9396
return castPtr(DestType, target);
@@ -686,3 +689,14 @@ test "Extended C ABI casting" {
686689
try testing.expect(@TypeOf(Macros.L_SUFFIX(math.maxInt(c_long) + 1)) == c_longlong); // comptime_int -> c_longlong
687690
}
688691
}
692+
693+
// Function with complex signature for testing the SDL case
694+
fn complexFunction(_: ?*anyopaque, _: c_uint, _: ?*const fn (?*anyopaque) callconv(.c) c_uint, _: ?*anyopaque, _: c_uint, _: [*c]c_uint) callconv(.c) usize {
695+
return 0;
696+
}
697+
698+
test "function pointer casting" {
699+
const SDL_FunctionPointer = ?*const fn () callconv(.c) void;
700+
const fn_ptr = cast(SDL_FunctionPointer, complexFunction);
701+
try testing.expect(fn_ptr != null);
702+
}

0 commit comments

Comments
 (0)