|
| 1 | +// revisions: e2018 e2021 |
| 2 | +//[e2018] edition:2018 |
| 3 | +//[e2021] edition:2021 |
| 4 | +//[e2021] check-pass |
| 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 | +fn main() { |
| 28 | + // For backwards compatibility this does not match `$p:pat`. This is similar to how or-patterns |
| 29 | + // without parens don't match `$p:pat` either. |
| 30 | + assert_eq!(detect_pat!(!), Never); |
| 31 | + |
| 32 | + // Edition 2018 parses the first one as `Other`. |
| 33 | + // FIXME(never_patterns): Neither edition parses the second one as a pattern. |
| 34 | + assert_eq!(detect_pat!(true | !), Pattern); |
| 35 | + //[e2018]~^ ERROR mismatched types |
| 36 | + assert_eq!(detect_pat!(! | true), Other); |
| 37 | + |
| 38 | + // These are never patterns; they take no body when they're in a match arm. |
| 39 | + assert_eq!(detect_pat!((!)), Pattern); |
| 40 | + assert_eq!(detect_pat!((true, !)), Pattern); |
| 41 | + assert_eq!(detect_pat!(Some(!)), Pattern); |
| 42 | + |
| 43 | + // These count as normal patterns. |
| 44 | + assert_eq!(detect_pat!((! | true)), Pattern); |
| 45 | + assert_eq!(detect_pat!((Ok(x) | Err(&!))), Pattern); |
| 46 | +} |
0 commit comments