Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: recover on ... as a pattern, suggesting .. #70417

Merged
merged 1 commit into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/librustc_parse/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ impl<'a> Parser<'a> {
// A rest pattern `..`.
self.bump(); // `..`
PatKind::Rest
} else if self.check(&token::DotDotDot) && !self.is_pat_range_end_start(1) {
self.recover_dotdotdot_rest_pat(lo)
} else if let Some(form) = self.parse_range_end() {
self.parse_pat_range_to(form)? // `..=X`, `...X`, or `..X`.
} else if self.eat_keyword(kw::Underscore) {
Expand Down Expand Up @@ -362,6 +364,25 @@ impl<'a> Parser<'a> {
Ok(pat)
}

/// Recover from a typoed `...` pattern that was encountered
/// Ref: Issue #70388
fn recover_dotdotdot_rest_pat(&mut self, lo: Span) -> PatKind {
// A typoed rest pattern `...`.
self.bump(); // `...`

// The user probably mistook `...` for a rest pattern `..`.
self.struct_span_err(lo, "unexpected `...`")
.span_label(lo, "not a valid pattern")
.span_suggestion_short(
lo,
"for a rest pattern, use `..` instead of `...`",
"..".to_owned(),
Applicability::MachineApplicable,
)
.emit();
PatKind::Rest
}

/// Try to recover the more general form `intersect ::= $pat_lhs @ $pat_rhs`.
///
/// Allowed binding patterns generated by `binding ::= ref? mut? $ident @ $pat_rhs`
Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/parser/issue-70388-recover-dotdotdot-rest-pat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
struct Foo(i32);

fn main() {
let Foo(...) = Foo(0); //~ ERROR unexpected `...`
let [_, ..., _] = [0, 1]; //~ ERROR unexpected `...`
let _recovery_witness: () = 0; //~ ERROR mismatched types
}
29 changes: 29 additions & 0 deletions src/test/ui/parser/issue-70388-recover-dotdotdot-rest-pat.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
error: unexpected `...`
--> $DIR/issue-70388-recover-dotdotdot-rest-pat.rs:4:13
|
LL | let Foo(...) = Foo(0);
| ^^^
| |
| not a valid pattern
| help: for a rest pattern, use `..` instead of `...`

error: unexpected `...`
--> $DIR/issue-70388-recover-dotdotdot-rest-pat.rs:5:13
|
LL | let [_, ..., _] = [0, 1];
| ^^^
| |
| not a valid pattern
| help: for a rest pattern, use `..` instead of `...`

error[E0308]: mismatched types
--> $DIR/issue-70388-recover-dotdotdot-rest-pat.rs:6:33
|
LL | let _recovery_witness: () = 0;
| -- ^ expected `()`, found integer
| |
| expected due to this

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0308`.