| author | |
| committer | |
| log | c53d94e5127a8dcfefd906c5be0e6b81eaf3d22c |
| tree | f3e94d0a5a2a8f8d7c7cf3d3611ff2a47c47612f |
| parent | b72f858194a3f6391de06d83ffa49596cfce21a4 |
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 | ... | @@ -8312,7 +8312,7 @@ static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type, ResolveStatu |
| 8312 | 8312 | ||
| 8313 | uint32_t field_count = enum_type->data.enumeration.src_field_count; | 8313 | uint32_t field_count = enum_type->data.enumeration.src_field_count; |
| 8314 | 8314 | ||
| 8315 | assert(enum_type->data.enumeration.fields); | 8315 | assert(field_count == 0 || enum_type->data.enumeration.fields != nullptr); |
| 8316 | ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count); | 8316 | ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count); |
| 8317 | 8317 | ||
| 8318 | for (uint32_t i = 0; i < field_count; i += 1) { | 8318 | 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, | ... | @@ -21631,7 +21631,7 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira, |
| 21631 | case ZigTypeIdEnum: { | 21631 | case ZigTypeIdEnum: { |
| 21632 | if ((err = type_resolve(ira->codegen, target_type, ResolveStatusZeroBitsKnown))) | 21632 | if ((err = type_resolve(ira->codegen, target_type, ResolveStatusZeroBitsKnown))) |
| 21633 | return ira->codegen->invalid_instruction; | 21633 | 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) { |
| 21635 | TypeEnumField *only_field = &target_type->data.enumeration.fields[0]; | 21635 | TypeEnumField *only_field = &target_type->data.enumeration.fields[0]; |
| 21636 | IrInstruction *result = ir_const(ira, &switch_target_instruction->base, target_type); | 21636 | IrInstruction *result = ir_const(ira, &switch_target_instruction->base, target_type); |
| 21637 | bigint_init_bigint(&result->value->data.x_enum_tag, &only_field->value); | 21637 | 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" { | ... | @@ -65,6 +65,26 @@ test "non-exhaustive enum" { |
| 65 | comptime S.doTheTest(52); | 65 | comptime S.doTheTest(52); |
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | test "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 | |||
| 68 | test "enum type" { | 88 | test "enum type" { |
| 69 | const foo1 = Foo{ .One = 13 }; | 89 | const foo1 = Foo{ .One = 13 }; |
| 70 | const foo2 = Foo{ | 90 | const foo2 = Foo{ |