authorgravatar for codroid@gmail.comStevie Hryciw <codroid@gmail.com> 2023-08-06 18:09:41-07:00
committergravatar for codroid@gmail.comStevie Hryciw <codroid@gmail.com> 2023-08-06 18:09:41-07:00
log6bba5a39e4a4eb2262052a6d218cf71795a9d704
tree024d9a9f1b4e2cf827443e33f60e84338e02dd60
parent68f84964b3d80e5b976810208a14c31268a181e1

Add behavior test for ptrCast on function pointer

Closes #6280. In the C++ implementation this triggered a compiler assertion.

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

test/behavior/cast.zig+23
......@@ -1154,6 +1154,29 @@ fn foobar(func: PFN_void) !void {
11541154 try std.testing.expect(@intFromPtr(func) == hardcoded_fn_addr);
11551155}
11561156
1157test "cast function with an opaque parameter" {
1158 const Container = struct {
1159 const Ctx = opaque {};
1160 ctx: *Ctx,
1161 func: *const fn (*Ctx) void,
1162 };
1163 const Foo = struct {
1164 x: i32,
1165 y: i32,
1166 fn funcImpl(self: *@This()) void {
1167 self.x += 1;
1168 self.y += 1;
1169 }
1170 };
1171 var foo = Foo{ .x = 100, .y = 200 };
1172 var c = Container{
1173 .ctx = @ptrCast(&foo),
1174 .func = @ptrCast(&Foo.funcImpl),
1175 };
1176 c.func(c.ctx);
1177 try std.testing.expectEqual(foo, .{ .x = 101, .y = 201 });
1178}
1179
11571180test "implicit ptr to *anyopaque" {
11581181 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
11591182 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO