From 5fb7af26abbdf8f23007c2ccb4114e40ca0f450d Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Fri, 13 Sep 2019 09:29:35 +0200 Subject: [PATCH] Fix result loc unwrapping with optional in error union Fixes #2899 --- src/ir.cpp | 3 ++- test/stage1/behavior/misc.zig | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/ir.cpp b/src/ir.cpp index 4e6752a0fa..b3906d8b61 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -15496,7 +15496,8 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s IrInstruction *unwrapped_err_ptr = ir_analyze_unwrap_error_payload(ira, suspend_source_instr, result_loc, false, true); ZigType *actual_payload_type = actual_elem_type->data.error_union.payload_type; - if (actual_payload_type->id == ZigTypeIdOptional && value_type->id != ZigTypeIdOptional) { + if (actual_payload_type->id == ZigTypeIdOptional && value_type->id != ZigTypeIdOptional && + value_type->id != ZigTypeIdNull) { return ir_analyze_unwrap_optional_payload(ira, suspend_source_instr, unwrapped_err_ptr, false, true); } else { return unwrapped_err_ptr; diff --git a/test/stage1/behavior/misc.zig b/test/stage1/behavior/misc.zig index cffe1faebe..ab16b08be8 100644 --- a/test/stage1/behavior/misc.zig +++ b/test/stage1/behavior/misc.zig @@ -754,3 +754,15 @@ test "nested optional field in struct" { }; expect(s.x.?.y == 127); } + +fn maybe(x: bool) anyerror!?u32 { + return switch (x) { + true => u32(42), + else => null, + }; +} + +test "result location is optional inside error union" { + const x = maybe(true) catch unreachable; + expect(x.? == 42); +}