From 0e3feebb042a538405153e4bc75a3bb73ee2e63c Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Sun, 18 Dec 2022 22:09:13 -0500 Subject: codegen: fix taking the address of a zero-bit field in a zero-bit struct Normally when we want a pointer to the end of a struct we just add 1 to the struct pointer. However, when it is a zero-bit struct, the pointer type being used during lowering is often a dummy pointer type that actually points to a non-zero-bit type, so we actually want to add 0 instead, since a zero-bit struct begins and ends at the same address. --- test/behavior/struct.zig | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'test/behavior') diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig index 0984f7d1e4..b6567b14c3 100644 --- a/test/behavior/struct.zig +++ b/test/behavior/struct.zig @@ -1365,7 +1365,7 @@ test "fieldParentPtr of a zero-bit field" { if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO const S = struct { - fn testOneType(comptime A: type) !void { + fn testStruct(comptime A: type) !void { { const a = A{ .u = 0 }; const b_ptr = &a.b; @@ -1379,9 +1379,29 @@ test "fieldParentPtr of a zero-bit field" { try std.testing.expectEqual(&a, a_ptr); } } + fn testNestedStruct(comptime A: type) !void { + { + const a = A{ .u = 0 }; + const c_ptr = &a.b.c; + const b_ptr = @fieldParentPtr(@TypeOf(a.b), "c", c_ptr); + try std.testing.expectEqual(&a.b, b_ptr); + const a_ptr = @fieldParentPtr(A, "b", b_ptr); + try std.testing.expectEqual(&a, a_ptr); + } + { + var a = A{ .u = 0 }; + const c_ptr = &a.b.c; + const b_ptr = @fieldParentPtr(@TypeOf(a.b), "c", c_ptr); + try std.testing.expectEqual(&a.b, b_ptr); + const a_ptr = @fieldParentPtr(A, "b", b_ptr); + try std.testing.expectEqual(&a, a_ptr); + } + } fn doTheTest() !void { - try testOneType(struct { b: void = {}, u: u8 }); - try testOneType(struct { u: u8, b: void = {} }); + try testStruct(struct { b: void = {}, u: u8 }); + try testStruct(struct { u: u8, b: void = {} }); + try testNestedStruct(struct { b: struct { c: void = {} } = .{}, u: u8 }); + try testNestedStruct(struct { u: u8, b: struct { c: void = {} } = .{} }); } }; try S.doTheTest(); -- cgit v1.2.3