Commit a3cfb15fb4

Veikka Tuominen <git@vexu.eu>
2022-03-12 10:36:05
Sema: always allow coercing error set to current inferred error set
1 parent 07cc2fc
Changed files (2)
src
test
behavior
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()) {
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 {};
+}