diff --git a/src/Sema.zig b/src/Sema.zig index 958a737ef7d5b04ce8958857e415cd0878bda30c..d813cf374bb78a4f92bb684725623125a7f61287 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -16692,6 +16692,13 @@ fn coerceInMemoryAllowedErrorSets( else => unreachable, } + if (dst_ies.func == sema.owner_func) { + // We are trying to coerce an error set to the current function's + // inferred error set. + try dst_ies.addErrorSet(sema.gpa, src_ty); + return .ok; + } + try sema.resolveInferredErrorSet(block, dest_src, dst_payload.data); // isAnyError might have changed from a false negative to a true positive after resolution. if (dest_ty.isAnyError()) { diff --git a/test/behavior/error.zig b/test/behavior/error.zig index 93d76443aefd98294102fbd9d8d93e10dad76bc8..e6c69f4abf64e05bc2d74cb3c466dc98a6b73335 100644 --- a/test/behavior/error.zig +++ b/test/behavior/error.zig @@ -634,3 +634,18 @@ test "peer type resolution of two different error unions" { const err = if (cond) a else b; try err; } + +test "coerce error set to the current inferred error set" { + const S = struct { + fn foo() !void { + var a = false; + if (a) { + const b: error{A}!void = error.A; + return b; + } + const b = error.A; + return b; + } + }; + S.foo() catch {}; +}