| author | |
| committer | |
| log | 21b42af5aa6bc25dd99d2e425aa3df6bac80476e |
| tree | effa8b8b8c3a2d8723444cfdcb0e3249ee0cc0de |
| parent | a226008e32af8700d1aa05bcaef50cb3cf1205d0 |
| signature |
`@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" { |
| 11 | const x: u16 = 13; | 11 | const x: u16 = 13; |
| 12 | const z: @TypeOf(x) = 19; | 12 | const z: @TypeOf(x) = 19; |
| 13 | 13 | ||
| 14 | test "@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 | |||
| 21 | test "@TypeOf() with multiple arguments" { | 14 | test "@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 | } |
| 267 | 260 | ||
| 268 | test "bitSizeOf comptime_int" { | ||
| 269 | try expect(@bitSizeOf(comptime_int) == 0); | ||
| 270 | } | ||
| 271 | |||
| 272 | test "runtime instructions inside typeof in comptime only scope" { | 261 | test "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; // TODO | 263 | 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 | } |
| 338 | 327 | ||
| 339 | test "@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 | |||
| 353 | const FILE = extern struct { | 328 | const 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" { |
| 278 | test "Type.Union from empty regular enum" { | 278 | test "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 | } |
| 283 | 283 | ||
| 284 | test "Type.Union from empty Type.Enum" { | 284 | test "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 | } |
| 289 | 289 | ||
| 290 | test "Type.Fn" { | 290 | test "Type.Fn" { |
test/cases/compile_errors/alignOf_bad_type.zig+8-2| ... | @@ -1,7 +1,13 @@ | ... | @@ -1,7 +1,13 @@ |
| 1 | export fn entry() usize { | 1 | export fn entry0() usize { |
| 2 | return @alignOf(noreturn); | 2 | return @alignOf(noreturn); |
| 3 | } | 3 | } |
| 4 | const S = struct { a: u32, b: noreturn }; | ||
| 5 | export fn entry1() usize { | ||
| 6 | return @alignOf(S); | ||
| 7 | } | ||
| 4 | 8 | ||
| 5 | // error | 9 | // 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 @@ |
| 1 | export fn entry() usize { | 1 | export fn entry0() usize { |
| 2 | return @sizeOf(@TypeOf(null)); | 2 | return @sizeOf(@TypeOf(null)); |
| 3 | } | 3 | } |
| 4 | export fn entry1() usize { | ||
| 5 | return @sizeOf(comptime_int); | ||
| 6 | } | ||
| 7 | export fn entry2() usize { | ||
| 8 | return @sizeOf(noreturn); | ||
| 9 | } | ||
| 10 | const S3 = struct { a: u32, b: comptime_int }; | ||
| 11 | export fn entry3() usize { | ||
| 12 | return @sizeOf(S3); | ||
| 13 | } | ||
| 14 | const S4 = struct { a: u32, b: noreturn }; | ||
| 15 | export fn entry4() usize { | ||
| 16 | return @sizeOf(S4); | ||
| 17 | } | ||
| 4 | 18 | ||
| 5 | // error | 19 | // 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' |