| author | |
| committer | |
| log | 96d6b22067cccd644190e9b63f8f5bc13b6e93db |
| tree | 83bb7c6132731d5c3a6d4183e2f1d586e0d8d2d9 |
| parent | 4e92592fee6b414757b2e6c088e20ca9a1b21f44 |
| signature |
'comptime_int' is no longer considered a valid backing type for an enum.
In other words, 'enum(comptime_int)' is a compile error. This change is
accepted to simplify the language.6 files changed, 29 insertions(+), 60 deletions(-)
test/behavior/enum.zig-16| ... | ... | @@ -823,15 +823,6 @@ test "enum with one member and u1 tag type @intFromEnum" { |
| 823 | 823 | try expect(@intFromEnum(Enum.Test) == 0); |
| 824 | 824 | } |
| 825 | 825 | |
| 826 | test "enum with comptime_int tag type" { | |
| 827 | const Enum = enum(comptime_int) { | |
| 828 | One = 3, | |
| 829 | Two = 2, | |
| 830 | Three = 1, | |
| 831 | }; | |
| 832 | comptime assert(Tag(Enum) == comptime_int); | |
| 833 | } | |
| 834 | ||
| 835 | 826 | test "enum with one member default to u0 tag type" { |
| 836 | 827 | const E0 = enum { X }; |
| 837 | 828 | comptime assert(Tag(E0) == u0); |
| ... | ... | @@ -1274,13 +1265,6 @@ fn getLazyInitialized(param: enum(u8) { |
| 1274 | 1265 | return @intFromEnum(param); |
| 1275 | 1266 | } |
| 1276 | 1267 | |
| 1277 | test "Non-exhaustive enum backed by comptime_int" { | |
| 1278 | const E = enum(comptime_int) { a, b, c, _ }; | |
| 1279 | comptime var e: E = .a; | |
| 1280 | e = @as(E, @enumFromInt(378089457309184723749)); | |
| 1281 | try expect(@intFromEnum(e) == 378089457309184723749); | |
| 1282 | } | |
| 1283 | ||
| 1284 | 1268 | test "matching captures causes enum equivalence" { |
| 1285 | 1269 | const S = struct { |
| 1286 | 1270 | fn Nonexhaustive(comptime I: type) type { |
test/behavior/union.zig+12-23| ... | ... | @@ -703,25 +703,23 @@ test "union with only 1 field casted to its enum type which has enum value speci |
| 703 | 703 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 704 | 704 | |
| 705 | 705 | const Literal = union(enum) { |
| 706 | Number: f64, | |
| 707 | Bool: bool, | |
| 706 | number: f64, | |
| 707 | bool: bool, | |
| 708 | 708 | }; |
| 709 | 709 | |
| 710 | const ExprTag = enum(comptime_int) { | |
| 711 | Literal = 33, | |
| 712 | }; | |
| 710 | const ExprTag = enum(u32) { literal = 33 }; | |
| 711 | const Expr = union(ExprTag) { literal: Literal }; | |
| 713 | 712 | |
| 714 | const Expr = union(ExprTag) { | |
| 715 | Literal: Literal, | |
| 716 | }; | |
| 713 | comptime assert(Tag(ExprTag) == u32); | |
| 717 | 714 | |
| 718 | var e = Expr{ .Literal = Literal{ .Bool = true } }; | |
| 719 | _ = &e; | |
| 720 | comptime assert(Tag(ExprTag) == comptime_int); | |
| 721 | const t = comptime @as(ExprTag, e); | |
| 722 | try expect(t == Expr.Literal); | |
| 723 | try expect(@intFromEnum(t) == 33); | |
| 715 | var e: Expr = undefined; | |
| 716 | e = .{ .literal = .{ .bool = true } }; | |
| 717 | ||
| 718 | const t: ExprTag = e; | |
| 719 | comptime assert(t == Expr.literal); | |
| 724 | 720 | comptime assert(@intFromEnum(t) == 33); |
| 721 | try expect(t == Expr.literal); | |
| 722 | try expect(@intFromEnum(t) == 33); | |
| 725 | 723 | } |
| 726 | 724 | |
| 727 | 725 | test "@intFromEnum works on unions" { |
| ... | ... | @@ -893,15 +891,6 @@ test "union no tag with struct member" { |
| 893 | 891 | u.foo(); |
| 894 | 892 | } |
| 895 | 893 | |
| 896 | test "union with comptime_int tag" { | |
| 897 | const Union = union(enum(comptime_int)) { | |
| 898 | X: u32, | |
| 899 | Y: u16, | |
| 900 | Z: u8, | |
| 901 | }; | |
| 902 | comptime assert(Tag(Tag(Union)) == comptime_int); | |
| 903 | } | |
| 904 | ||
| 905 | 894 | test "extern union doesn't trigger field check at comptime" { |
| 906 | 895 | const U = extern union { |
| 907 | 896 | x: u32, |
test/cases/compile_errors/enum_backed_by_comptime_int.zig created+8| ... | ... | @@ -0,0 +1,8 @@ |
| 1 | const E = enum(comptime_int) { a }; | |
| 2 | comptime { | |
| 3 | _ = E.a; | |
| 4 | } | |
| 5 | ||
| 6 | // error | |
| 7 | // | |
| 8 | // :1:16: error: expected integer tag type, found 'comptime_int' |
test/cases/compile_errors/enum_backed_by_comptime_int_must_be_casted_from_comptime_value.zig deleted-12| ... | ... | @@ -1,12 +0,0 @@ |
| 1 | export fn entry() void { | |
| 2 | const Tag = enum(comptime_int) { a, b }; | |
| 3 | ||
| 4 | var v: u32 = 0; | |
| 5 | _ = &v; | |
| 6 | _ = @as(Tag, @enumFromInt(v)); | |
| 7 | } | |
| 8 | ||
| 9 | // error | |
| 10 | // | |
| 11 | // :6:31: error: unable to resolve comptime value | |
| 12 | // :6:31: note: value casted to enum with 'comptime_int' tag type must be comptime-known |
test/cases/compile_errors/enum_backed_by_comptime_int_must_be_comptime.zig deleted-9| ... | ... | @@ -1,9 +0,0 @@ |
| 1 | pub export fn entry() void { | |
| 2 | const E = enum(comptime_int) { a, b, c, _ }; | |
| 3 | var e: E = .a; | |
| 4 | _ = &e; | |
| 5 | } | |
| 6 | ||
| 7 | // error | |
| 8 | // | |
| 9 | // :3:12: error: variable of type 'tmp.entry.E' must be const or comptime |
test/cases/compile_errors/union_backed_by_enum_backed_by_comptime_int.zig created+9| ... | ... | @@ -0,0 +1,9 @@ |
| 1 | const U = union(enum(comptime_int)) { a: u32 }; | |
| 2 | comptime { | |
| 3 | const u: U = .{ .a = 123 }; | |
| 4 | _ = u; | |
| 5 | } | |
| 6 | ||
| 7 | // error | |
| 8 | // | |
| 9 | // :1:22: error: expected integer tag type, found 'comptime_int' |