diff --git a/src/Sema.zig b/src/Sema.zig index 06bd74c67d..376dc25278 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -12415,6 +12415,15 @@ fn coerceInMemoryAllowed(dest_ty: Type, src_ty: Type, dest_is_mut: bool, target: return coerceInMemoryAllowedFns(dest_ty, src_ty, target); } + // Error Unions + if (dest_ty.zigTypeTag() == .ErrorUnion and src_ty.zigTypeTag() == .ErrorUnion) { + const child = coerceInMemoryAllowed(dest_ty.errorUnionPayload(), src_ty.errorUnionPayload(), dest_is_mut, target); + if (child == .no_match) { + return child; + } + return coerceInMemoryAllowed(dest_ty.errorUnionSet(), src_ty.errorUnionSet(), dest_is_mut, target); + } + // Error Sets if (dest_ty.zigTypeTag() == .ErrorSet and src_ty.zigTypeTag() == .ErrorSet) { return coerceInMemoryAllowedErrorSets(dest_ty, src_ty); @@ -12422,7 +12431,6 @@ fn coerceInMemoryAllowed(dest_ty: Type, src_ty: Type, dest_is_mut: bool, target: // TODO: arrays // TODO: non-pointer-like optionals - // TODO: error unions // TODO: vectors return .no_match; diff --git a/test/behavior/error.zig b/test/behavior/error.zig index edbe866b95..b7d4511fe9 100644 --- a/test/behavior/error.zig +++ b/test/behavior/error.zig @@ -115,3 +115,19 @@ test "implicit cast to optional to error union to return result loc" { try S.entry(); //comptime S.entry(); TODO } + +test "error: fn returning empty error set can be passed as fn returning any error" { + entry(); + comptime entry(); +} + +fn entry() void { + foo2(bar2); +} + +fn foo2(f: fn () anyerror!void) void { + const x = f(); + x catch {}; +} + +fn bar2() (error{}!void) {} diff --git a/test/behavior/error_stage1.zig b/test/behavior/error_stage1.zig index c379257b99..b279927eb8 100644 --- a/test/behavior/error_stage1.zig +++ b/test/behavior/error_stage1.zig @@ -120,22 +120,6 @@ fn quux_1() !i32 { return error.C; } -test "error: fn returning empty error set can be passed as fn returning any error" { - entry(); - comptime entry(); -} - -fn entry() void { - foo2(bar2); -} - -fn foo2(f: fn () anyerror!void) void { - const x = f(); - x catch {}; -} - -fn bar2() (error{}!void) {} - test "error: Zero sized error set returned with value payload crash" { _ = foo3(0) catch {}; _ = comptime foo3(0) catch {};