Skip to content

Commit 815b4d4

Browse files
committed
langref: mention error union switch peer resolution
1 parent 1f57320 commit 815b4d4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

doc/langref.html.in

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

0 commit comments

Comments
 (0)