File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff 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#}
You can’t perform that action at this time.
0 commit comments