Add behavior test for ptrCast on function pointer

Closes #6280. In the C++ implementation this triggered a
compiler assertion.
This commit is contained in:
Stevie Hryciw 2023-08-06 18:09:41 -07:00
parent 68f84964b3
commit 6bba5a39e4

View File

@ -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