authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-09-13 09:29:35+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-24 01:09:26-04:00
log5fb7af26abbdf8f23007c2ccb4114e40ca0f450d
treeb77b5723938032486e01cb5f833a452a29b1d6c6
parent93367adaa770944a13a8d44628dfefe1abe91906
signaturelock-open Commit is signed but in an unrecognized format.

Fix result loc unwrapping with optional in error union

Fixes #2899

2 files changed, 14 insertions(+), 1 deletions(-)

src/ir.cpp+2-1
...@@ -15496,7 +15496,8 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s...@@ -15496,7 +15496,8 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s
15496 IrInstruction *unwrapped_err_ptr = ir_analyze_unwrap_error_payload(ira, suspend_source_instr,15496 IrInstruction *unwrapped_err_ptr = ir_analyze_unwrap_error_payload(ira, suspend_source_instr,
15497 result_loc, false, true);15497 result_loc, false, true);
15498 ZigType *actual_payload_type = actual_elem_type->data.error_union.payload_type;15498 ZigType *actual_payload_type = actual_elem_type->data.error_union.payload_type;
15499 if (actual_payload_type->id == ZigTypeIdOptional && value_type->id != ZigTypeIdOptional) {15499 if (actual_payload_type->id == ZigTypeIdOptional && value_type->id != ZigTypeIdOptional &&
15500 value_type->id != ZigTypeIdNull) {
15500 return ir_analyze_unwrap_optional_payload(ira, suspend_source_instr, unwrapped_err_ptr, false, true);15501 return ir_analyze_unwrap_optional_payload(ira, suspend_source_instr, unwrapped_err_ptr, false, true);
15501 } else {15502 } else {
15502 return unwrapped_err_ptr;15503 return unwrapped_err_ptr;
test/stage1/behavior/misc.zig+12
...@@ -754,3 +754,15 @@ test "nested optional field in struct" {...@@ -754,3 +754,15 @@ test "nested optional field in struct" {
754 };754 };
755 expect(s.x.?.y == 127);755 expect(s.x.?.y == 127);
756}756}
757
758fn maybe(x: bool) anyerror!?u32 {
759 return switch (x) {
760 true => u32(42),
761 else => null,
762 };
763}
764
765test "result location is optional inside error union" {
766 const x = maybe(true) catch unreachable;
767 expect(x.? == 42);
768}