authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-18 15:13:21+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-18 15:13:21+01:00
logc53d94e5127a8dcfefd906c5be0e6b81eaf3d22c
treef3e94d0a5a2a8f8d7c7cf3d3611ff2a47c47612f
parentb72f858194a3f6391de06d83ffa49596cfce21a4

Prevent crash with empty non-exhaustive enum


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

src/analyze.cpp+1-1
......@@ -8312,7 +8312,7 @@ static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type, ResolveStatu
83128312
83138313 uint32_t field_count = enum_type->data.enumeration.src_field_count;
83148314
8315 assert(enum_type->data.enumeration.fields);
8315 assert(field_count == 0 || enum_type->data.enumeration.fields != nullptr);
83168316 ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count);
83178317
83188318 for (uint32_t i = 0; i < field_count; i += 1) {
src/ir.cpp+1-1
......@@ -21631,7 +21631,7 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira,
2163121631 case ZigTypeIdEnum: {
2163221632 if ((err = type_resolve(ira->codegen, target_type, ResolveStatusZeroBitsKnown)))
2163321633 return ira->codegen->invalid_instruction;
21634 if (target_type->data.enumeration.src_field_count < 2) {
21634 if (target_type->data.enumeration.src_field_count == 1) {
2163521635 TypeEnumField *only_field = &target_type->data.enumeration.fields[0];
2163621636 IrInstruction *result = ir_const(ira, &switch_target_instruction->base, target_type);
2163721637 bigint_init_bigint(&result->value->data.x_enum_tag, &only_field->value);
test/stage1/behavior/enum.zig+20
......@@ -65,6 +65,26 @@ test "non-exhaustive enum" {
6565 comptime S.doTheTest(52);
6666}
6767
68test "empty non-exhaustive enum" {
69 const S = struct {
70 const E = enum(u8) {
71 _,
72 };
73 fn doTheTest(y: u8) void {
74 var e = @intToEnum(E, y);
75 expect(switch (e) {
76 _ => true,
77 });
78 expect(@enumToInt(e) == y);
79
80 expect(@typeInfo(E).Enum.fields.len == 0);
81 expect(@typeInfo(E).Enum.is_exhaustive == false);
82 }
83 };
84 S.doTheTest(42);
85 comptime S.doTheTest(42);
86}
87
6888test "enum type" {
6989 const foo1 = Foo{ .One = 13 };
7090 const foo2 = Foo{