zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit ea57fb55ea3e433ffb2801f818084fdc9a3c5618 (tree)
parent 37bbe7e93037069fa91415d464ce927bce4c7be0
Author: Jonathan Gautheron <gautheronjonathan@gmail.com>
Date:   Fri, 14 Mar 2025 22:04:42 +0100

std.zig.c_translation: fix function pointer casting

Diffstat:
Mlib/std/zig/c_translation.zig | 14++++++++++++++
1 file changed, 14 insertions(+), 0 deletions(-)

diff --git 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); +}