Skip to content

Commit 73c8203

Browse files
committed
Throw error when encountering ... instead of .. while destructing a pattern
Added tests and stderr output
1 parent a17dd36 commit 73c8203

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/librustc_parse/parser/pat.rs

+21
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ impl<'a> Parser<'a> {
295295
// A rest pattern `..`.
296296
self.bump(); // `..`
297297
PatKind::Rest
298+
} else if self.check(&token::DotDotDot) && !self.is_pat_range_end_start(1) {
299+
self.recover_dotdotdot_rest_pat(lo)
298300
} else if let Some(form) = self.parse_range_end() {
299301
self.parse_pat_range_to(form)? // `..=X`, `...X`, or `..X`.
300302
} else if self.eat_keyword(kw::Underscore) {
@@ -362,6 +364,25 @@ impl<'a> Parser<'a> {
362364
Ok(pat)
363365
}
364366

367+
/// Recover from a typoed `...` pattern that was encountered
368+
/// Ref: Issue #70388
369+
fn recover_dotdotdot_rest_pat(&mut self, lo: Span) -> PatKind {
370+
// A typoed rest pattern `...`.
371+
self.bump(); // `...`
372+
373+
// The user probably mistook `...` for a rest pattern `..`.
374+
self.struct_span_err(lo, "unexpected `...`")
375+
.span_label(lo, "not a valid pattern")
376+
.span_suggestion_short(
377+
lo,
378+
"for a rest pattern, use `..` instead of `...`",
379+
"..".to_owned(),
380+
Applicability::MachineApplicable,
381+
)
382+
.emit();
383+
PatKind::Rest
384+
}
385+
365386
/// Try to recover the more general form `intersect ::= $pat_lhs @ $pat_rhs`.
366387
///
367388
/// Allowed binding patterns generated by `binding ::= ref? mut? $ident @ $pat_rhs`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
struct Foo(i32);
2+
3+
fn main() {
4+
let Foo(...) = Foo(0); //~ ERROR unexpected `...`
5+
let [_, ..., _] = [0, 1]; //~ ERROR unexpected `...`
6+
let _recovery_witness: () = 0; //~ ERROR mismatched types
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: unexpected `...`
2+
--> $DIR/issue-70388-recover-dotdotdot-rest-pat.rs:4:13
3+
|
4+
LL | let Foo(...) = Foo(0);
5+
| ^^^
6+
| |
7+
| not a valid pattern
8+
| help: for a rest pattern, use `..` instead of `...`
9+
10+
error: unexpected `...`
11+
--> $DIR/issue-70388-recover-dotdotdot-rest-pat.rs:5:13
12+
|
13+
LL | let [_, ..., _] = [0, 1];
14+
| ^^^
15+
| |
16+
| not a valid pattern
17+
| help: for a rest pattern, use `..` instead of `...`
18+
19+
error[E0308]: mismatched types
20+
--> $DIR/issue-70388-recover-dotdotdot-rest-pat.rs:6:33
21+
|
22+
LL | let _recovery_witness: () = 0;
23+
| -- ^ expected `()`, found integer
24+
| |
25+
| expected due to this
26+
27+
error: aborting due to 3 previous errors
28+
29+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)