Skip to content

Commit f35432e

Browse files
committed
or-patterns: add syntactic tests.
1 parent a9af18b commit f35432e

File tree

4 files changed

+217
-0
lines changed

4 files changed

+217
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Test some cases where or-patterns may ostensibly be allowed but are in fact not.
2+
// This is not a semantic test. We only test parsing.
3+
4+
#![feature(or_patterns)]
5+
//~^ WARN the feature `or_patterns` is incomplete and may cause the compiler to crash
6+
7+
fn main() {}
8+
9+
// Test the `pat` macro fragment parser:
10+
macro_rules! accept_pat {
11+
($p:pat) => {}
12+
}
13+
14+
accept_pat!(p | q); //~ ERROR no rules expected the token `|`
15+
accept_pat!(| p | q); //~ ERROR no rules expected the token `|`
16+
17+
// Non-macro tests:
18+
19+
enum E { A, B }
20+
use E::*;
21+
22+
fn no_top_level_or_patterns() {
23+
// We do *not* allow or-patterns at the top level of lambdas...
24+
let _ = |A | B: E| (); //~ ERROR binary operation `|` cannot be applied to type `E`
25+
// -------- This looks like an or-pattern but is in fact `|A| (B: E | ())`.
26+
27+
// ...and for now neither do we allow or-patterns at the top level of functions.
28+
fn fun(A | B: E) {} //~ ERROR expected one of `:` or `@`, found `|`
29+
}
30+
31+
// We also do not allow a leading `|` when not in a top level position:
32+
33+
#[cfg(FALSE)]
34+
fn no_leading_parens() {
35+
let ( | A | B); //~ ERROR expected pattern, found `|`
36+
}
37+
38+
#[cfg(FALSE)]
39+
fn no_leading_tuple() {
40+
let ( | A | B,); //~ ERROR expected pattern, found `|`
41+
}
42+
43+
#[cfg(FALSE)]
44+
fn no_leading_slice() {
45+
let [ | A | B ]; //~ ERROR expected pattern, found `|`
46+
}
47+
48+
#[cfg(FALSE)]
49+
fn no_leading_tuple_struct() {
50+
let TS( | A | B ); //~ ERROR expected pattern, found `|`
51+
}
52+
53+
#[cfg(FALSE)]
54+
fn no_leading_struct() {
55+
let NS { f: | A | B }; //~ ERROR expected pattern, found `|`
56+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
error: expected one of `:` or `@`, found `|`
2+
--> $DIR/or-patterns-syntactic-fail.rs:28:14
3+
|
4+
LL | fn fun(A | B: E) {}
5+
| ^ expected one of `:` or `@` here
6+
7+
error: expected pattern, found `|`
8+
--> $DIR/or-patterns-syntactic-fail.rs:35:11
9+
|
10+
LL | let ( | A | B);
11+
| ^ expected pattern
12+
13+
error: expected pattern, found `|`
14+
--> $DIR/or-patterns-syntactic-fail.rs:40:11
15+
|
16+
LL | let ( | A | B,);
17+
| ^ expected pattern
18+
19+
error: expected pattern, found `|`
20+
--> $DIR/or-patterns-syntactic-fail.rs:45:11
21+
|
22+
LL | let [ | A | B ];
23+
| ^ expected pattern
24+
25+
error: expected pattern, found `|`
26+
--> $DIR/or-patterns-syntactic-fail.rs:50:13
27+
|
28+
LL | let TS( | A | B );
29+
| ^ expected pattern
30+
31+
error: expected pattern, found `|`
32+
--> $DIR/or-patterns-syntactic-fail.rs:55:17
33+
|
34+
LL | let NS { f: | A | B };
35+
| ^ expected pattern
36+
37+
error: no rules expected the token `|`
38+
--> $DIR/or-patterns-syntactic-fail.rs:14:15
39+
|
40+
LL | macro_rules! accept_pat {
41+
| ----------------------- when calling this macro
42+
...
43+
LL | accept_pat!(p | q);
44+
| ^ no rules expected this token in macro call
45+
46+
error: no rules expected the token `|`
47+
--> $DIR/or-patterns-syntactic-fail.rs:15:13
48+
|
49+
LL | macro_rules! accept_pat {
50+
| ----------------------- when calling this macro
51+
...
52+
LL | accept_pat!(| p | q);
53+
| ^ no rules expected this token in macro call
54+
55+
warning: the feature `or_patterns` is incomplete and may cause the compiler to crash
56+
--> $DIR/or-patterns-syntactic-fail.rs:4:12
57+
|
58+
LL | #![feature(or_patterns)]
59+
| ^^^^^^^^^^^
60+
|
61+
= note: `#[warn(incomplete_features)]` on by default
62+
63+
error[E0369]: binary operation `|` cannot be applied to type `E`
64+
--> $DIR/or-patterns-syntactic-fail.rs:24:22
65+
|
66+
LL | let _ = |A | B: E| ();
67+
| ----^ -- ()
68+
| |
69+
| E
70+
|
71+
= note: an implementation of `std::ops::BitOr` might be missing for `E`
72+
73+
error: aborting due to 9 previous errors
74+
75+
For more information about this error, try `rustc --explain E0369`.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Here we test all the places `|` is *syntactically* allowed.
2+
// This is not a semantic test. We only test parsing.
3+
4+
// check-pass
5+
6+
#![feature(or_patterns)]
7+
8+
fn main() {}
9+
10+
// Test the `pat` macro fragment parser:
11+
macro_rules! accept_pat {
12+
($p:pat) => {}
13+
}
14+
15+
accept_pat!((p | q));
16+
accept_pat!((p | q,));
17+
accept_pat!(TS(p | q));
18+
accept_pat!(NS { f: p | q });
19+
accept_pat!([p | q]);
20+
21+
// Non-macro tests:
22+
23+
#[cfg(FALSE)]
24+
fn or_patterns() {
25+
// Top level of `let`:
26+
let | A | B;
27+
let A | B;
28+
let A | B: u8;
29+
let A | B = 0;
30+
let A | B: u8 = 0;
31+
32+
// Top level of `for`:
33+
for | A | B in 0 {}
34+
for A | B in 0 {}
35+
36+
// Top level of `while`:
37+
while let | A | B = 0 {}
38+
while let A | B = 0 {}
39+
40+
// Top level of `if`:
41+
if let | A | B = 0 {}
42+
if let A | B = 0 {}
43+
44+
// Top level of `match` arms:
45+
match 0 {
46+
| A | B => {},
47+
A | B => {},
48+
}
49+
50+
// Functions:
51+
fn fun((A | B): _) {}
52+
53+
// Lambdas:
54+
let _ = |(A | B): u8| ();
55+
56+
// Parenthesis and tuple patterns:
57+
let (A | B);
58+
let (A | B,);
59+
60+
// Tuple struct patterns:
61+
let A(B | C);
62+
let E::V(B | C);
63+
64+
// Struct patterns:
65+
let S { f1: B | C, f2 };
66+
let E::V { f1: B | C, f2 };
67+
68+
// Slice patterns:
69+
let [A | B, .. | ..];
70+
71+
// These bind as `(prefix p) | q` as opposed to `prefix (p | q)`:
72+
let box 0 | 1; // Unstable; we *can* the precedence if we want.
73+
let &0 | 1;
74+
let &mut 0 | 1;
75+
let x @ 0 | 1;
76+
let ref x @ 0 | 1;
77+
let ref mut x @ 0 | 1;
78+
}
Lines changed: 8 additions & 0 deletions
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/or-patterns-syntactic-pass.rs:6:12
3+
|
4+
LL | #![feature(or_patterns)]
5+
| ^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+

0 commit comments

Comments
 (0)