authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-12-08 20:16:11+11:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2024-01-09 14:42:12+11:00
log8695bc7ed3583bb6b896079dd0b7bfd678e82722
tree11dd480f101f294070785777d15f041bd1614451
parent6a18cee3af8021bcebbca40413056b18f33af8c7

langref: mention error union switch peer resolution


1 files changed, 26 insertions(+), 0 deletions(-)

doc/langref.html.in+26
......@@ -6801,6 +6801,32 @@ test "peer type resolution: *const T and ?*T" {
68016801 try expect(a == b);
68026802 try expect(b == a);
68036803}
6804
6805test "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#}