]> git.lizzy.rs Git - rust.git/blob - src/test/ui/or-patterns/basic-switchint.rs
Rollup merge of #93112 - pietroalbini:pa-cve-2022-21658-nightly, r=pietroalbini
[rust.git] / src / test / ui / or-patterns / basic-switchint.rs
1 // Test basic or-patterns when the target pattern type will be lowered to
2 // a `SwitchInt`. This will happen when the target type is an integer.
3
4 // run-pass
5
6 #[derive(Debug, PartialEq)]
7 enum MatchArm {
8     Arm(usize),
9     Wild,
10 }
11
12 #[derive(Debug)]
13 enum Foo {
14     One(usize),
15     Two(usize, usize),
16 }
17
18 fn test_foo(x: Foo) -> MatchArm {
19     match x {
20         // normal pattern.
21         Foo::One(0) | Foo::One(1) | Foo::One(2) => MatchArm::Arm(0),
22         // most simple or-pattern.
23         Foo::One(42 | 255) => MatchArm::Arm(1),
24         // multiple or-patterns for one structure.
25         Foo::Two(42 | 255, 1024 | 2048) => MatchArm::Arm(2),
26         // mix of pattern types in one or-pattern (range).
27         Foo::One(100 | 110..=120 | 210..=220) => MatchArm::Arm(3),
28         // multiple or-patterns with wild.
29         Foo::Two(0..=10 | 100..=110, 0 | _) => MatchArm::Arm(4),
30         // wild
31         _ => MatchArm::Wild,
32     }
33 }
34
35 fn main() {
36     // `Foo` tests.
37     assert_eq!(test_foo(Foo::One(0)), MatchArm::Arm(0));
38     assert_eq!(test_foo(Foo::One(42)), MatchArm::Arm(1));
39     assert_eq!(test_foo(Foo::One(43)), MatchArm::Wild);
40     assert_eq!(test_foo(Foo::One(255)), MatchArm::Arm(1));
41     assert_eq!(test_foo(Foo::One(256)), MatchArm::Wild);
42     assert_eq!(test_foo(Foo::Two(42, 1023)), MatchArm::Wild);
43     assert_eq!(test_foo(Foo::Two(255, 2048)), MatchArm::Arm(2));
44     assert_eq!(test_foo(Foo::One(100)), MatchArm::Arm(3));
45     assert_eq!(test_foo(Foo::One(115)), MatchArm::Arm(3));
46     assert_eq!(test_foo(Foo::One(105)), MatchArm::Wild);
47     assert_eq!(test_foo(Foo::One(215)), MatchArm::Arm(3));
48     assert_eq!(test_foo(Foo::One(121)), MatchArm::Wild);
49     assert_eq!(test_foo(Foo::Two(0, 42)), MatchArm::Arm(4));
50     assert_eq!(test_foo(Foo::Two(100, 0)), MatchArm::Arm(4));
51     assert_eq!(test_foo(Foo::Two(42, 0)), MatchArm::Wild);
52 }