authorgravatar for gautheronjonathan@gmail.comJonathan Gautheron <gautheronjonathan@gmail.com> 2025-03-14 22:04:42+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-03-18 04:58:03+01:00
log18b821666e13bf6d0279f328d2575e1b10bf157f
tree4f2660bd115d4598a0e3684dc17ea45751a11e0b
parent6c690a966ad21bfa7fabf1b1f9a25bb2d3f826cb
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

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}