authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-02-27 11:23:05+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-03-10 10:26:13+00:00
logbb78871aa44594a1b4782792942664e532491d5a
tree276fd48a89a6657aae11782ff7a2afe6739656c4
parent51c23f7ba4f09f26f53b21299a456f2ed9d7839e
signaturelock-open Commit is signed but in an unrecognized format.

behavior: re-introduce some previously-removed tests

Now that struct default value resolution is separate from struct layout resolution, a handful of old behavior tests are now once again valid. This partially reverts the commit titled "behavior: update for changes to struct field default value resolution".

2 files changed, 104 insertions(+), 3 deletions(-)

test/behavior/struct.zig+55-3
......@@ -1249,6 +1249,20 @@ test "store to comptime field" {
12491249 }
12501250}
12511251
1252test "struct field init value is size of the struct" {
1253 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
1254
1255 const namespace = struct {
1256 const S = extern struct {
1257 size: u8 = @sizeOf(S),
1258 blah: u16,
1259 };
1260 };
1261 var s: namespace.S = .{ .blah = 1234 };
1262 _ = &s;
1263 try expect(s.size == 4);
1264}
1265
12521266test "under-aligned struct field" {
12531267 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
12541268 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
......@@ -1696,19 +1710,41 @@ test "comptimeness of optional and error union payload is analyzed properly" {
16961710 try std.testing.expectEqual(3, x);
16971711}
16981712
1713test "initializer uses own alignment" {
1714 const S = struct {
1715 x: u32 = @alignOf(@This()) + 1,
1716 };
1717
1718 var s: S = .{};
1719 _ = &s;
1720 try expectEqual(4, @alignOf(S));
1721 try expectEqual(@as(usize, 5), s.x);
1722}
1723
1724test "initializer uses own size" {
1725 const S = struct {
1726 x: u32 = @sizeOf(@This()) + 1,
1727 };
1728
1729 var s: S = .{};
1730 _ = &s;
1731 try expectEqual(4, @sizeOf(S));
1732 try expectEqual(@as(usize, 5), s.x);
1733}
1734
16991735test "initializer takes a pointer to a variable inside its struct" {
17001736 if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
17011737
17021738 const namespace = struct {
17031739 const S = struct {
1704 x: *u32 = &S.int,
1705 var int: u32 = undefined;
1740 s: *S = &S.instance,
1741 var instance: S = undefined;
17061742 };
17071743
17081744 fn doTheTest() !void {
17091745 var foo: S = .{};
17101746 _ = &foo;
1711 try expectEqual(&S.int, foo.x);
1747 try expectEqual(&S.instance, foo.s);
17121748 }
17131749 };
17141750
......@@ -1739,6 +1775,22 @@ test "circular dependency through pointer field of a struct" {
17391775 try expect(outer.middle.inner == null);
17401776}
17411777
1778test "field calls do not force struct field init resolution" {
1779 const S = struct {
1780 x: u32 = blk: {
1781 _ = @TypeOf(make().dummyFn()); // runtime field call - S not fully resolved - dummyFn call should not force field init resolution
1782 break :blk 123;
1783 },
1784 dummyFn: *const fn () void = undefined,
1785 fn make() @This() {
1786 return .{};
1787 }
1788 };
1789 var s: S = .{};
1790 _ = &s;
1791 try expect(s.x == 123);
1792}
1793
17421794test "tuple with comptime-only field" {
17431795 const S = struct {
17441796 fn getTuple() struct { comptime_int } {
test/behavior/union.zig+49
......@@ -1796,6 +1796,55 @@ test "reinterpret packed union inside packed struct" {
17961796 try S.doTheTest();
17971797}
17981798
1799test "inner struct initializer uses union layout" {
1800 const namespace = struct {
1801 const U = union {
1802 a: struct {
1803 x: u32 = @alignOf(U) + 1,
1804 },
1805 b: struct {
1806 y: u16 = @sizeOf(U) + 2,
1807 },
1808 };
1809 };
1810
1811 {
1812 const u: namespace.U = .{ .a = .{} };
1813 try expectEqual(4, @alignOf(namespace.U));
1814 try expectEqual(@as(usize, 5), u.a.x);
1815 }
1816
1817 {
1818 const u: namespace.U = .{ .b = .{} };
1819 try expectEqual(@as(usize, @sizeOf(namespace.U) + 2), u.b.y);
1820 }
1821}
1822
1823test "inner struct initializer uses packed union layout" {
1824 const namespace = struct {
1825 const U = packed union {
1826 a: packed struct {
1827 x: u32 = @alignOf(U) + 1,
1828 },
1829 b: packed struct(u32) {
1830 y: u16 = @sizeOf(U) + 2,
1831 padding: u16 = 0,
1832 },
1833 };
1834 };
1835
1836 {
1837 const u: namespace.U = .{ .a = .{} };
1838 try expectEqual(4, @alignOf(namespace.U));
1839 try expectEqual(@as(usize, 5), u.a.x);
1840 }
1841
1842 {
1843 const u: namespace.U = .{ .b = .{} };
1844 try expectEqual(@as(usize, @sizeOf(namespace.U) + 2), u.b.y);
1845 }
1846}
1847
17991848test "extern union initialized via reintepreted struct field initializer" {
18001849 if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
18011850