Commit 6bba5a39e4

Stevie Hryciw <codroid@gmail.com>
2023-08-07 03:09:41
Add behavior test for ptrCast on function pointer
Closes #6280. In the C++ implementation this triggered a compiler assertion.
1 parent 68f8496
Changed files (1)
test
behavior
test/behavior/cast.zig
@@ -1154,6 +1154,29 @@ fn foobar(func: PFN_void) !void {
     try std.testing.expect(@intFromPtr(func) == hardcoded_fn_addr);
 }
 
+test "cast function with an opaque parameter" {
+    const Container = struct {
+        const Ctx = opaque {};
+        ctx: *Ctx,
+        func: *const fn (*Ctx) void,
+    };
+    const Foo = struct {
+        x: i32,
+        y: i32,
+        fn funcImpl(self: *@This()) void {
+            self.x += 1;
+            self.y += 1;
+        }
+    };
+    var foo = Foo{ .x = 100, .y = 200 };
+    var c = Container{
+        .ctx = @ptrCast(&foo),
+        .func = @ptrCast(&Foo.funcImpl),
+    };
+    c.func(c.ctx);
+    try std.testing.expectEqual(foo, .{ .x = 101, .y = 201 });
+}
+
 test "implicit ptr to *anyopaque" {
     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO