Skip to content

Commit f464fe1

Browse files
committedDec 6, 2017
switch on enum which only has 1 field is comptime known
closes #593
·
0.15.20.2.0
1 parent bb6b4f8 commit f464fe1

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed
 

‎src/ir.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13174,6 +13174,16 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1317413174
return tag_type;
1317513175
}
1317613176
case TypeTableEntryIdEnum: {
13177+
type_ensure_zero_bits_known(ira->codegen, target_type);
13178+
if (type_is_invalid(target_type))
13179+
return ira->codegen->builtin_types.entry_invalid;
13180+
if (target_type->data.enumeration.src_field_count < 2) {
13181+
TypeEnumField *only_field = &target_type->data.enumeration.fields[0];
13182+
ConstExprValue *out_val = ir_build_const_from(ira, &switch_target_instruction->base);
13183+
bigint_init_bigint(&out_val->data.x_enum_tag, &only_field->value);
13184+
return target_type;
13185+
}
13186+
1317713187
if (pointee_val) {
1317813188
ConstExprValue *out_val = ir_build_const_from(ira, &switch_target_instruction->base);
1317913189
bigint_init_bigint(&out_val->data.x_enum_tag, &pointee_val->data.x_enum_tag);

‎test/cases/enum.zig‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,14 @@ fn doALoopThing(id: EnumWithOneMember) {
366366
test "comparison operator on enum with one member is comptime known" {
367367
doALoopThing(EnumWithOneMember.Eof);
368368
}
369+
370+
const State = enum {
371+
Start,
372+
};
373+
test "switch on enum with one member is comptime known" {
374+
var state = State.Start;
375+
switch (state) {
376+
State.Start => return,
377+
}
378+
@compileError("analysis should not reach here");
379+
}

0 commit comments

Comments
 (0)
Please sign in to comment.