authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-01-15 20:23:49+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-01-15 20:24:59+02:00
log6fd0dddf186f6435f422f2992f44ec9a35e09f20
tree4b285418ef2d5f47262247fa21040a7b0fb01c86
parent8d9d4a065820bfce0395465834cb9b7e01509a12
signature Commit is signed but in an unrecognized format.

implement non-exhaustive enums


4 files changed, 97 insertions(+), 35 deletions(-)

src/all_types.hpp+2
......@@ -1383,6 +1383,7 @@ struct ZigTypeEnum {
13831383 ContainerLayout layout;
13841384 ResolveStatus resolve_status;
13851385
1386 bool non_exhaustive;
13861387 bool resolve_loop_flag;
13871388};
13881389
......@@ -3665,6 +3666,7 @@ struct IrInstructionCheckSwitchProngs {
36653666 IrInstructionCheckSwitchProngsRange *ranges;
36663667 size_t range_count;
36673668 bool have_else_prong;
3669 bool have_underscore_prong;
36683670};
36693671
36703672struct IrInstructionCheckStatementIsVoid {
src/analyze.cpp+16-2
......@@ -2572,6 +2572,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
25722572 enum_type->data.enumeration.src_field_count = field_count;
25732573 enum_type->data.enumeration.fields = allocate<TypeEnumField>(field_count);
25742574 enum_type->data.enumeration.fields_by_name.init(field_count);
2575 enum_type->data.enumeration.non_exhaustive = false;
25752576
25762577 Scope *scope = &enum_type->data.enumeration.decls_scope->base;
25772578
......@@ -2648,6 +2649,21 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
26482649 buf_sprintf("consider 'union(enum)' here"));
26492650 }
26502651
2652 AstNode *tag_value = field_node->data.struct_field.value;
2653
2654 if (buf_eql_str(type_enum_field->name, "_")) {
2655 if (field_i != field_count - 1) {
2656 add_node_error(g, field_node, buf_sprintf("'_' field of non-exhaustive enum must be last"));
2657 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
2658 }
2659 if (tag_value != nullptr) {
2660 add_node_error(g, field_node, buf_sprintf("value assigned to '_' field of non-exhaustive enum"));
2661 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
2662 }
2663 enum_type->data.enumeration.non_exhaustive = true;
2664 continue;
2665 }
2666
26512667 auto field_entry = enum_type->data.enumeration.fields_by_name.put_unique(type_enum_field->name, type_enum_field);
26522668 if (field_entry != nullptr) {
26532669 ErrorMsg *msg = add_node_error(g, field_node,
......@@ -2657,8 +2673,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
26572673 continue;
26582674 }
26592675
2660 AstNode *tag_value = field_node->data.struct_field.value;
2661
26622676 if (tag_value != nullptr) {
26632677 // A user-specified value is available
26642678 ZigValue *result = analyze_const_value(g, scope, tag_value, tag_int_type,
src/codegen.cpp+1-1
......@@ -3356,7 +3356,7 @@ static LLVMValueRef ir_render_int_to_enum(CodeGen *g, IrExecutable *executable,
33563356 LLVMValueRef tag_int_value = gen_widen_or_shorten(g, ir_want_runtime_safety(g, &instruction->base),
33573357 instruction->target->value->type, tag_int_type, target_val);
33583358
3359 if (ir_want_runtime_safety(g, &instruction->base) && wanted_type->data.enumeration.layout != ContainerLayoutExtern) {
3359 if (ir_want_runtime_safety(g, &instruction->base) && !wanted_type->data.enumeration.non_exhaustive) {
33603360 LLVMBasicBlockRef bad_value_block = LLVMAppendBasicBlock(g->cur_fn_val, "BadValue");
33613361 LLVMBasicBlockRef ok_value_block = LLVMAppendBasicBlock(g->cur_fn_val, "OkValue");
33623362 size_t field_count = wanted_type->data.enumeration.src_field_count;
src/ir.cpp+78-32
......@@ -3451,7 +3451,7 @@ static IrInstruction *ir_build_err_to_int(IrBuilder *irb, Scope *scope, AstNode
34513451
34523452static IrInstruction *ir_build_check_switch_prongs(IrBuilder *irb, Scope *scope, AstNode *source_node,
34533453 IrInstruction *target_value, IrInstructionCheckSwitchProngsRange *ranges, size_t range_count,
3454 bool have_else_prong)
3454 bool have_else_prong, bool have_underscore_prong)
34553455{
34563456 IrInstructionCheckSwitchProngs *instruction = ir_build_instruction<IrInstructionCheckSwitchProngs>(
34573457 irb, scope, source_node);
......@@ -3459,6 +3459,7 @@ static IrInstruction *ir_build_check_switch_prongs(IrBuilder *irb, Scope *scope,
34593459 instruction->ranges = ranges;
34603460 instruction->range_count = range_count;
34613461 instruction->have_else_prong = have_else_prong;
3462 instruction->have_underscore_prong = have_underscore_prong;
34623463
34633464 ir_ref_instruction(target_value, irb->current_basic_block);
34643465 for (size_t i = 0; i < range_count; i += 1) {
......@@ -8090,34 +8091,11 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
80908091 Scope *subexpr_scope = create_runtime_scope(irb->codegen, node, scope, is_comptime);
80918092 Scope *comptime_scope = create_comptime_scope(irb->codegen, node, scope);
80928093 AstNode *else_prong = nullptr;
8094 AstNode *underscore_prong = nullptr;
80938095 for (size_t prong_i = 0; prong_i < prong_count; prong_i += 1) {
80948096 AstNode *prong_node = node->data.switch_expr.prongs.at(prong_i);
80958097 size_t prong_item_count = prong_node->data.switch_prong.items.length;
8096 if (prong_item_count == 0) {
8097 ResultLocPeer *this_peer_result_loc = create_peer_result(peer_parent);
8098 if (else_prong) {
8099 ErrorMsg *msg = add_node_error(irb->codegen, prong_node,
8100 buf_sprintf("multiple else prongs in switch expression"));
8101 add_error_note(irb->codegen, msg, else_prong,
8102 buf_sprintf("previous else prong is here"));
8103 return irb->codegen->invalid_instruction;
8104 }
8105 else_prong = prong_node;
8106
8107 IrBasicBlock *prev_block = irb->current_basic_block;
8108 if (peer_parent->peers.length > 0) {
8109 peer_parent->peers.last()->next_bb = else_block;
8110 }
8111 peer_parent->peers.append(this_peer_result_loc);
8112 ir_set_cursor_at_end_and_append_block(irb, else_block);
8113 if (!ir_gen_switch_prong_expr(irb, subexpr_scope, node, prong_node, end_block,
8114 is_comptime, var_is_comptime, target_value_ptr, nullptr, 0, &incoming_blocks, &incoming_values,
8115 &switch_else_var, LValNone, &this_peer_result_loc->base))
8116 {
8117 return irb->codegen->invalid_instruction;
8118 }
8119 ir_set_cursor_at_end(irb, prev_block);
8120 } else if (prong_node->data.switch_prong.any_items_are_range) {
8098 if (prong_node->data.switch_prong.any_items_are_range) {
81218099 ResultLocPeer *this_peer_result_loc = create_peer_result(peer_parent);
81228100
81238101 IrInstruction *ok_bit = nullptr;
......@@ -8195,6 +8173,59 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
81958173 }
81968174
81978175 ir_set_cursor_at_end_and_append_block(irb, range_block_no);
8176 } else {
8177 if (prong_item_count == 0) {
8178 if (else_prong) {
8179 ErrorMsg *msg = add_node_error(irb->codegen, prong_node,
8180 buf_sprintf("multiple else prongs in switch expression"));
8181 add_error_note(irb->codegen, msg, else_prong,
8182 buf_sprintf("previous else prong is here"));
8183 return irb->codegen->invalid_instruction;
8184 }
8185 else_prong = prong_node;
8186 if (underscore_prong) {
8187 ErrorMsg *msg = add_node_error(irb->codegen, prong_node,
8188 buf_sprintf("else and '_' prong in switch expression"));
8189 add_error_note(irb->codegen, msg, underscore_prong,
8190 buf_sprintf("'_' prong is here"));
8191 return irb->codegen->invalid_instruction;
8192 }
8193 } else if (prong_item_count == 1 &&
8194 prong_node->data.switch_prong.items.at(0)->type == NodeTypeSymbol &&
8195 buf_eql_str(prong_node->data.switch_prong.items.at(0)->data.symbol_expr.symbol, "_")) {
8196 if (underscore_prong) {
8197 ErrorMsg *msg = add_node_error(irb->codegen, prong_node,
8198 buf_sprintf("multiple '_' prongs in switch expression"));
8199 add_error_note(irb->codegen, msg, underscore_prong,
8200 buf_sprintf("previous '_' prong is here"));
8201 return irb->codegen->invalid_instruction;
8202 }
8203 underscore_prong = prong_node;
8204 if (else_prong) {
8205 ErrorMsg *msg = add_node_error(irb->codegen, prong_node,
8206 buf_sprintf("else and '_' prong in switch expression"));
8207 add_error_note(irb->codegen, msg, else_prong,
8208 buf_sprintf("else prong is here"));
8209 return irb->codegen->invalid_instruction;
8210 }
8211 } else {
8212 continue;
8213 }
8214 ResultLocPeer *this_peer_result_loc = create_peer_result(peer_parent);
8215
8216 IrBasicBlock *prev_block = irb->current_basic_block;
8217 if (peer_parent->peers.length > 0) {
8218 peer_parent->peers.last()->next_bb = else_block;
8219 }
8220 peer_parent->peers.append(this_peer_result_loc);
8221 ir_set_cursor_at_end_and_append_block(irb, else_block);
8222 if (!ir_gen_switch_prong_expr(irb, subexpr_scope, node, prong_node, end_block,
8223 is_comptime, var_is_comptime, target_value_ptr, nullptr, 0, &incoming_blocks, &incoming_values,
8224 &switch_else_var, LValNone, &this_peer_result_loc->base))
8225 {
8226 return irb->codegen->invalid_instruction;
8227 }
8228 ir_set_cursor_at_end(irb, prev_block);
81988229 }
81998230 }
82008231
......@@ -8206,6 +8237,8 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
82068237 continue;
82078238 if (prong_node->data.switch_prong.any_items_are_range)
82088239 continue;
8240 if (underscore_prong == prong_node)
8241 continue;
82098242
82108243 ResultLocPeer *this_peer_result_loc = create_peer_result(peer_parent);
82118244
......@@ -8249,7 +8282,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
82498282 }
82508283
82518284 IrInstruction *switch_prongs_void = ir_build_check_switch_prongs(irb, scope, node, target_value,
8252 check_ranges.items, check_ranges.length, else_prong != nullptr);
8285 check_ranges.items, check_ranges.length, else_prong != nullptr, underscore_prong != nullptr);
82538286
82548287 IrInstruction *br_instruction;
82558288 if (cases.length == 0) {
......@@ -8269,7 +8302,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
82698302 peer_parent->peers.at(i)->base.source_instruction = peer_parent->base.source_instruction;
82708303 }
82718304
8272 if (!else_prong) {
8305 if (!else_prong && !underscore_prong) {
82738306 if (peer_parent->peers.length != 0) {
82748307 peer_parent->peers.last()->next_bb = else_block;
82758308 }
......@@ -12790,7 +12823,7 @@ static IrInstruction *ir_analyze_int_to_enum(IrAnalyze *ira, IrInstruction *sour
1279012823 return ira->codegen->invalid_instruction;
1279112824
1279212825 TypeEnumField *field = find_enum_field_by_tag(wanted_type, &val->data.x_bigint);
12793 if (field == nullptr && wanted_type->data.enumeration.layout != ContainerLayoutExtern) {
12826 if (field == nullptr && !wanted_type->data.enumeration.non_exhaustive) {
1279412827 Buf *val_buf = buf_alloc();
1279512828 bigint_append_buf(val_buf, &val->data.x_bigint, 10);
1279612829 ErrorMsg *msg = ir_add_error(ira, source_instr,
......@@ -26433,10 +26466,23 @@ static IrInstruction *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
2643326466 bigint_incr(&field_index);
2643426467 }
2643526468 }
26436 if (!instruction->have_else_prong) {
26437 if (switch_type->data.enumeration.layout == ContainerLayoutExtern) {
26469 if (switch_type->data.enumeration.non_exhaustive && instruction->have_underscore_prong) {
26470 for (uint32_t i = 0; i < switch_type->data.enumeration.src_field_count; i += 1) {
26471 TypeEnumField *enum_field = &switch_type->data.enumeration.fields[i];
26472 if (buf_eql_str(enum_field->name, "_"))
26473 continue;
26474
26475 auto entry = field_prev_uses.maybe_get(enum_field->value);
26476 if (!entry) {
26477 ir_add_error(ira, &instruction->base,
26478 buf_sprintf("enumeration value '%s.%s' not handled in switch", buf_ptr(&switch_type->name),
26479 buf_ptr(enum_field->name)));
26480 }
26481 }
26482 } else if (!instruction->have_else_prong) {
26483 if (switch_type->data.enumeration.non_exhaustive) {
2643826484 ir_add_error(ira, &instruction->base,
26439 buf_sprintf("switch on an extern enum must have an else prong"));
26485 buf_sprintf("switch on non-exhaustive enum must include `else` or `_` prong"));
2644026486 }
2644126487 for (uint32_t i = 0; i < switch_type->data.enumeration.src_field_count; i += 1) {
2644226488 TypeEnumField *enum_field = &switch_type->data.enumeration.fields[i];