authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-08-06 21:57:22-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-08-06 21:57:22-07:00
logac95cfe4496a5504b42bf37b02f34eff390fe956
treeaae962f357e65bbba93146073f17adf2ec50c463
parent49244dc0ca68d4b1282b7b26b52e750a4892fce2
parent2ad6ca581da28a547b33d93f1a9855974a8b15c1
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #16721 from hryx/stage1-coverage


3 files changed, 46 insertions(+), 0 deletions(-)

test/behavior/bugs/6305.zig created+10
......@@ -0,0 +1,10 @@
1const ListNode = struct {
2 next: ?*const @This() = null,
3};
4
5test "copy array of self-referential struct" {
6 comptime var nodes = [_]ListNode{ .{}, .{} };
7 nodes[0].next = &nodes[1];
8 const copy = nodes;
9 _ = copy;
10}
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
test/cases/compile_errors/comptime_dereference_slice_of_struct.zig created+13
......@@ -0,0 +1,13 @@
1const MyStruct = struct { x: bool = false };
2
3comptime {
4 const x = &[_]MyStruct{ .{}, .{} };
5 const y = x[0..1] ++ &[_]MyStruct{};
6 _ = y;
7}
8
9// error
10// backend=stage2
11// target=native
12//
13// :5:16: error: comptime dereference requires '[1]tmp.MyStruct' to have a well-defined layout, but it does not.