From 6bba5a39e4a4eb2262052a6d218cf71795a9d704 Mon Sep 17 00:00:00 2001 From: Stevie Hryciw Date: Sun, 6 Aug 2023 18:09:41 -0700 Subject: [PATCH 1/3] Add behavior test for ptrCast on function pointer Closes #6280. In the C++ implementation this triggered a compiler assertion. --- test/behavior/cast.zig | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig index aa1a24bc9b..742f6b5af5 100644 --- a/test/behavior/cast.zig +++ b/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 From 16dcefaca5956af747c29d6ab1915cc955f81af9 Mon Sep 17 00:00:00 2001 From: Stevie Hryciw Date: Sun, 6 Aug 2023 18:11:49 -0700 Subject: [PATCH 2/3] Add compile error test for comptime slice-of-struct copy Closes #6305. In the C++ implementation this hit a compiler assertion. It is handled properly in the self-hosted version. --- .../comptime_dereference_slice_of_struct.zig | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 test/cases/compile_errors/comptime_dereference_slice_of_struct.zig diff --git a/test/cases/compile_errors/comptime_dereference_slice_of_struct.zig b/test/cases/compile_errors/comptime_dereference_slice_of_struct.zig new file mode 100644 index 0000000000..8d3619d906 --- /dev/null +++ b/test/cases/compile_errors/comptime_dereference_slice_of_struct.zig @@ -0,0 +1,13 @@ +const MyStruct = struct { x: bool = false }; + +comptime { + const x = &[_]MyStruct{ .{}, .{} }; + const y = x[0..1] ++ &[_]MyStruct{}; + _ = y; +} + +// error +// backend=stage2 +// target=native +// +// :5:16: error: comptime dereference requires '[1]tmp.MyStruct' to have a well-defined layout, but it does not. From 2ad6ca581da28a547b33d93f1a9855974a8b15c1 Mon Sep 17 00:00:00 2001 From: Stevie Hryciw Date: Sun, 6 Aug 2023 18:23:28 -0700 Subject: [PATCH 3/3] Add behavior test for copying self-referential struct Closes #6312. In the C++ implementation this caused a stack overflow from infinite recursion during analysis. --- test/behavior/bugs/6305.zig | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 test/behavior/bugs/6305.zig diff --git a/test/behavior/bugs/6305.zig b/test/behavior/bugs/6305.zig new file mode 100644 index 0000000000..6912944ad1 --- /dev/null +++ b/test/behavior/bugs/6305.zig @@ -0,0 +1,10 @@ +const ListNode = struct { + next: ?*const @This() = null, +}; + +test "copy array of self-referential struct" { + comptime var nodes = [_]ListNode{ .{}, .{} }; + nodes[0].next = &nodes[1]; + const copy = nodes; + _ = copy; +}