zig

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

commit 357235d9de9feb9d5b29d40574bce10e23382c79 (tree)
parent 6e9fbc83ca2cfe145d73a102e4175e49524d0dee
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Tue, 27 Dec 2022 15:19:00 -0700

add behavior test for ptrcasted function pointers

See #2626. The runtime case is solved but comptime is not.

Diffstat:
Mtest/behavior/fn.zig | 27+++++++++++++++++++++++++++
1 file changed, 27 insertions(+), 0 deletions(-)

diff --git a/test/behavior/fn.zig b/test/behavior/fn.zig @@ -457,3 +457,30 @@ test "method call with optional and error union first param" { try s.opt(); try s.errUnion(); } + +test "using @ptrCast on function pointers" { + if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + + const S = struct { + const A = struct { data: [4]u8 }; + + fn at(arr: *const A, index: usize) *const u8 { + return &arr.data[index]; + } + + fn run() !void { + const a = A{ .data = "abcd".* }; + const casted_fn = @ptrCast(*const fn (*const anyopaque, usize) *const u8, &at); + const casted_impl = @ptrCast(*const anyopaque, &a); + const ptr = casted_fn(casted_impl, 2); + try expect(ptr.* == 'c'); + } + }; + + try S.run(); + // https://github.com/ziglang/zig/issues/2626 + // try comptime S.run(); +}