diff options
| author | Catherine Flores <catherine.3.flores@gmail.com> | 2023-07-25 19:15:19 +0000 |
|---|---|---|
| committer | Catherine Flores <catherine.3.flores@gmail.com> | 2023-07-25 19:17:33 +0000 |
| commit | 79b6ed0e0860dc4a75af50a975d47efabdd95cbc (patch) | |
| tree | f6890eeb5528a86f6b7d7c69e730ba22b96f65b1 /compiler/rustc_parse/src | |
| parent | faa73953c07530bc02c9a89e4d719beb3f4a376e (diff) | |
| download | rust-79b6ed0e0860dc4a75af50a975d47efabdd95cbc.tar.gz rust-79b6ed0e0860dc4a75af50a975d47efabdd95cbc.zip | |
Only early return if recovered
Diffstat (limited to 'compiler/rustc_parse/src')
| -rw-r--r-- | compiler/rustc_parse/src/parser/diagnostics.rs | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index ef5581e1efe..7d04a335c9e 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -500,8 +500,7 @@ impl<'a> Parser<'a> { // Special-case "expected `;`" errors if expected.contains(&TokenType::Token(token::Semi)) { - if self.prev_token.kind == token::Question { - self.maybe_recover_from_ternary_operator(); + if self.prev_token == token::Question && self.maybe_recover_from_ternary_operator() { return Ok(true); } @@ -1336,26 +1335,30 @@ impl<'a> Parser<'a> { } /// Rust has no ternary operator (`cond ? then : else`). Parse it and try - /// to recover from it if `then` and `else` are valid expressions. - pub(super) fn maybe_recover_from_ternary_operator(&mut self) { - let snapshot = self.create_snapshot_for_diagnostic(); + /// to recover from it if `then` and `else` are valid expressions. Returns + /// whether it was a ternary operator. + pub(super) fn maybe_recover_from_ternary_operator(&mut self) -> bool { + if self.prev_token != token::Question { + return false; + } + let lo = self.prev_token.span.lo(); + let snapshot = self.create_snapshot_for_diagnostic(); - if self.prev_token == token::Question - && match self.parse_expr() { - Ok(_) => true, - Err(err) => { - err.cancel(); - // The colon can sometimes be mistaken for type - // ascription. Catch when this happens and continue. - self.token == token::Colon - } + if match self.parse_expr() { + Ok(_) => true, + Err(err) => { + err.cancel(); + // The colon can sometimes be mistaken for type + // ascription. Catch when this happens and continue. + self.token == token::Colon } - { + } { if self.eat_noexpect(&token::Colon) { match self.parse_expr() { Ok(_) => { self.sess.emit_err(TernaryOperator { span: self.token.span.with_lo(lo) }); + return true; } Err(err) => { err.cancel(); @@ -1366,6 +1369,8 @@ impl<'a> Parser<'a> { } else { self.restore_snapshot(snapshot); }; + + false } pub(super) fn maybe_recover_from_bad_type_plus(&mut self, ty: &Ty) -> PResult<'a, ()> { |
