From 12ddd5a698577e22a0bb5ed788ea4b9729a45b5d Mon Sep 17 00:00:00 2001 From: Matthew Lugg Date: Sun, 8 Feb 2026 15:58:51 +0000 Subject: [PATCH] behavior: update for changes to struct field default value resolution This is separate from the previous commit so that these changes can be easily reverted in the event that we decide to allow more granularity in default value resolution in exchange for increased language complexity. --- test/behavior/eval.zig | 114 --------------------------------------- test/behavior/struct.zig | 58 ++------------------ test/behavior/union.zig | 49 ----------------- 3 files changed, 3 insertions(+), 218 deletions(-) diff --git a/test/behavior/eval.zig b/test/behavior/eval.zig index 8f45405e94838c9c7768616a9f1f1dd24c2baa7f..9e3cd732f0e3b41152fbbb51732283bbe9c07192 100644 --- a/test/behavior/eval.zig +++ b/test/behavior/eval.zig @@ -1081,120 +1081,6 @@ test "comptime break operand passing through runtime switch converted to runtime try comptime S.doTheTest('b'); } -test "no dependency loop for alignment of self struct" { - if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; - - const S = struct { - fn doTheTest() !void { - var a: namespace.A = undefined; - a.d = .{ .g = &buf }; - a.d.g[3] = 42; - a.d.g[3] += 1; - try expect(a.d.g[3] == 43); - } - - var buf: [10]u8 align(@alignOf([*]u8)) = undefined; - - const namespace = struct { - const B = struct { a: A }; - const A = C(B); - }; - - pub fn C(comptime B: type) type { - return struct { - d: D(F) = .{}, - - const F = struct { b: B }; - }; - } - - pub fn D(comptime F: type) type { - return struct { - g: [*]align(@alignOf(F)) u8 = undefined, - }; - } - }; - try S.doTheTest(); -} - -test "no dependency loop for alignment of self bare union" { - if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; - - const S = struct { - fn doTheTest() !void { - var a: namespace.A = undefined; - a.d = .{ .g = &buf }; - a.d.g[3] = 42; - a.d.g[3] += 1; - try expect(a.d.g[3] == 43); - } - - var buf: [10]u8 align(@alignOf([*]u8)) = undefined; - - const namespace = struct { - const B = union { a: A, b: void }; - const A = C(B); - }; - - pub fn C(comptime B: type) type { - return struct { - d: D(F) = .{}, - - const F = struct { b: B }; - }; - } - - pub fn D(comptime F: type) type { - return struct { - g: [*]align(@alignOf(F)) u8 = undefined, - }; - } - }; - try S.doTheTest(); -} - -test "no dependency loop for alignment of self tagged union" { - if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; - - const S = struct { - fn doTheTest() !void { - var a: namespace.A = undefined; - a.d = .{ .g = &buf }; - a.d.g[3] = 42; - a.d.g[3] += 1; - try expect(a.d.g[3] == 43); - } - - var buf: [10]u8 align(@alignOf([*]u8)) = undefined; - - const namespace = struct { - const B = union(enum) { a: A, b: void }; - const A = C(B); - }; - - pub fn C(comptime B: type) type { - return struct { - d: D(F) = .{}, - - const F = struct { b: B }; - }; - } - - pub fn D(comptime F: type) type { - return struct { - g: [*]align(@alignOf(F)) u8 = undefined, - }; - } - }; - try S.doTheTest(); -} - test "equality of pointers to comptime const" { const a: i32 = undefined; comptime assert(&a == &a); diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig index e6a355864629600f6294492ee11e4cf7c73f5674..37dc873d60385608367b1841362aaadb21254e5c 100644 --- a/test/behavior/struct.zig +++ b/test/behavior/struct.zig @@ -1249,20 +1249,6 @@ test "store to comptime field" { } } -test "struct field init value is size of the struct" { - if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - - const namespace = struct { - const S = extern struct { - size: u8 = @sizeOf(S), - blah: u16, - }; - }; - var s: namespace.S = .{ .blah = 1234 }; - _ = &s; - try expect(s.size == 4); -} - test "under-aligned struct field" { if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO @@ -1710,41 +1696,19 @@ test "comptimeness of optional and error union payload is analyzed properly" { try std.testing.expectEqual(3, x); } -test "initializer uses own alignment" { - const S = struct { - x: u32 = @alignOf(@This()) + 1, - }; - - var s: S = .{}; - _ = &s; - try expectEqual(4, @alignOf(S)); - try expectEqual(@as(usize, 5), s.x); -} - -test "initializer uses own size" { - const S = struct { - x: u32 = @sizeOf(@This()) + 1, - }; - - var s: S = .{}; - _ = &s; - try expectEqual(4, @sizeOf(S)); - try expectEqual(@as(usize, 5), s.x); -} - test "initializer takes a pointer to a variable inside its struct" { if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; const namespace = struct { const S = struct { - s: *S = &S.instance, - var instance: S = undefined; + x: *u32 = &S.int, + var int: u32 = undefined; }; fn doTheTest() !void { var foo: S = .{}; _ = &foo; - try expectEqual(&S.instance, foo.s); + try expectEqual(&S.int, foo.x); } }; @@ -1775,22 +1739,6 @@ test "circular dependency through pointer field of a struct" { try expect(outer.middle.inner == null); } -test "field calls do not force struct field init resolution" { - const S = struct { - x: u32 = blk: { - _ = @TypeOf(make().dummyFn()); // runtime field call - S not fully resolved - dummyFn call should not force field init resolution - break :blk 123; - }, - dummyFn: *const fn () void = undefined, - fn make() @This() { - return .{}; - } - }; - var s: S = .{}; - _ = &s; - try expect(s.x == 123); -} - test "tuple with comptime-only field" { const S = struct { fn getTuple() struct { comptime_int } { diff --git a/test/behavior/union.zig b/test/behavior/union.zig index 9badc9dabd11bd6709c95ae32ba755f6cc3b9bd2..b081d9b33a06e41df101c7f55f2d716821be30fe 100644 --- a/test/behavior/union.zig +++ b/test/behavior/union.zig @@ -1796,55 +1796,6 @@ test "reinterpret packed union inside packed struct" { try S.doTheTest(); } -test "inner struct initializer uses union layout" { - const namespace = struct { - const U = union { - a: struct { - x: u32 = @alignOf(U) + 1, - }, - b: struct { - y: u16 = @sizeOf(U) + 2, - }, - }; - }; - - { - const u: namespace.U = .{ .a = .{} }; - try expectEqual(4, @alignOf(namespace.U)); - try expectEqual(@as(usize, 5), u.a.x); - } - - { - const u: namespace.U = .{ .b = .{} }; - try expectEqual(@as(usize, @sizeOf(namespace.U) + 2), u.b.y); - } -} - -test "inner struct initializer uses packed union layout" { - const namespace = struct { - const U = packed union { - a: packed struct { - x: u32 = @alignOf(U) + 1, - }, - b: packed struct(u32) { - y: u16 = @sizeOf(U) + 2, - padding: u16 = 0, - }, - }; - }; - - { - const u: namespace.U = .{ .a = .{} }; - try expectEqual(4, @alignOf(namespace.U)); - try expectEqual(@as(usize, 5), u.a.x); - } - - { - const u: namespace.U = .{ .b = .{} }; - try expectEqual(@as(usize, @sizeOf(namespace.U) + 2), u.b.y); - } -} - test "extern union initialized via reintepreted struct field initializer" { if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; -- 2.54.0