Skip to content

Commit 1bf639a

Browse files
committed
basic run-pass tests for or-patterns
Add some basic run-pass ui tests for or-patterns.
1 parent 619fc1b commit 1bf639a

8 files changed

+202
-0
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Test basic or-patterns when the target pattern type will be lowered to a
2+
// `SwitchInt` (an `enum`).
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)]
8+
enum Test {
9+
Foo,
10+
Bar,
11+
Baz,
12+
Qux
13+
}
14+
15+
fn test(x: Option<Test>) -> bool {
16+
match x {
17+
// most simple case
18+
Some(Test::Bar | Test::Qux) => true,
19+
// wild case
20+
Some(_) => false,
21+
// empty case
22+
None => false,
23+
}
24+
}
25+
26+
fn main() {
27+
assert!(!test(Some(Test::Foo)));
28+
assert!(test(Some(Test::Bar)));
29+
assert!(!test(Some(Test::Baz)));
30+
assert!(test(Some(Test::Qux)));
31+
assert!(!test(None))
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
warning: the feature `or_patterns` is incomplete and may cause the compiler to crash
2+
--> $DIR/basic-switch.rs:4:12
3+
|
4+
LL | #![feature(or_patterns)]
5+
| ^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
warning: the feature `or_patterns` is incomplete and may cause the compiler to crash
2+
--> $DIR/basic-switchint.rs:4:12
3+
|
4+
LL | #![feature(or_patterns)]
5+
| ^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
9+
warning: unreachable pattern
10+
--> $DIR/basic-switchint.rs:31:9
11+
|
12+
LL | Foo::One(100 | 110..=120 | 210..=220) => MatchArm::Arm(3),
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
= note: `#[warn(unreachable_patterns)]` on by default
16+
17+
warning: unreachable pattern
18+
--> $DIR/basic-switchint.rs:33:9
19+
|
20+
LL | Foo::Two(0..=10 | 100..=110, 0 | _) => MatchArm::Arm(4),
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Test that an or-pattern works with a wild pattern. This tests two things:
2+
//
3+
// 1) The Wild pattern should cause the pattern to always succeed.
4+
// 2) or-patterns should work with simplifyable patterns.
5+
//
6+
// run-pass
7+
#![feature(or_patterns)]
8+
//~^ WARN the feature `or_patterns` is incomplete and may cause the compiler to crash
9+
10+
pub fn test(x: Option<usize>) -> bool {
11+
match x {
12+
Some(0 | _) => true,
13+
_ => false
14+
}
15+
}
16+
17+
fn main() {
18+
assert!(test(Some(42)));
19+
assert!(!test(None));
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
warning: the feature `or_patterns` is incomplete and may cause the compiler to crash
2+
--> $DIR/mix-with-wild.rs:7:12
3+
|
4+
LL | #![feature(or_patterns)]
5+
| ^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// run-pass
2+
#![feature(or_patterns)]
3+
//~^ WARN the feature `or_patterns` is incomplete and may cause the compiler to crash
4+
5+
#[derive(Debug)]
6+
enum Other {
7+
One,
8+
Two,
9+
Three,
10+
}
11+
12+
#[derive(Debug)]
13+
enum Test {
14+
Foo { first: usize, second: usize },
15+
Bar { other: Option<Other> },
16+
Baz,
17+
}
18+
19+
fn test(x: Option<Test>) -> bool {
20+
match x {
21+
Some(
22+
Test::Foo {
23+
first: 1024 | 2048,
24+
second: 2048 | 4096
25+
} |
26+
Test::Bar { other: Some(
27+
Other::One |
28+
Other::Two
29+
)}
30+
) => true,
31+
// wild case
32+
Some(_) => false,
33+
// empty case
34+
None => false,
35+
}
36+
}
37+
38+
fn main() {
39+
assert!(test(Some(Test::Foo { first: 1024, second: 4096 })));
40+
assert!(!test(Some(Test::Foo { first: 2048, second: 8192 })));
41+
assert!(!test(Some(Test::Foo { first: 42, second: 2048 })));
42+
assert!(test(Some(Test::Bar { other: Some(Other::One) })));
43+
assert!(test(Some(Test::Bar { other: Some(Other::Two) })));
44+
assert!(!test(Some(Test::Bar { other: Some(Other::Three) })));
45+
assert!(!test(Some(Test::Bar { other: None })));
46+
assert!(!test(Some(Test::Baz)));
47+
assert!(!test(None));
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
warning: the feature `or_patterns` is incomplete and may cause the compiler to crash
2+
--> $DIR/struct-like.rs:2:12
3+
|
4+
LL | #![feature(or_patterns)]
5+
| ^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+

0 commit comments

Comments
 (0)