authorgravatar for will.lillis24@gmail.comWill Lillis <will.lillis24@gmail.com> 2025-02-01 22:36:16-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-02-02 03:36:16+00:00
log953355ebeab881abff4a2c9315daa4fbb290d733
tree28e266010b69995fab79197a43a583bb5e511945
parent963651bbf292b21017cac9e977a933b5e2a8c671
signaturebadge-check Signed by PGP key B5690EEEBB952194

fix: error on non-exhaustive enums with zero width backing type (#21374)

Co-authored-by: WillLillis <wlillis@umass.edu>

2 files changed, 22 insertions(+), 6 deletions(-)

src/Sema.zig+5-6
......@@ -38436,12 +38436,6 @@ fn resolveDeclaredEnumInner(
3843638436
3843738437 wip_ty.setTagTy(ip, int_tag_ty.toIntern());
3843838438
38439 if (small.nonexhaustive and int_tag_ty.toIntern() != .comptime_int_type) {
38440 if (fields_len > 1 and std.math.log2_int(u64, fields_len) == int_tag_ty.bitSize(zcu)) {
38441 return sema.fail(block, src, "non-exhaustive enum specifies every value", .{});
38442 }
38443 }
38444
3844538439 var extra_index = body_end + bit_bags_count;
3844638440 var bit_bag_index: usize = body_end;
3844738441 var cur_bit_bag: u32 = undefined;
......@@ -38528,6 +38522,11 @@ fn resolveDeclaredEnumInner(
3852838522 return sema.failWithOwnedErrorMsg(block, msg);
3852938523 }
3853038524 }
38525 if (small.nonexhaustive and int_tag_ty.toIntern() != .comptime_int_type) {
38526 if (fields_len >= 1 and std.math.log2_int(u64, fields_len) == int_tag_ty.bitSize(zcu)) {
38527 return sema.fail(block, src, "non-exhaustive enum specifies every value", .{});
38528 }
38529 }
3853138530}
3853238531
3853338532pub const bitCastVal = @import("Sema/bitcast.zig").bitCast;
test/cases/compile_errors/zero_width_nonexhaustive_enum.zig created+17
......@@ -0,0 +1,17 @@
1comptime {
2 _ = enum(i0) { a, _ };
3}
4
5comptime {
6 _ = enum(u0) { a, _ };
7}
8
9comptime {
10 _ = enum(u0) { a, b, _ };
11}
12
13// error
14//
15// :2:9: error: non-exhaustive enum specifies every value
16// :6:9: error: non-exhaustive enum specifies every value
17// :10:23: error: enumeration value '1' too large for type 'u0'