Skip to content

Improve diagnostics when list of tokens has incorrect separators #45503

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

Merged
merged 2 commits into from
Oct 28, 2017
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
18 changes: 17 additions & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,23 @@ impl<'a> Parser<'a> {
} else {
if let Err(e) = self.expect(t) {
fe(e);
break;
// Attempt to keep parsing if it was a similar separator
if let Some(ref tokens) = t.similar_tokens() {
if tokens.contains(&self.token) {
self.bump();
}
}
// Attempt to keep parsing if it was an omitted separator
match f(self) {
Ok(t) => {
v.push(t);
continue;
},
Err(mut e) => {
e.cancel();
break;
}
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/libsyntax/parse/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,16 @@ impl Token {
})
}

/// Returns tokens that are likely to be typed accidentally instead of the current token.
/// Enables better error recovery when the wrong token is found.
pub fn similar_tokens(&self) -> Option<Vec<Token>> {
match *self {
Comma => Some(vec![Dot, Lt]),
Semi => Some(vec![Colon]),
_ => None
}
}

/// Returns `true` if the token is either a special identifier or a keyword.
pub fn is_reserved_ident(&self) -> bool {
self.is_special_ident() || self.is_used_keyword() || self.is_unused_keyword()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ mod foo {
struct S1(pub(in foo) (), pub(T), pub(crate) (), pub(((), T)));
struct S2(pub((foo)) ());
//~^ ERROR expected `,`, found `(`
//~| ERROR expected one of `;` or `where`, found `(`
//~| ERROR cannot find type `foo` in this scope
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ macro_rules! define_struct {
struct S2(pub (in foo) ());
struct S3(pub $t ());
//~^ ERROR expected `,`, found `(`
//~| ERROR expected one of `;` or `where`, found `(`
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ macro_rules! define_struct {
struct S2(pub (in foo) ());
struct S3(pub($t) ());
//~^ ERROR expected `,`, found `(`
//~| ERROR expected one of `;` or `where`, found `(`
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/similar-tokens.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

mod x {
pub struct A;
pub struct B;
}

// `.` is similar to `,` so list parsing should continue to closing `}`
use x::{A. B};

fn main() {}
8 changes: 8 additions & 0 deletions src/test/ui/similar-tokens.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: expected one of `,` or `as`, found `.`
--> $DIR/similar-tokens.rs:17:10
|
17 | use x::{A. B};
| ^ expected one of `,` or `as` here

error: aborting due to previous error