|
| 1 | +// Test basic or-patterns when the target pattern type will be lowered to |
| 2 | +// a `Switch`. This will happen when the target type is an integer. |
| 3 | +// run-pass |
| 4 | +#![feature(or_patterns)] |
| 5 | +//~^ WARN the feature `or_patterns` is incomplete and may cause the compiler to crash |
| 6 | + |
| 7 | +#[derive(Debug, PartialEq)] |
| 8 | +enum MatchArm { |
| 9 | + Arm(usize), |
| 10 | + Wild |
| 11 | +} |
| 12 | + |
| 13 | +#[derive(Debug)] |
| 14 | +enum Foo { |
| 15 | + One(usize), |
| 16 | + Two(usize, usize), |
| 17 | +} |
| 18 | + |
| 19 | +fn test_foo(x: Foo) -> MatchArm { |
| 20 | + match x { |
| 21 | + // normal pattern. |
| 22 | + Foo::One(0) | Foo::One(1) | Foo::One(2) => MatchArm::Arm(0), |
| 23 | + // most simple or-pattern. |
| 24 | + Foo::One(42 | 255) => MatchArm::Arm(1), |
| 25 | + // multiple or-patterns for one structure. |
| 26 | + Foo::Two(42 | 255, 1024 | 2048) => MatchArm::Arm(2), |
| 27 | + // mix of pattern types in one or-pattern (range). |
| 28 | + // |
| 29 | + // FIXME(dlrobertson | Nadrieril): Fix or-pattern completeness and |
| 30 | + // unreachabilitychecks for ranges. |
| 31 | + Foo::One(100 | 110..=120 | 210..=220) => MatchArm::Arm(3), |
| 32 | + // multiple or-patterns with wild. |
| 33 | + Foo::Two(0..=10 | 100..=110, 0 | _) => MatchArm::Arm(4), |
| 34 | + // wild |
| 35 | + _ => MatchArm::Wild |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +fn main() { |
| 40 | + // `Foo` tests. |
| 41 | + assert_eq!(test_foo(Foo::One(0)), MatchArm::Arm(0)); |
| 42 | + assert_eq!(test_foo(Foo::One(42)), MatchArm::Arm(1)); |
| 43 | + assert_eq!(test_foo(Foo::One(43)), MatchArm::Wild); |
| 44 | + assert_eq!(test_foo(Foo::One(255)), MatchArm::Arm(1)); |
| 45 | + assert_eq!(test_foo(Foo::One(256)), MatchArm::Wild); |
| 46 | + assert_eq!(test_foo(Foo::Two(42, 1023)), MatchArm::Wild); |
| 47 | + assert_eq!(test_foo(Foo::Two(255, 2048)), MatchArm::Arm(2)); |
| 48 | + assert_eq!(test_foo(Foo::One(100)), MatchArm::Arm(3)); |
| 49 | + assert_eq!(test_foo(Foo::One(115)), MatchArm::Arm(3)); |
| 50 | + assert_eq!(test_foo(Foo::One(105)), MatchArm::Wild); |
| 51 | + assert_eq!(test_foo(Foo::One(215)), MatchArm::Arm(3)); |
| 52 | + assert_eq!(test_foo(Foo::One(121)), MatchArm::Wild); |
| 53 | + assert_eq!(test_foo(Foo::Two(0, 42)), MatchArm::Arm(4)); |
| 54 | + assert_eq!(test_foo(Foo::Two(100, 0)), MatchArm::Arm(4)); |
| 55 | + assert_eq!(test_foo(Foo::Two(42, 0)), MatchArm::Wild); |
| 56 | +} |
0 commit comments