Commit 5c2238fc4a

Vexu <git@vexu.eu>
2020-01-15 21:09:19
small fixes
* error for '_' prong on exhaustive enum * todo panic for `@tagName` on non-exhaustive enum * don't require '_' field on tagged unions
1 parent c57784a
Changed files (3)
src/analyze.cpp
@@ -3293,7 +3293,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
     } else if (enum_type_node != nullptr) {
         for (uint32_t i = 0; i < tag_type->data.enumeration.src_field_count; i += 1) {
             TypeEnumField *enum_field = &tag_type->data.enumeration.fields[i];
-            if (!covered_enum_fields[i]) {
+            if (!covered_enum_fields[i] && !buf_eql_str(enum_field->name, "_")) {
                 AstNode *enum_decl_node = tag_type->data.enumeration.decl_node;
                 AstNode *field_node = enum_decl_node->data.container_decl.fields.at(i);
                 ErrorMsg *msg = add_node_error(g, decl_node,
src/codegen.cpp
@@ -5065,6 +5065,8 @@ static LLVMValueRef ir_render_enum_tag_name(CodeGen *g, IrExecutable *executable
 {
     ZigType *enum_type = instruction->target->value->type;
     assert(enum_type->id == ZigTypeIdEnum);
+    if (enum_type->data.enumeration.non_exhaustive)
+        zig_panic("TODO @tagName on non-exhaustive enum");
 
     LLVMValueRef enum_name_function = get_enum_tag_name_function(g, enum_type);
 
src/ir.cpp
@@ -22357,6 +22357,8 @@ static IrInstruction *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrIns
     if (instr_is_comptime(target)) {
         if ((err = type_resolve(ira->codegen, target->value->type, ResolveStatusZeroBitsKnown)))
             return ira->codegen->invalid_instruction;
+        if (target->value->type->data.enumeration.non_exhaustive)
+            zig_panic("TODO @tagName on non-exhaustive enum");
         TypeEnumField *field = find_enum_field_by_tag(target->value->type, &target->value->data.x_bigint);
         ZigValue *array_val = create_const_str_lit(ira->codegen, field->name)->data.x_ptr.data.ref.pointee;
         IrInstruction *result = ir_const(ira, &instruction->base, nullptr);
@@ -26471,7 +26473,11 @@ static IrInstruction *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
                 bigint_incr(&field_index);
             }
         }
-        if (switch_type->data.enumeration.non_exhaustive && instruction->have_underscore_prong) {
+        if (instruction->have_underscore_prong) {
+            if (!switch_type->data.enumeration.non_exhaustive){
+                ir_add_error(ira, &instruction->base,
+                    buf_sprintf("switch on non-exhaustive enum has `_` prong"));
+            }
             for (uint32_t i = 0; i < switch_type->data.enumeration.src_field_count; i += 1) {
                 TypeEnumField *enum_field = &switch_type->data.enumeration.fields[i];
                 if (buf_eql_str(enum_field->name, "_"))