Skip to content

Commit aff7b38

Browse files
committed
make switch expressions allow enum literal types
See #683
1 parent a736dfe commit aff7b38

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/ir.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20884,18 +20884,19 @@ static IrInstruction *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
2088420884
for (size_t range_i = 0; range_i < instruction->range_count; range_i += 1) {
2088520885
IrInstructionCheckSwitchProngsRange *range = &instruction->ranges[range_i];
2088620886

20887-
IrInstruction *start_value = range->start->child;
20887+
IrInstruction *start_value_uncasted = range->start->child;
20888+
if (type_is_invalid(start_value_uncasted->value.type))
20889+
return ira->codegen->invalid_instruction;
20890+
IrInstruction *start_value = ir_implicit_cast(ira, start_value_uncasted, switch_type);
2088820891
if (type_is_invalid(start_value->value.type))
2088920892
return ira->codegen->invalid_instruction;
2089020893

20891-
IrInstruction *end_value = range->end->child;
20892-
if (type_is_invalid(end_value->value.type))
20894+
IrInstruction *end_value_uncasted = range->end->child;
20895+
if (type_is_invalid(end_value_uncasted->value.type))
2089320896
return ira->codegen->invalid_instruction;
20894-
20895-
if (start_value->value.type->id != ZigTypeIdEnum) {
20896-
ir_add_error(ira, range->start, buf_sprintf("not an enum type"));
20897+
IrInstruction *end_value = ir_implicit_cast(ira, end_value_uncasted, switch_type);
20898+
if (type_is_invalid(end_value->value.type))
2089720899
return ira->codegen->invalid_instruction;
20898-
}
2089920900

2090020901
BigInt start_index;
2090120902
bigint_init_bigint(&start_index, &start_value->value.data.x_enum_tag);

test/compile_errors.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
203203
\\const InvalidToken = struct {};
204204
\\const ExpectedVarDeclOrFn = struct {};
205205
,
206-
"tmp.zig:4:9: error: not an enum type",
206+
"tmp.zig:4:9: error: expected type '@TagType(Error)', found 'type'",
207207
);
208208

209209
cases.addTest(

0 commit comments

Comments
 (0)