authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-11-29 21:33:58-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-11-29 21:34:17-05:00
log716b0b8655610a3a64818c51a91254c6e4715e49
tree0933336a01ef47e578db46a90b7641c843a7c3ef
parentccea8dcbf61cc4483bc73ba45751e545a8f3541e

fix capturing value of switch with all unreachable prongs

closes #635

3 files changed, 14 insertions(+), 3 deletions(-)

src/analyze.cpp+1-1
...@@ -4664,7 +4664,7 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {...@@ -4664,7 +4664,7 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
4664 buf_appendf(buf, "%s", buf_ptr(&const_val->data.x_type->name));4664 buf_appendf(buf, "%s", buf_ptr(&const_val->data.x_type->name));
4665 return;4665 return;
4666 case TypeTableEntryIdUnreachable:4666 case TypeTableEntryIdUnreachable:
4667 buf_appendf(buf, "@unreachable()");4667 buf_appendf(buf, "unreachable");
4668 return;4668 return;
4669 case TypeTableEntryIdBool:4669 case TypeTableEntryIdBool:
4670 {4670 {
src/ir.cpp+2-2
...@@ -11154,8 +11154,8 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP...@@ -11154,8 +11154,8 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP
11154 }11154 }
1115511155
11156 if (new_incoming_blocks.length == 0) {11156 if (new_incoming_blocks.length == 0) {
11157 ir_build_const_from(ira, &phi_instruction->base);11157 ir_build_unreachable_from(&ira->new_irb, &phi_instruction->base);
11158 return ira->codegen->builtin_types.entry_void;11158 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);
11159 }11159 }
1116011160
11161 if (new_incoming_blocks.length == 1) {11161 if (new_incoming_blocks.length == 1) {
test/cases/switch.zig+11
...@@ -224,3 +224,14 @@ fn switchWithUnreachable(x: i32) -> i32 {...@@ -224,3 +224,14 @@ fn switchWithUnreachable(x: i32) -> i32 {
224 }224 }
225 return 10;225 return 10;
226}226}
227
228fn return_a_number() -> %i32 {
229 return 1;
230}
231
232test "capture value of switch with all unreachable prongs" {
233 const x = return_a_number() %% |err| switch (err) {
234 else => unreachable,
235 };
236 assert(x == 1);
237}