Commit 716b0b8655

Andrew Kelley <superjoe30@gmail.com>
2017-11-30 03:33:58
fix capturing value of switch with all unreachable prongs
closes #635
1 parent ccea8dc
Changed files (3)
src/analyze.cpp
@@ -4664,7 +4664,7 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
             buf_appendf(buf, "%s", buf_ptr(&const_val->data.x_type->name));
             return;
         case TypeTableEntryIdUnreachable:
-            buf_appendf(buf, "@unreachable()");
+            buf_appendf(buf, "unreachable");
             return;
         case TypeTableEntryIdBool:
             {
src/ir.cpp
@@ -11154,8 +11154,8 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP
     }
 
     if (new_incoming_blocks.length == 0) {
-        ir_build_const_from(ira, &phi_instruction->base);
-        return ira->codegen->builtin_types.entry_void;
+        ir_build_unreachable_from(&ira->new_irb, &phi_instruction->base);
+        return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);
     }
 
     if (new_incoming_blocks.length == 1) {
test/cases/switch.zig
@@ -224,3 +224,14 @@ fn switchWithUnreachable(x: i32) -> i32 {
     }
     return 10;
 }
+
+fn return_a_number() -> %i32 {
+    return 1;
+}
+
+test "capture value of switch with all unreachable prongs" {
+    const x = return_a_number() %% |err| switch (err) {
+        else => unreachable,
+    };
+    assert(x == 1);
+}