zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 953355ebeab881abff4a2c9315daa4fbb290d733 (tree)
parent 963651bbf292b21017cac9e977a933b5e2a8c671
Author: Will Lillis <will.lillis24@gmail.com>
Date:   Sat,  1 Feb 2025 22:36:16 -0500

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

Co-authored-by: WillLillis <wlillis@umass.edu>
Diffstat:
Msrc/Sema.zig | 11+++++------
Atest/cases/compile_errors/zero_width_nonexhaustive_enum.zig | 17+++++++++++++++++
2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/src/Sema.zig b/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; diff --git a/test/cases/compile_errors/zero_width_nonexhaustive_enum.zig b/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'