Skip to content

Commit acca16c

Browse files
authored
Merge pull request #18173 from dweiller/switch-err-union
Special-case switching on error union capture
2 parents dbdee2d + 67d7d7b commit acca16c

14 files changed

+2412
-301
lines changed

doc/langref.html.in

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6801,6 +6801,32 @@ test "peer type resolution: *const T and ?*T" {
68016801
try expect(a == b);
68026802
try expect(b == a);
68036803
}
6804+
6805+
test "peer type resolution: error union switch" {
6806+
// The non-error and error cases are only peers if the error case is just a switch expression;
6807+
// the pattern `if (x) {...} else |err| blk: { switch (err) {...} }` does not consider the
6808+
// non-error and error case to be peers.
6809+
var a: error{ A, B, C }!u32 = 0;
6810+
_ = &a;
6811+
const b = if (a) |x|
6812+
x + 3
6813+
else |err| switch (err) {
6814+
error.A => 0,
6815+
error.B => 1,
6816+
error.C => null,
6817+
};
6818+
try expect(@TypeOf(b) == ?u32);
6819+
6820+
// The non-error and error cases are only peers if the error case is just a switch expression;
6821+
// the pattern `x catch |err| blk: { switch (err) {...} }` does not consider the unwrapped `x`
6822+
// and error case to be peers.
6823+
const c = a catch |err| switch (err) {
6824+
error.A => 0,
6825+
error.B => 1,
6826+
error.C => null,
6827+
};
6828+
try expect(@TypeOf(c) == ?u32);
6829+
}
68046830
{#code_end#}
68056831
{#header_close#}
68066832
{#header_close#}

0 commit comments

Comments
 (0)