diff --git a/test/behavior/fn.zig b/test/behavior/fn.zig index d33d1aad71..e306fdc207 100644 --- 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(); +}