Commit 357235d9de

Andrew Kelley <andrew@ziglang.org>
2022-12-27 23:19:00
add behavior test for ptrcasted function pointers
See #2626. The runtime case is solved but comptime is not.
1 parent 6e9fbc8
Changed files (1)
test
behavior
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();
+}