Skip to content

Commit 91c10f8

Browse files
authored
Rollup merge of rust-lang#63111 - Centril:rest-pat-tests, r=estebank
Add syntactic and semantic tests for rest patterns, i.e. `..` As per my first note in rust-lang#62254 (comment) this adds syntactic and semantic tests for `..` ("rest") patterns which were implemented in rust-lang#62550. r? @estebank
2 parents 2becb62 + 6c7613b commit 91c10f8

File tree

3 files changed

+340
-0
lines changed

3 files changed

+340
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Here we test that rest patterns, i.e. `..`, are not allowed
2+
// outside of slice (+ ident patterns witin those), tuple,
3+
// and tuple struct patterns and that duplicates are caught in these contexts.
4+
5+
#![feature(slice_patterns, box_patterns)]
6+
7+
fn main() {}
8+
9+
macro_rules! mk_pat {
10+
() => { .. } //~ ERROR `..` patterns are not allowed here
11+
}
12+
13+
fn rest_patterns() {
14+
let mk_pat!();
15+
16+
// Top level:
17+
fn foo(..: u8) {} //~ ERROR `..` patterns are not allowed here
18+
let ..; //~ ERROR `..` patterns are not allowed here
19+
20+
// Box patterns:
21+
let box ..; //~ ERROR `..` patterns are not allowed here
22+
23+
// In or-patterns:
24+
match 1 {
25+
1 | .. => {} //~ ERROR `..` patterns are not allowed here
26+
}
27+
28+
// Ref patterns:
29+
let &..; //~ ERROR `..` patterns are not allowed here
30+
let &mut ..; //~ ERROR `..` patterns are not allowed here
31+
32+
// Ident patterns:
33+
let x @ ..; //~ ERROR `..` patterns are not allowed here
34+
let ref x @ ..; //~ ERROR `..` patterns are not allowed here
35+
let ref mut x @ ..; //~ ERROR `..` patterns are not allowed here
36+
37+
// Tuple:
38+
let (..): (u8,); // OK.
39+
let (..,): (u8,); // OK.
40+
let (
41+
..,
42+
.., //~ ERROR `..` can only be used once per tuple pattern
43+
.. //~ ERROR `..` can only be used once per tuple pattern
44+
): (u8, u8, u8);
45+
let (
46+
..,
47+
x,
48+
.. //~ ERROR `..` can only be used once per tuple pattern
49+
): (u8, u8, u8);
50+
51+
struct A(u8, u8, u8);
52+
53+
// Tuple struct (same idea as for tuple patterns):
54+
let A(..); // OK.
55+
let A(..,); // OK.
56+
let A(
57+
..,
58+
.., //~ ERROR `..` can only be used once per tuple struct pattern
59+
.. //~ ERROR `..` can only be used once per tuple struct pattern
60+
);
61+
let A(
62+
..,
63+
x,
64+
.. //~ ERROR `..` can only be used once per tuple struct pattern
65+
);
66+
67+
// Array/Slice:
68+
let [..]: &[u8]; // OK.
69+
let [..,]: &[u8]; // OK.
70+
let [
71+
..,
72+
.., //~ ERROR `..` can only be used once per slice pattern
73+
.. //~ ERROR `..` can only be used once per slice pattern
74+
]: &[u8];
75+
let [
76+
..,
77+
ref x @ .., //~ ERROR `..` can only be used once per slice pattern
78+
ref mut y @ .., //~ ERROR `..` can only be used once per slice pattern
79+
(ref z @ ..), //~ ERROR `..` patterns are not allowed here
80+
.. //~ ERROR `..` can only be used once per slice pattern
81+
]: &[u8];
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
error: `..` patterns are not allowed here
2+
--> $DIR/rest-pat-semantic-disallowed.rs:10:13
3+
|
4+
LL | () => { .. }
5+
| ^^
6+
...
7+
LL | let mk_pat!();
8+
| --------- in this macro invocation
9+
|
10+
= note: only allowed in tuple, tuple struct, and slice patterns
11+
12+
error: `..` patterns are not allowed here
13+
--> $DIR/rest-pat-semantic-disallowed.rs:18:9
14+
|
15+
LL | let ..;
16+
| ^^
17+
|
18+
= note: only allowed in tuple, tuple struct, and slice patterns
19+
20+
error: `..` patterns are not allowed here
21+
--> $DIR/rest-pat-semantic-disallowed.rs:21:13
22+
|
23+
LL | let box ..;
24+
| ^^
25+
|
26+
= note: only allowed in tuple, tuple struct, and slice patterns
27+
28+
error: `..` patterns are not allowed here
29+
--> $DIR/rest-pat-semantic-disallowed.rs:25:13
30+
|
31+
LL | 1 | .. => {}
32+
| ^^
33+
|
34+
= note: only allowed in tuple, tuple struct, and slice patterns
35+
36+
error: `..` patterns are not allowed here
37+
--> $DIR/rest-pat-semantic-disallowed.rs:29:10
38+
|
39+
LL | let &..;
40+
| ^^
41+
|
42+
= note: only allowed in tuple, tuple struct, and slice patterns
43+
44+
error: `..` patterns are not allowed here
45+
--> $DIR/rest-pat-semantic-disallowed.rs:30:14
46+
|
47+
LL | let &mut ..;
48+
| ^^
49+
|
50+
= note: only allowed in tuple, tuple struct, and slice patterns
51+
52+
error: `..` patterns are not allowed here
53+
--> $DIR/rest-pat-semantic-disallowed.rs:33:13
54+
|
55+
LL | let x @ ..;
56+
| ^^
57+
|
58+
= note: only allowed in tuple, tuple struct, and slice patterns
59+
60+
error: `..` patterns are not allowed here
61+
--> $DIR/rest-pat-semantic-disallowed.rs:34:17
62+
|
63+
LL | let ref x @ ..;
64+
| ^^
65+
|
66+
= note: only allowed in tuple, tuple struct, and slice patterns
67+
68+
error: `..` patterns are not allowed here
69+
--> $DIR/rest-pat-semantic-disallowed.rs:35:21
70+
|
71+
LL | let ref mut x @ ..;
72+
| ^^
73+
|
74+
= note: only allowed in tuple, tuple struct, and slice patterns
75+
76+
error: `..` can only be used once per tuple pattern
77+
--> $DIR/rest-pat-semantic-disallowed.rs:42:9
78+
|
79+
LL | ..,
80+
| -- previously used here
81+
LL | ..,
82+
| ^^ can only be used once per tuple pattern
83+
84+
error: `..` can only be used once per tuple pattern
85+
--> $DIR/rest-pat-semantic-disallowed.rs:43:9
86+
|
87+
LL | ..,
88+
| -- previously used here
89+
LL | ..,
90+
LL | ..
91+
| ^^ can only be used once per tuple pattern
92+
93+
error: `..` can only be used once per tuple pattern
94+
--> $DIR/rest-pat-semantic-disallowed.rs:48:9
95+
|
96+
LL | ..,
97+
| -- previously used here
98+
LL | x,
99+
LL | ..
100+
| ^^ can only be used once per tuple pattern
101+
102+
error: `..` can only be used once per tuple struct pattern
103+
--> $DIR/rest-pat-semantic-disallowed.rs:58:9
104+
|
105+
LL | ..,
106+
| -- previously used here
107+
LL | ..,
108+
| ^^ can only be used once per tuple struct pattern
109+
110+
error: `..` can only be used once per tuple struct pattern
111+
--> $DIR/rest-pat-semantic-disallowed.rs:59:9
112+
|
113+
LL | ..,
114+
| -- previously used here
115+
LL | ..,
116+
LL | ..
117+
| ^^ can only be used once per tuple struct pattern
118+
119+
error: `..` can only be used once per tuple struct pattern
120+
--> $DIR/rest-pat-semantic-disallowed.rs:64:9
121+
|
122+
LL | ..,
123+
| -- previously used here
124+
LL | x,
125+
LL | ..
126+
| ^^ can only be used once per tuple struct pattern
127+
128+
error: `..` can only be used once per slice pattern
129+
--> $DIR/rest-pat-semantic-disallowed.rs:72:9
130+
|
131+
LL | ..,
132+
| -- previously used here
133+
LL | ..,
134+
| ^^ can only be used once per slice pattern
135+
136+
error: `..` can only be used once per slice pattern
137+
--> $DIR/rest-pat-semantic-disallowed.rs:73:9
138+
|
139+
LL | ..,
140+
| -- previously used here
141+
LL | ..,
142+
LL | ..
143+
| ^^ can only be used once per slice pattern
144+
145+
error: `..` can only be used once per slice pattern
146+
--> $DIR/rest-pat-semantic-disallowed.rs:77:17
147+
|
148+
LL | ..,
149+
| -- previously used here
150+
LL | ref x @ ..,
151+
| ^^ can only be used once per slice pattern
152+
153+
error: `..` can only be used once per slice pattern
154+
--> $DIR/rest-pat-semantic-disallowed.rs:78:21
155+
|
156+
LL | ..,
157+
| -- previously used here
158+
LL | ref x @ ..,
159+
LL | ref mut y @ ..,
160+
| ^^ can only be used once per slice pattern
161+
162+
error: `..` patterns are not allowed here
163+
--> $DIR/rest-pat-semantic-disallowed.rs:79:18
164+
|
165+
LL | (ref z @ ..),
166+
| ^^
167+
|
168+
= note: only allowed in tuple, tuple struct, and slice patterns
169+
170+
error: `..` can only be used once per slice pattern
171+
--> $DIR/rest-pat-semantic-disallowed.rs:80:9
172+
|
173+
LL | ..,
174+
| -- previously used here
175+
...
176+
LL | ..
177+
| ^^ can only be used once per slice pattern
178+
179+
error: `..` patterns are not allowed here
180+
--> $DIR/rest-pat-semantic-disallowed.rs:17:12
181+
|
182+
LL | fn foo(..: u8) {}
183+
| ^^
184+
|
185+
= note: only allowed in tuple, tuple struct, and slice patterns
186+
187+
error: aborting due to 22 previous errors
188+
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Here we test that `..` is allowed in all pattern locations *syntactically*.
2+
// The semantic test is in `rest-pat-semantic-disallowed.rs`.
3+
4+
// check-pass
5+
6+
fn main() {}
7+
8+
macro_rules! accept_pat {
9+
($p:pat) => {}
10+
}
11+
12+
accept_pat!(..);
13+
14+
#[cfg(FALSE)]
15+
fn rest_patterns() {
16+
// Top level:
17+
fn foo(..: u8) {}
18+
let ..;
19+
20+
// Box patterns:
21+
let box ..;
22+
23+
// In or-patterns:
24+
match x {
25+
.. | .. => {}
26+
}
27+
28+
// Ref patterns:
29+
let &..;
30+
let &mut ..;
31+
32+
// Ident patterns:
33+
let x @ ..;
34+
let ref x @ ..;
35+
let ref mut x @ ..;
36+
37+
// Tuple:
38+
let (..); // This is interpreted as a tuple pattern, not a parenthesis one.
39+
let (..,); // Allowing trailing comma.
40+
let (.., .., ..); // Duplicates also.
41+
let (.., P, ..); // Including with things in between.
42+
43+
// Tuple struct (same idea as for tuple patterns):
44+
let A(..);
45+
let A(..,);
46+
let A(.., .., ..);
47+
let A(.., P, ..);
48+
49+
// Array/Slice (like with tuple patterns):
50+
let [..];
51+
let [..,];
52+
let [.., .., ..];
53+
let [.., P, ..];
54+
55+
// Random walk to guard against special casing:
56+
match x {
57+
.. |
58+
[
59+
(
60+
box ..,
61+
&(..),
62+
&mut ..,
63+
x @ ..
64+
),
65+
ref x @ ..,
66+
] |
67+
ref mut x @ ..
68+
=> {}
69+
}
70+
}

0 commit comments

Comments
 (0)