authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-02 16:08:20+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-02 17:57:11+03:00
logf281f3d10e4eaedc7c68afc4fcbbfd35e1f29a0f
tree3a28628a95126b07fe7fa17e12ff3cbab1e9df65
parenta9cdacff95a2a6f60945c7b2a299f9f66bd94ddb

Sema: improve behavior of comptime_int backed enums


4 files changed, 23 insertions(+), 4 deletions(-)

src/Sema.zig+4-3
......@@ -2565,7 +2565,7 @@ fn zirEnumDecl(
25652565 }
25662566 }
25672567
2568 if (small.nonexhaustive) {
2568 if (small.nonexhaustive and enum_obj.tag_ty.zigTypeTag() != .ComptimeInt) {
25692569 if (fields_len > 1 and std.math.log2_int(u64, fields_len) == enum_obj.tag_ty.bitSize(sema.mod.getTarget())) {
25702570 return sema.fail(block, src, "non-exhaustive enum specifies every value", .{});
25712571 }
......@@ -20363,12 +20363,13 @@ fn validateRunTimeType(
2036320363 .Int,
2036420364 .Float,
2036520365 .ErrorSet,
20366 .Enum,
2036720366 .Frame,
2036820367 .AnyFrame,
2036920368 .Void,
2037020369 => return true,
2037120370
20371 .Enum => return !(try sema.typeRequiresComptime(block, src, ty)),
20372
2037220373 .BoundFn,
2037320374 .ComptimeFloat,
2037420375 .ComptimeInt,
......@@ -29049,7 +29050,7 @@ pub fn typeHasOnePossibleValue(
2904929050 },
2905029051 .enum_nonexhaustive => {
2905129052 const tag_ty = ty.castTag(.enum_nonexhaustive).?.data.tag_ty;
29052 if (!(try sema.typeHasRuntimeBits(block, src, tag_ty))) {
29053 if (tag_ty.zigTypeTag() != .ComptimeInt and !(try sema.typeHasRuntimeBits(block, src, tag_ty))) {
2905329054 return Value.zero;
2905429055 } else {
2905529056 return null;
test/behavior/enum.zig+7
......@@ -1175,3 +1175,10 @@ test "Non-exhaustive enum with nonstandard int size behaves correctly" {
11751175 const E = enum(u15) { _ };
11761176 try expect(@sizeOf(E) == @sizeOf(u15));
11771177}
1178
1179test "Non-exhaustive enum backed by comptime_int" {
1180 const E = enum(comptime_int) { a, b, c, _ };
1181 comptime var e: E = .a;
1182 e = @intToEnum(E, 378089457309184723749);
1183 try expect(@enumToInt(e) == 378089457309184723749);
1184}
test/behavior/union.zig+1-1
......@@ -690,7 +690,7 @@ test "union with only 1 field casted to its enum type which has enum value speci
690690
691691 var e = Expr{ .Literal = Literal{ .Bool = true } };
692692 comptime try expect(Tag(ExprTag) == comptime_int);
693 var t = @as(ExprTag, e);
693 comptime var t = @as(ExprTag, e);
694694 try expect(t == Expr.Literal);
695695 try expect(@enumToInt(t) == 33);
696696 comptime try expect(@enumToInt(t) == 33);
test/cases/compile_errors/enum_backed_by_comptime_int_must_be_comptime.zig created+11
......@@ -0,0 +1,11 @@
1pub 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// backend=stage2
9// target=native
10//
11// :3:12: error: variable of type 'tmp.entry.E' must be const or comptime