From 815b4d4183fb82a99171512d600f29da743731f2 Mon Sep 17 00:00:00 2001 From: dweiller <4678790+dweiller@users.noreply.github.com> Date: Fri, 8 Dec 2023 20:16:11 +1100 Subject: [PATCH] langref: mention error union switch peer resolution --- doc/langref.html.in | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/doc/langref.html.in b/doc/langref.html.in index 870726fada07..c115ff578c74 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -6804,6 +6804,32 @@ test "peer type resolution: *const T and ?*T" { try expect(a == b); try expect(b == a); } + +test "peer type resolution: error union switch" { + // The non-error and error cases are only peers if the error case is only a switch expression; + // the pattern `if (x) {...} else |err| blk: { switch (err) {...} }` does not consider the + // non-error and error case to be peers. + var a: error{ A, B, C }!u32 = 0; + _ = &a; + const b = if (a) |x| + x + 3 + else |err| switch (err) { + error.A => 0, + error.B => 1, + error.C => null, + }; + try expect(@TypeOf(b) == ?u32); + + // The non-error and error cases are only peers if the error case is only a switch expression; + // the pattern `x catch |err| blk: { switch (err) {...} }` does not consider the unwrapped `x` + // and error case to be peers. + const c = a catch |err| switch (err) { + error.A => 0, + error.B => 1, + error.C => null, + }; + try expect(@TypeOf(c) == ?u32); +} {#code_end#} {#header_close#} {#header_close#}