| ... | ... | @@ -3861,6 +3861,32 @@ test "if error union" { |
| 3861 | 3861 | unreachable; |
| 3862 | 3862 | } |
| 3863 | 3863 | } |
| 3864 | |
| 3865 | test "if error union with optional" { |
| 3866 | // If expressions test for errors before unwrapping optionals. |
| 3867 | // The |optional_value| capture's type is ?u32. |
| 3868 | |
| 3869 | const a: anyerror!?u32 = 0; |
| 3870 | if (a) |optional_value| { |
| 3871 | assert(optional_value.? == 0); |
| 3872 | } else |err| { |
| 3873 | unreachable; |
| 3874 | } |
| 3875 | |
| 3876 | const b: anyerror!?u32 = null; |
| 3877 | if (b) |optional_value| { |
| 3878 | assert(optional_value == null); |
| 3879 | } else |err| { |
| 3880 | unreachable; |
| 3881 | } |
| 3882 | |
| 3883 | const c: anyerror!?u32 = error.BadValue; |
| 3884 | if (c) |optional_value| { |
| 3885 | unreachable; |
| 3886 | } else |err| { |
| 3887 | assert(err == error.BadValue); |
| 3888 | } |
| 3889 | } |
| 3864 | 3890 | {#code_end#} |
| 3865 | 3891 | {#see_also|Optionals|Errors#} |
| 3866 | 3892 | {#header_close#} |