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

Do not panic on missing close paren #58863

Closed
wants to merge 4 commits into from
Closed
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
13 changes: 11 additions & 2 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use crate::ThinVec;
use crate::tokenstream::{self, DelimSpan, TokenTree, TokenStream, TreeAndJoint};
use crate::symbol::{Symbol, keywords};

use errors::{Applicability, DiagnosticBuilder, DiagnosticId};
use errors::{Applicability, DiagnosticBuilder, DiagnosticId, FatalError};
use rustc_target::spec::abi::{self, Abi};
use syntax_pos::{Span, MultiSpan, BytePos, FileName};
use log::{debug, trace};
Expand Down Expand Up @@ -256,6 +256,7 @@ pub struct Parser<'a> {
/// it gets removed from here. Every entry left at the end gets emitted as an independent
/// error.
crate unclosed_delims: Vec<UnmatchedBrace>,
last_unexpected_token_span: Option<Span>,
}


Expand Down Expand Up @@ -582,6 +583,7 @@ impl<'a> Parser<'a> {
unmatched_angle_bracket_count: 0,
max_angle_bracket_count: 0,
unclosed_delims: Vec::new(),
last_unexpected_token_span: None,
};

let tok = parser.next_tok();
Expand Down Expand Up @@ -775,6 +777,9 @@ impl<'a> Parser<'a> {
} else if inedible.contains(&self.token) {
// leave it in the input
Ok(false)
} else if self.last_unexpected_token_span == Some(self.span) {
emit_unclosed_delims(&self.unclosed_delims, self.diagnostic());
FatalError.raise();
} else {
let mut expected = edible.iter()
.map(|x| TokenType::Token(x.clone()))
Expand Down Expand Up @@ -802,6 +807,7 @@ impl<'a> Parser<'a> {
(self.sess.source_map().next_point(self.prev_span),
format!("expected {} here", expect)))
};
self.last_unexpected_token_span = Some(self.span);
let mut err = self.fatal(&msg_exp);
if self.token.is_ident_named("and") {
err.span_suggestion_short(
Expand Down Expand Up @@ -6333,7 +6339,10 @@ impl<'a> Parser<'a> {
fn_inputs.append(&mut input);
(fn_inputs, recovered)
} else {
return self.unexpected();
match self.expect_one_of(&[], &[]) {
Err(err) => return Err(err),
Ok(recovered) => (vec![self_arg], recovered),
}
}
} else {
self.parse_seq_to_before_end(&token::CloseDelim(token::Paren), sep, parse_arg_fn)?
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/issues/issue-58856-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
impl A {
fn b(self>
//~^ ERROR expected one of `)`, `,`, or `:`, found `>`
}

fn main() {}
11 changes: 11 additions & 0 deletions src/test/ui/issues/issue-58856-1.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: expected one of `)`, `,`, or `:`, found `>`
--> $DIR/issue-58856-1.rs:2:14
|
LL | fn b(self>
| - ^
| | |
| | help: `)` may belong here
| unclosed delimiter

error: aborting due to previous error

14 changes: 14 additions & 0 deletions src/test/ui/issues/issue-58856-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
struct Empty;

trait Howness {}

impl Howness for () {
fn how_are_you(&self -> Empty {
//~^ ERROR expected one of `)` or `,`, found `->`
//~| ERROR method `how_are_you` is not a member of trait `Howness`
Empty
}
}
//~^ ERROR expected one of `async`, `const`, `crate`, `default`, `existential`, `extern`, `fn`,

fn main() {}
30 changes: 30 additions & 0 deletions src/test/ui/issues/issue-58856-2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
error: expected one of `)` or `,`, found `->`
--> $DIR/issue-58856-2.rs:6:26
|
LL | fn how_are_you(&self -> Empty {
| - -^^
| | |
| | help: `)` may belong here
| unclosed delimiter

error: expected one of `async`, `const`, `crate`, `default`, `existential`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found `)`
--> $DIR/issue-58856-2.rs:11:1
|
LL | }
| - expected one of 11 possible tokens here
LL | }
| ^ unexpected token

error[E0407]: method `how_are_you` is not a member of trait `Howness`
--> $DIR/issue-58856-2.rs:6:5
|
LL | / fn how_are_you(&self -> Empty {
LL | | //~^ ERROR expected one of `)` or `,`, found `->`
LL | | //~| ERROR method `how_are_you` is not a member of trait `Howness`
LL | | Empty
LL | | }
| |_____^ not a member of trait `Howness`

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0407`.
3 changes: 0 additions & 3 deletions src/test/ui/parser/recover-enum2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ fn main() {
// fail again
enum Test4 {
Nope(i32 {}) //~ ERROR: found `{`
//~^ ERROR: found `{`
}
}
// still recover later
let bad_syntax = _; //~ ERROR: expected expression, found reserved identifier `_`
}
14 changes: 1 addition & 13 deletions src/test/ui/parser/recover-enum2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,5 @@ error: expected one of `!`, `(`, `)`, `+`, `,`, `::`, or `<`, found `{`
LL | Nope(i32 {}) //~ ERROR: found `{`
| ^ expected one of 7 possible tokens here

error: expected one of `!`, `&&`, `&`, `(`, `)`, `*`, `+`, `,`, `...`, `::`, `<`, `?`, `[`, `_`, `crate`, `dyn`, `extern`, `fn`, `for`, `impl`, `pub`, `unsafe`, `}`, or lifetime, found `{`
--> $DIR/recover-enum2.rs:27:22
|
LL | Nope(i32 {}) //~ ERROR: found `{`
| ^ expected one of 24 possible tokens here

error: expected expression, found reserved identifier `_`
--> $DIR/recover-enum2.rs:32:22
|
LL | let bad_syntax = _; //~ ERROR: expected expression, found reserved identifier `_`
| ^ expected expression

error: aborting due to 4 previous errors
error: aborting due to 2 previous errors