Only early return if recovered
This commit is contained in:
parent
faa73953c0
commit
79b6ed0e08
1 changed files with 21 additions and 16 deletions
|
@ -500,8 +500,7 @@ impl<'a> Parser<'a> {
|
||||||
|
|
||||||
// Special-case "expected `;`" errors
|
// Special-case "expected `;`" errors
|
||||||
if expected.contains(&TokenType::Token(token::Semi)) {
|
if expected.contains(&TokenType::Token(token::Semi)) {
|
||||||
if self.prev_token.kind == token::Question {
|
if self.prev_token == token::Question && self.maybe_recover_from_ternary_operator() {
|
||||||
self.maybe_recover_from_ternary_operator();
|
|
||||||
return Ok(true);
|
return Ok(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1336,13 +1335,17 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Rust has no ternary operator (`cond ? then : else`). Parse it and try
|
/// Rust has no ternary operator (`cond ? then : else`). Parse it and try
|
||||||
/// to recover from it if `then` and `else` are valid expressions.
|
/// to recover from it if `then` and `else` are valid expressions. Returns
|
||||||
pub(super) fn maybe_recover_from_ternary_operator(&mut self) {
|
/// whether it was a ternary operator.
|
||||||
let snapshot = self.create_snapshot_for_diagnostic();
|
pub(super) fn maybe_recover_from_ternary_operator(&mut self) -> bool {
|
||||||
let lo = self.prev_token.span.lo();
|
if self.prev_token != token::Question {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if self.prev_token == token::Question
|
let lo = self.prev_token.span.lo();
|
||||||
&& match self.parse_expr() {
|
let snapshot = self.create_snapshot_for_diagnostic();
|
||||||
|
|
||||||
|
if match self.parse_expr() {
|
||||||
Ok(_) => true,
|
Ok(_) => true,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
err.cancel();
|
err.cancel();
|
||||||
|
@ -1350,12 +1353,12 @@ impl<'a> Parser<'a> {
|
||||||
// ascription. Catch when this happens and continue.
|
// ascription. Catch when this happens and continue.
|
||||||
self.token == token::Colon
|
self.token == token::Colon
|
||||||
}
|
}
|
||||||
}
|
} {
|
||||||
{
|
|
||||||
if self.eat_noexpect(&token::Colon) {
|
if self.eat_noexpect(&token::Colon) {
|
||||||
match self.parse_expr() {
|
match self.parse_expr() {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
self.sess.emit_err(TernaryOperator { span: self.token.span.with_lo(lo) });
|
self.sess.emit_err(TernaryOperator { span: self.token.span.with_lo(lo) });
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
err.cancel();
|
err.cancel();
|
||||||
|
@ -1366,6 +1369,8 @@ impl<'a> Parser<'a> {
|
||||||
} else {
|
} else {
|
||||||
self.restore_snapshot(snapshot);
|
self.restore_snapshot(snapshot);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn maybe_recover_from_bad_type_plus(&mut self, ty: &Ty) -> PResult<'a, ()> {
|
pub(super) fn maybe_recover_from_bad_type_plus(&mut self, ty: &Ty) -> PResult<'a, ()> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue