From 31ed8d293ddb79d34306e0b23e76d8a361c8856f Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Thu, 2 Feb 2023 14:39:13 +0200 Subject: [PATCH] Sema: add missing peer type resolution for error unions Closes #14077 --- src/Sema.zig | 19 +++++++++++++++++++ test/behavior/cast.zig | 12 ++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/Sema.zig b/src/Sema.zig index 2dba678931..ddcdecb6b4 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -29761,6 +29761,25 @@ fn resolvePeerTypes( continue; } }, + .ErrorSet => { + chosen = candidate; + chosen_i = candidate_i + 1; + if (err_set_ty) |chosen_set_ty| { + if (.ok == try sema.coerceInMemoryAllowedErrorSets(block, chosen_set_ty, chosen_ty, src, src)) { + continue; + } + if (.ok == try sema.coerceInMemoryAllowedErrorSets(block, chosen_ty, chosen_set_ty, src, src)) { + err_set_ty = chosen_ty; + continue; + } + + err_set_ty = try chosen_set_ty.errorSetMerge(sema.arena, chosen_ty); + continue; + } else { + err_set_ty = chosen_ty; + continue; + } + }, else => {}, } diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig index dbb4c07f64..30889aef04 100644 --- a/test/behavior/cast.zig +++ b/test/behavior/cast.zig @@ -1541,3 +1541,15 @@ test "single item pointer to pointer to array to slice" { const z1 = @as([]const i32, @as(*[1]i32, &x)); try expect(z1[0] == 1234); } + +test "peer type resolution forms error union" { + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO + + var foo: i32 = 123; + const result = if (foo < 0) switch (-foo) { + 0 => unreachable, + 42 => error.AccessDenied, + else => unreachable, + } else @intCast(u32, foo); + try expect(try result == 123); +}