authorgravatar for gautheronjonathan@gmail.comJonathan Gautheron <gautheronjonathan@gmail.com> 2025-03-14 22:04:42+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2025-03-15 16:21:55+02:00
logea57fb55ea3e433ffb2801f818084fdc9a3c5618
tree64ff9b4f45ff757cc8a552474d132ea37a95d812
parent37bbe7e93037069fa91415d464ce927bce4c7be0

std.zig.c_translation: fix function pointer casting


1 files changed, 14 insertions(+), 0 deletions(-)

lib/std/zig/c_translation.zig+14
......@@ -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
694fn 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
698test "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}