| ... | ... | @@ -457,3 +457,30 @@ test "method call with optional and error union first param" { |
| 457 | 457 | try s.opt(); |
| 458 | 458 | try s.errUnion(); |
| 459 | 459 | } |
| 460 | |
| 461 | test "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 | } |