diff options
| author | antlilja <liljaanton2001@gmail.com> | 2020-06-24 19:12:42 +0200 |
|---|---|---|
| committer | antlilja <liljaanton2001@gmail.com> | 2020-07-01 16:09:36 +0200 |
| commit | dcc406deff0fb4ee3c2cd1f4ff8614e972a8ea7a (patch) | |
| tree | ce198a2b6005c96a74a65de3b7af7126efc7be23 /src/ir.cpp | |
| parent | e60be3082499ff19078699675044b993f6921ad0 (diff) | |
| download | zig-dcc406deff0fb4ee3c2cd1f4ff8614e972a8ea7a.tar.gz zig-dcc406deff0fb4ee3c2cd1f4ff8614e972a8ea7a.zip | |
Add new error message for unreachable else prongs
* Adds error message for types: enum, int and bool
* Adds compile error tests
Diffstat (limited to 'src/ir.cpp')
| -rw-r--r-- | src/ir.cpp | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/src/ir.cpp b/src/ir.cpp index 44df69aeed..8395cf0a3e 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -28700,6 +28700,10 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira, buf_ptr(enum_field->name))); } } + } else if(!switch_type->data.enumeration.non_exhaustive && switch_type->data.enumeration.src_field_count == instruction->range_count) { + ir_add_error_node(ira, instruction->else_prong, + buf_sprintf("unreachable else prong, all cases already handled")); + return ira->codegen->invalid_inst_gen; } } else if (switch_type->id == ZigTypeIdErrorSet) { if (!resolve_inferred_error_set(ira->codegen, switch_type, target_value->base.source_node)) { @@ -28808,16 +28812,20 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira, return ira->codegen->invalid_inst_gen; } } - if (instruction->else_prong == nullptr) { + BigInt min_val; eval_min_max_value_int(ira->codegen, switch_type, &min_val, false); BigInt max_val; eval_min_max_value_int(ira->codegen, switch_type, &max_val, true); - if (!rangeset_spans(&rs, &min_val, &max_val)) { + bool handles_all_cases = rangeset_spans(&rs, &min_val, &max_val); + if (!handles_all_cases && instruction->else_prong == nullptr) { ir_add_error(ira, &instruction->base.base, buf_sprintf("switch must handle all possibilities")); return ira->codegen->invalid_inst_gen; + } else if(handles_all_cases && instruction->else_prong != nullptr) { + ir_add_error_node(ira, instruction->else_prong, + buf_sprintf("unreachable else prong, all cases already handled")); + return ira->codegen->invalid_inst_gen; } - } } else if (switch_type->id == ZigTypeIdBool) { int seenTrue = 0; int seenFalse = 0; @@ -28851,6 +28859,12 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira, ir_add_error(ira, &instruction->base.base, buf_sprintf("switch must handle all possibilities")); return ira->codegen->invalid_inst_gen; } + + if(seenTrue == 1 && seenFalse == 1 && instruction->else_prong != nullptr) { + ir_add_error_node(ira, instruction->else_prong, + buf_sprintf("unreachable else prong, all cases already handled")); + return ira->codegen->invalid_inst_gen; + } } else if (instruction->else_prong == nullptr) { ir_add_error(ira, &instruction->base.base, buf_sprintf("else prong required when switching on type '%s'", buf_ptr(&switch_type->name))); |
