authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-27 15:19:00-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-27 15:19:00-07:00
log357235d9de9feb9d5b29d40574bce10e23382c79
tree06c4d680e6bd391351b2561473a33ea82679d5ab
parent6e9fbc83ca2cfe145d73a102e4175e49524d0dee

add behavior test for ptrcasted function pointers

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

1 files changed, 27 insertions(+), 0 deletions(-)

test/behavior/fn.zig+27
......@@ -457,3 +457,30 @@ test "method call with optional and error union first param" {
457457 try s.opt();
458458 try s.errUnion();
459459}
460
461test "using @ptrCast on function pointers" {
462 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
463 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
464 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
465 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
466
467 const S = struct {
468 const A = struct { data: [4]u8 };
469
470 fn at(arr: *const A, index: usize) *const u8 {
471 return &arr.data[index];
472 }
473
474 fn run() !void {
475 const a = A{ .data = "abcd".* };
476 const casted_fn = @ptrCast(*const fn (*const anyopaque, usize) *const u8, &at);
477 const casted_impl = @ptrCast(*const anyopaque, &a);
478 const ptr = casted_fn(casted_impl, 2);
479 try expect(ptr.* == 'c');
480 }
481 };
482
483 try S.run();
484 // https://github.com/ziglang/zig/issues/2626
485 // try comptime S.run();
486}