authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-02-08 14:58:32+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-03-10 10:26:10+00:00
log21b42af5aa6bc25dd99d2e425aa3df6bac80476e
treeeffa8b8b8c3a2d8723444cfdcb0e3249ee0cc0de
parenta226008e32af8700d1aa05bcaef50cb3cf1205d0
signaturelock-open Commit is signed but in an unrecognized format.

tests: update for accepted language change

`@sizeOf` and `@bitSizeOf` are now more restricted: they are not allowed on comptime-only or NPV (uninstantiable) types. This is because there is no correct way to actually use the returned ABI size (e.g. you cannot copy a comptime-only type by copying all of its runtime bits), so having a non-zero return value had no benefit and was simply confusing.

4 files changed, 30 insertions(+), 31 deletions(-)

test/behavior/sizeof_and_typeof.zig-25
...@@ -11,13 +11,6 @@ test "@sizeOf and @TypeOf" {...@@ -11,13 +11,6 @@ test "@sizeOf and @TypeOf" {
11const x: u16 = 13;11const x: u16 = 13;
12const z: @TypeOf(x) = 19;12const z: @TypeOf(x) = 19;
1313
14test "@sizeOf on compile-time types" {
15 try expect(@sizeOf(comptime_int) == 0);
16 try expect(@sizeOf(comptime_float) == 0);
17 try expect(@sizeOf(@TypeOf(.hi)) == 0);
18 try expect(@sizeOf(@TypeOf(type)) == 0);
19}
20
21test "@TypeOf() with multiple arguments" {14test "@TypeOf() with multiple arguments" {
22 {15 {
23 var var_1: u32 = undefined;16 var var_1: u32 = undefined;
...@@ -265,10 +258,6 @@ test "lazy size cast to float" {...@@ -265,10 +258,6 @@ test "lazy size cast to float" {
265 }258 }
266}259}
267260
268test "bitSizeOf comptime_int" {
269 try expect(@bitSizeOf(comptime_int) == 0);
270}
271
272test "runtime instructions inside typeof in comptime only scope" {261test "runtime instructions inside typeof in comptime only scope" {
273 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;262 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
274 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO263 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
...@@ -336,20 +325,6 @@ test "peer type resolution with @TypeOf doesn't trigger dependency loop check" {...@@ -336,20 +325,6 @@ test "peer type resolution with @TypeOf doesn't trigger dependency loop check" {
336 try std.testing.expect(t.next == null);325 try std.testing.expect(t.next == null);
337}326}
338327
339test "@sizeOf reified union zero-size payload fields" {
340 comptime {
341 try std.testing.expect(0 == @sizeOf(@Union(.auto, null, &.{}, &.{}, &.{})));
342 try std.testing.expect(0 == @sizeOf(@Union(.auto, null, &.{"a"}, &.{void}, &.{.{}})));
343 if (builtin.mode == .Debug or builtin.mode == .ReleaseSafe) {
344 try std.testing.expect(1 == @sizeOf(@Union(.auto, null, &.{ "a", "b" }, &.{ void, void }, &.{ .{}, .{} })));
345 try std.testing.expect(1 == @sizeOf(@Union(.auto, null, &.{ "a", "b", "c" }, &.{ void, void, void }, &.{ .{}, .{}, .{} })));
346 } else {
347 try std.testing.expect(0 == @sizeOf(@Union(.auto, null, &.{ "a", "b" }, &.{ void, void }, &.{ .{}, .{} })));
348 try std.testing.expect(0 == @sizeOf(@Union(.auto, null, &.{ "a", "b", "c" }, &.{ void, void, void }, &.{ .{}, .{}, .{} })));
349 }
350 }
351}
352
353const FILE = extern struct {328const FILE = extern struct {
354 dummy_field: u8,329 dummy_field: u8,
355};330};
test/behavior/type.zig+2-2
...@@ -278,13 +278,13 @@ test "Type.Union from regular enum" {...@@ -278,13 +278,13 @@ test "Type.Union from regular enum" {
278test "Type.Union from empty regular enum" {278test "Type.Union from empty regular enum" {
279 const E = enum {};279 const E = enum {};
280 const U = @Union(.auto, E, &.{}, &.{}, &.{});280 const U = @Union(.auto, E, &.{}, &.{}, &.{});
281 try testing.expectEqual(@sizeOf(U), 0);281 try testing.expectEqual(@typeInfo(U).@"union".fields.len, 0);
282}282}
283283
284test "Type.Union from empty Type.Enum" {284test "Type.Union from empty Type.Enum" {
285 const E = @Enum(u0, .exhaustive, &.{}, &.{});285 const E = @Enum(u0, .exhaustive, &.{}, &.{});
286 const U = @Union(.auto, E, &.{}, &.{}, &.{});286 const U = @Union(.auto, E, &.{}, &.{}, &.{});
287 try testing.expectEqual(@sizeOf(U), 0);287 try testing.expectEqual(@typeInfo(U).@"union".fields.len, 0);
288}288}
289289
290test "Type.Fn" {290test "Type.Fn" {
test/cases/compile_errors/alignOf_bad_type.zig+8-2
...@@ -1,7 +1,13 @@...@@ -1,7 +1,13 @@
1export fn entry() usize {1export fn entry0() usize {
2 return @alignOf(noreturn);2 return @alignOf(noreturn);
3}3}
4const S = struct { a: u32, b: noreturn };
5export fn entry1() usize {
6 return @alignOf(S);
7}
48
5// error9// error
6//10//
7// :2:21: error: no align available for type 'noreturn'11// :2:21: error: no align available for uninstantiable type 'noreturn'
12// :6:21: error: no align available for uninstantiable type 'alignOf_bad_type.S'
13// :4:11: note: struct declared here
test/cases/compile_errors/sizeOf_bad_type.zig+20-2
...@@ -1,7 +1,25 @@...@@ -1,7 +1,25 @@
1export fn entry() usize {1export fn entry0() usize {
2 return @sizeOf(@TypeOf(null));2 return @sizeOf(@TypeOf(null));
3}3}
4export fn entry1() usize {
5 return @sizeOf(comptime_int);
6}
7export fn entry2() usize {
8 return @sizeOf(noreturn);
9}
10const S3 = struct { a: u32, b: comptime_int };
11export fn entry3() usize {
12 return @sizeOf(S3);
13}
14const S4 = struct { a: u32, b: noreturn };
15export fn entry4() usize {
16 return @sizeOf(S4);
17}
418
5// error19// error
6//20//
7// :2:20: error: no size available for type '@TypeOf(null)'21// :2:20: error: no size available for comptime-only type '@TypeOf(null)'
22// :5:20: error: no size available for comptime-only type 'comptime_int'
23// :8:20: error: no size available for uninstantiable type 'noreturn'
24// :12:20: error: no size available for comptime-only type 'tmp.S3'
25// :16:20: error: no size available for uninstantiable type 'tmp.S4'