Commit 953355ebea

Will Lillis <will.lillis24@gmail.com>
2025-02-02 04:36:16
fix: error on non-exhaustive enums with zero width backing type (#21374)
Co-authored-by: WillLillis <wlillis@umass.edu>
1 parent 963651b
Changed files (2)
src
test
src/Sema.zig
@@ -38436,12 +38436,6 @@ fn resolveDeclaredEnumInner(
 
     wip_ty.setTagTy(ip, int_tag_ty.toIntern());
 
-    if (small.nonexhaustive and int_tag_ty.toIntern() != .comptime_int_type) {
-        if (fields_len > 1 and std.math.log2_int(u64, fields_len) == int_tag_ty.bitSize(zcu)) {
-            return sema.fail(block, src, "non-exhaustive enum specifies every value", .{});
-        }
-    }
-
     var extra_index = body_end + bit_bags_count;
     var bit_bag_index: usize = body_end;
     var cur_bit_bag: u32 = undefined;
@@ -38528,6 +38522,11 @@ fn resolveDeclaredEnumInner(
             return sema.failWithOwnedErrorMsg(block, msg);
         }
     }
+    if (small.nonexhaustive and int_tag_ty.toIntern() != .comptime_int_type) {
+        if (fields_len >= 1 and std.math.log2_int(u64, fields_len) == int_tag_ty.bitSize(zcu)) {
+            return sema.fail(block, src, "non-exhaustive enum specifies every value", .{});
+        }
+    }
 }
 
 pub const bitCastVal = @import("Sema/bitcast.zig").bitCast;
test/cases/compile_errors/zero_width_nonexhaustive_enum.zig
@@ -0,0 +1,17 @@
+comptime {
+    _ = enum(i0) { a, _ };
+}
+
+comptime {
+    _ = enum(u0) { a, _ };
+}
+
+comptime {
+    _ = enum(u0) { a, b, _ };
+}
+
+// error
+//
+// :2:9: error: non-exhaustive enum specifies every value
+// :6:9: error: non-exhaustive enum specifies every value
+// :10:23: error: enumeration value '1' too large for type 'u0'