add behavior test for ptrcasted function pointers

See #2626. The runtime case is solved but comptime is not.
This commit is contained in:
Andrew Kelley 2022-12-27 15:19:00 -07:00
parent 6e9fbc83ca
commit 357235d9de

View File

@ -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();
}