|
| 1 | +// check-pass |
| 2 | +// revisions: e2018 e2021 |
| 3 | +//[e2018] edition:2018 |
| 4 | +//[e2021] edition:2021 |
| 5 | +#![feature(never_patterns)] |
| 6 | +#![allow(incomplete_features)] |
| 7 | + |
| 8 | +#[derive(Debug, PartialEq, Eq)] |
| 9 | +struct Pattern; |
| 10 | +#[derive(Debug, PartialEq, Eq)] |
| 11 | +struct Never; |
| 12 | +#[derive(Debug, PartialEq, Eq)] |
| 13 | +struct Other; |
| 14 | + |
| 15 | +macro_rules! detect_pat { |
| 16 | + ($p:pat) => { |
| 17 | + Pattern |
| 18 | + }; |
| 19 | + (!) => { |
| 20 | + Never |
| 21 | + }; |
| 22 | + ($($x:tt)*) => { |
| 23 | + Other |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +// For backwards-compatibility, all the cases that parse as `Pattern` under the feature gate must |
| 28 | +// have been parse errors before. |
| 29 | +fn main() { |
| 30 | + // For backwards compatibility this does not match `$p:pat`. |
| 31 | + assert_eq!(detect_pat!(!), Never); |
| 32 | + |
| 33 | + // Edition 2018 parses both of these cases as `Other`. Both editions have been parsing the |
| 34 | + // first case as `Other` before, so we mustn't change that. |
| 35 | + assert_eq!(detect_pat!(! | true), Other); |
| 36 | + #[cfg(e2018)] |
| 37 | + assert_eq!(detect_pat!(true | !), Other); |
| 38 | + #[cfg(e2021)] |
| 39 | + assert_eq!(detect_pat!(true | !), Pattern); |
| 40 | + |
| 41 | + // These are never patterns; they take no body when they're in a match arm. |
| 42 | + assert_eq!(detect_pat!((!)), Pattern); |
| 43 | + assert_eq!(detect_pat!((true, !)), Pattern); |
| 44 | + assert_eq!(detect_pat!(Some(!)), Pattern); |
| 45 | + |
| 46 | + // These count as normal patterns. |
| 47 | + assert_eq!(detect_pat!((! | true)), Pattern); |
| 48 | + assert_eq!(detect_pat!((Ok(x) | Err(&!))), Pattern); |
| 49 | +} |
0 commit comments