authorgravatar for justin.b.alexander1@gmail.comalexander <justin.b.alexander1@gmail.com> 2018-12-26 10:31:45-06:00
committergravatar for justin.b.alexander1@gmail.comalexander <justin.b.alexander1@gmail.com> 2018-12-26 10:44:25-06:00
log4a1f0e141893fe56f540709c8fb12b8b8dc22218
tree668c9940453c556b4bdc17921fa567ddd1d8164b
parent280187031a68c577e84c72add037271153d27c62

Switching on bools with duplicate and missing value detection: Issue 1768


1 files changed, 33 insertions(+), 0 deletions(-)

src/ir.cpp+33
......@@ -19787,6 +19787,39 @@ static IrInstruction *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
1978719787 return ira->codegen->invalid_instruction;
1978819788 }
1978919789 }
19790 } else if (switch_type->id == ZigTypeIdBool) {
19791 int seenTrue = 0;
19792 int seenFalse = 0;
19793 for (size_t range_i = 0; range_i < instruction->range_count; range_i += 1) {
19794 IrInstructionCheckSwitchProngsRange *range = &instruction->ranges[range_i];
19795
19796 IrInstruction *value = range->start->child;
19797
19798 IrInstruction *casted_value = ir_implicit_cast(ira, value, switch_type);
19799 if (type_is_invalid(casted_value->value.type))
19800 return ira->codegen->invalid_instruction;
19801
19802 ConstExprValue *const_expr_val = ir_resolve_const(ira, casted_value, UndefBad);
19803 if (!const_expr_val)
19804 return ira->codegen->invalid_instruction;
19805
19806 assert(const_expr_val->type->id == ZigTypeIdBool);
19807
19808 if (const_expr_val->data.x_bool == true) {
19809 seenTrue += 1;
19810 } else {
19811 seenFalse += 1;
19812 }
19813
19814 if ((seenTrue > 1) || (seenFalse > 1)) {
19815 ir_add_error(ira, value, buf_sprintf("duplicate switch value"));
19816 return ira->codegen->invalid_instruction;
19817 }
19818 }
19819 if (((seenTrue < 1) || (seenFalse < 1)) && !instruction->have_else_prong) {
19820 ir_add_error(ira, &instruction->base, buf_sprintf("switch must handle all possibilities"));
19821 return ira->codegen->invalid_instruction;
19822 }
1979019823 } else if (!instruction->have_else_prong) {
1979119824 ir_add_error(ira, &instruction->base,
1979219825 buf_sprintf("else prong required when switching on type '%s'", buf_ptr(&switch_type->name)));