| ... | @@ -1249,6 +1249,20 @@ test "store to comptime field" { | ... | @@ -1249,6 +1249,20 @@ test "store to comptime field" { |
| 1249 | } | 1249 | } |
| 1250 | } | 1250 | } |
| 1251 | | 1251 | |
| | 1252 | test "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 | |
| 1252 | test "under-aligned struct field" { | 1266 | test "under-aligned struct field" { |
| 1253 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO | 1267 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 1254 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO | 1268 | 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" { | ... | @@ -1696,19 +1710,41 @@ test "comptimeness of optional and error union payload is analyzed properly" { |
| 1696 | try std.testing.expectEqual(3, x); | 1710 | try std.testing.expectEqual(3, x); |
| 1697 | } | 1711 | } |
| 1698 | | 1712 | |
| | 1713 | test "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 | |
| | 1724 | test "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 | |
| 1699 | test "initializer takes a pointer to a variable inside its struct" { | 1735 | test "initializer takes a pointer to a variable inside its struct" { |
| 1700 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; | 1736 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 1701 | | 1737 | |
| 1702 | const namespace = struct { | 1738 | const namespace = struct { |
| 1703 | const S = struct { | 1739 | const S = struct { |
| 1704 | x: *u32 = &S.int, | 1740 | s: *S = &S.instance, |
| 1705 | var int: u32 = undefined; | 1741 | var instance: S = undefined; |
| 1706 | }; | 1742 | }; |
| 1707 | | 1743 | |
| 1708 | fn doTheTest() !void { | 1744 | fn doTheTest() !void { |
| 1709 | var foo: S = .{}; | 1745 | var foo: S = .{}; |
| 1710 | _ = &foo; | 1746 | _ = &foo; |
| 1711 | try expectEqual(&S.int, foo.x); | 1747 | try expectEqual(&S.instance, foo.s); |
| 1712 | } | 1748 | } |
| 1713 | }; | 1749 | }; |
| 1714 | | 1750 | |
| ... | @@ -1739,6 +1775,22 @@ test "circular dependency through pointer field of a struct" { | ... | @@ -1739,6 +1775,22 @@ test "circular dependency through pointer field of a struct" { |
| 1739 | try expect(outer.middle.inner == null); | 1775 | try expect(outer.middle.inner == null); |
| 1740 | } | 1776 | } |
| 1741 | | 1777 | |
| | 1778 | test "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 | |
| 1742 | test "tuple with comptime-only field" { | 1794 | test "tuple with comptime-only field" { |
| 1743 | const S = struct { | 1795 | const S = struct { |
| 1744 | fn getTuple() struct { comptime_int } { | 1796 | fn getTuple() struct { comptime_int } { |