From 21b42af5aa6bc25dd99d2e425aa3df6bac80476e Mon Sep 17 00:00:00 2001 From: Matthew Lugg Date: Sun, 8 Feb 2026 14:58:32 +0000 Subject: [PATCH] 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. --- test/behavior/sizeof_and_typeof.zig | 25 ------------------- test/behavior/type.zig | 4 +-- .../cases/compile_errors/alignOf_bad_type.zig | 10 ++++++-- test/cases/compile_errors/sizeOf_bad_type.zig | 22 ++++++++++++++-- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/test/behavior/sizeof_and_typeof.zig b/test/behavior/sizeof_and_typeof.zig index a6087787b5..2fb89bc484 100644 --- a/test/behavior/sizeof_and_typeof.zig +++ b/test/behavior/sizeof_and_typeof.zig @@ -11,13 +11,6 @@ test "@sizeOf and @TypeOf" { const x: u16 = 13; const z: @TypeOf(x) = 19; -test "@sizeOf on compile-time types" { - try expect(@sizeOf(comptime_int) == 0); - try expect(@sizeOf(comptime_float) == 0); - try expect(@sizeOf(@TypeOf(.hi)) == 0); - try expect(@sizeOf(@TypeOf(type)) == 0); -} - test "@TypeOf() with multiple arguments" { { var var_1: u32 = undefined; @@ -265,10 +258,6 @@ test "lazy size cast to float" { } } -test "bitSizeOf comptime_int" { - try expect(@bitSizeOf(comptime_int) == 0); -} - test "runtime instructions inside typeof in comptime only scope" { if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; 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" { try std.testing.expect(t.next == null); } -test "@sizeOf reified union zero-size payload fields" { - comptime { - try std.testing.expect(0 == @sizeOf(@Union(.auto, null, &.{}, &.{}, &.{}))); - try std.testing.expect(0 == @sizeOf(@Union(.auto, null, &.{"a"}, &.{void}, &.{.{}}))); - if (builtin.mode == .Debug or builtin.mode == .ReleaseSafe) { - try std.testing.expect(1 == @sizeOf(@Union(.auto, null, &.{ "a", "b" }, &.{ void, void }, &.{ .{}, .{} }))); - try std.testing.expect(1 == @sizeOf(@Union(.auto, null, &.{ "a", "b", "c" }, &.{ void, void, void }, &.{ .{}, .{}, .{} }))); - } else { - try std.testing.expect(0 == @sizeOf(@Union(.auto, null, &.{ "a", "b" }, &.{ void, void }, &.{ .{}, .{} }))); - try std.testing.expect(0 == @sizeOf(@Union(.auto, null, &.{ "a", "b", "c" }, &.{ void, void, void }, &.{ .{}, .{}, .{} }))); - } - } -} - const FILE = extern struct { dummy_field: u8, }; diff --git a/test/behavior/type.zig b/test/behavior/type.zig index ef27522a63..88358db182 100644 --- a/test/behavior/type.zig +++ b/test/behavior/type.zig @@ -278,13 +278,13 @@ test "Type.Union from regular enum" { test "Type.Union from empty regular enum" { const E = enum {}; const U = @Union(.auto, E, &.{}, &.{}, &.{}); - try testing.expectEqual(@sizeOf(U), 0); + try testing.expectEqual(@typeInfo(U).@"union".fields.len, 0); } test "Type.Union from empty Type.Enum" { const E = @Enum(u0, .exhaustive, &.{}, &.{}); const U = @Union(.auto, E, &.{}, &.{}, &.{}); - try testing.expectEqual(@sizeOf(U), 0); + try testing.expectEqual(@typeInfo(U).@"union".fields.len, 0); } test "Type.Fn" { diff --git a/test/cases/compile_errors/alignOf_bad_type.zig b/test/cases/compile_errors/alignOf_bad_type.zig index 253adceb05..93dcfa3b94 100644 --- a/test/cases/compile_errors/alignOf_bad_type.zig +++ b/test/cases/compile_errors/alignOf_bad_type.zig @@ -1,7 +1,13 @@ -export fn entry() usize { +export fn entry0() usize { return @alignOf(noreturn); } +const S = struct { a: u32, b: noreturn }; +export fn entry1() usize { + return @alignOf(S); +} // error // -// :2:21: error: no align available for type 'noreturn' +// :2:21: error: no align available for uninstantiable type 'noreturn' +// :6:21: error: no align available for uninstantiable type 'alignOf_bad_type.S' +// :4:11: note: struct declared here diff --git a/test/cases/compile_errors/sizeOf_bad_type.zig b/test/cases/compile_errors/sizeOf_bad_type.zig index 93dec1f88e..c81ad7450f 100644 --- a/test/cases/compile_errors/sizeOf_bad_type.zig +++ b/test/cases/compile_errors/sizeOf_bad_type.zig @@ -1,7 +1,25 @@ -export fn entry() usize { +export fn entry0() usize { return @sizeOf(@TypeOf(null)); } +export fn entry1() usize { + return @sizeOf(comptime_int); +} +export fn entry2() usize { + return @sizeOf(noreturn); +} +const S3 = struct { a: u32, b: comptime_int }; +export fn entry3() usize { + return @sizeOf(S3); +} +const S4 = struct { a: u32, b: noreturn }; +export fn entry4() usize { + return @sizeOf(S4); +} // error // -// :2:20: error: no size available for type '@TypeOf(null)' +// :2:20: error: no size available for comptime-only type '@TypeOf(null)' +// :5:20: error: no size available for comptime-only type 'comptime_int' +// :8:20: error: no size available for uninstantiable type 'noreturn' +// :12:20: error: no size available for comptime-only type 'tmp.S3' +// :16:20: error: no size available for uninstantiable type 'tmp.S4'