authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-24 01:15:21-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-24 01:15:21-04:00
logaff7b38838628a18f384c1f625d71c085c8eee1f
treeda3aeac68f687d1fb15e5aa138f2b4436acba438
parenta736dfe6a1c7a43c9cb9474255ec34261e5f80ab
signaturelock-open Commit is signed but in an unrecognized format.

make switch expressions allow enum literal types

See #683

2 files changed, 9 insertions(+), 8 deletions(-)

src/ir.cpp+8-7
...@@ -20884,18 +20884,19 @@ static IrInstruction *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,...@@ -20884,18 +20884,19 @@ static IrInstruction *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
20884 for (size_t range_i = 0; range_i < instruction->range_count; range_i += 1) {20884 for (size_t range_i = 0; range_i < instruction->range_count; range_i += 1) {
20885 IrInstructionCheckSwitchProngsRange *range = &instruction->ranges[range_i];20885 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);
20888 if (type_is_invalid(start_value->value.type))20891 if (type_is_invalid(start_value->value.type))
20889 return ira->codegen->invalid_instruction;20892 return ira->codegen->invalid_instruction;
2089020893
20891 IrInstruction *end_value = range->end->child;20894 IrInstruction *end_value_uncasted = range->end->child;
20892 if (type_is_invalid(end_value->value.type))20895 if (type_is_invalid(end_value_uncasted->value.type))
20893 return ira->codegen->invalid_instruction;20896 return ira->codegen->invalid_instruction;
2089420897 IrInstruction *end_value = ir_implicit_cast(ira, end_value_uncasted, switch_type);
20895 if (start_value->value.type->id != ZigTypeIdEnum) {20898 if (type_is_invalid(end_value->value.type))
20896 ir_add_error(ira, range->start, buf_sprintf("not an enum type"));
20897 return ira->codegen->invalid_instruction;20899 return ira->codegen->invalid_instruction;
20898 }
2089920900
20900 BigInt start_index;20901 BigInt start_index;
20901 bigint_init_bigint(&start_index, &start_value->value.data.x_enum_tag);20902 bigint_init_bigint(&start_index, &start_value->value.data.x_enum_tag);
test/compile_errors.zig+1-1
...@@ -203,7 +203,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -203,7 +203,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
203 \\const InvalidToken = struct {};203 \\const InvalidToken = struct {};
204 \\const ExpectedVarDeclOrFn = struct {};204 \\const ExpectedVarDeclOrFn = struct {};
205 ,205 ,
206 "tmp.zig:4:9: error: not an enum type",206 "tmp.zig:4:9: error: expected type '@TagType(Error)', found 'type'",
207 );207 );
208208
209 cases.addTest(209 cases.addTest(