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.
This commit is contained in:
Matthew Lugg 2026-02-08 14:58:32 +00:00
parent fdb1a52948
commit e5bc5b29a5
No known key found for this signature in database
GPG key ID: 3F5B7DCCBF4AF02E
4 changed files with 30 additions and 31 deletions

View file

@ -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,
};

View file

@ -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" {

View file

@ -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

View file

@ -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'