Skip to content

Commit

Permalink
make switch expressions allow enum literal types
Browse files Browse the repository at this point in the history
See #683
  • Loading branch information
andrewrk committed Mar 24, 2019
1 parent a736dfe commit aff7b38
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 8 additions & 7 deletions src/ir.cpp
Expand Up @@ -20884,18 +20884,19 @@ static IrInstruction *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
for (size_t range_i = 0; range_i < instruction->range_count; range_i += 1) {
IrInstructionCheckSwitchProngsRange *range = &instruction->ranges[range_i];

IrInstruction *start_value = range->start->child;
IrInstruction *start_value_uncasted = range->start->child;
if (type_is_invalid(start_value_uncasted->value.type))
return ira->codegen->invalid_instruction;
IrInstruction *start_value = ir_implicit_cast(ira, start_value_uncasted, switch_type);
if (type_is_invalid(start_value->value.type))
return ira->codegen->invalid_instruction;

IrInstruction *end_value = range->end->child;
if (type_is_invalid(end_value->value.type))
IrInstruction *end_value_uncasted = range->end->child;
if (type_is_invalid(end_value_uncasted->value.type))
return ira->codegen->invalid_instruction;

if (start_value->value.type->id != ZigTypeIdEnum) {
ir_add_error(ira, range->start, buf_sprintf("not an enum type"));
IrInstruction *end_value = ir_implicit_cast(ira, end_value_uncasted, switch_type);
if (type_is_invalid(end_value->value.type))
return ira->codegen->invalid_instruction;
}

BigInt start_index;
bigint_init_bigint(&start_index, &start_value->value.data.x_enum_tag);
Expand Down
2 changes: 1 addition & 1 deletion test/compile_errors.zig
Expand Up @@ -203,7 +203,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\const InvalidToken = struct {};
\\const ExpectedVarDeclOrFn = struct {};
,
"tmp.zig:4:9: error: not an enum type",
"tmp.zig:4:9: error: expected type '@TagType(Error)', found 'type'",
);

cases.addTest(
Expand Down

0 comments on commit aff7b38

Please sign in to comment.