about summary refs log tree commit diff
path: root/compiler/rustc_parse/src/parser
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-10-27 21:11:52 +0000
committerbors <bors@rust-lang.org>2023-10-27 21:11:52 +0000
commit2f1bd0729b74787f55d4cbc7818cfd787cd43a99 (patch)
tree532937d9ad800363c5dce83071e1066f2964428a /compiler/rustc_parse/src/parser
parent59bb9505bc0d0c9fbf9b5daf052bf033b63e5cc0 (diff)
parentb9015dab9dc27e83532f08b1ddb683b37eeeac45 (diff)
downloadrust-2f1bd0729b74787f55d4cbc7818cfd787cd43a99.tar.gz
rust-2f1bd0729b74787f55d4cbc7818cfd787cd43a99.zip
Auto merge of #117294 - matthiaskrgr:rollup-xylsec7, r=matthiaskrgr
Rollup of 7 pull requests

Successful merges:

 - #116834 (Remove `rustc_symbol_mangling/messages.ftl`.)
 - #117212 (Properly restore snapshot when failing to recover parsing ternary)
 - #117246 (Fix ICE: Restrict param constraint suggestion)
 - #117247 (NVPTX: Allow PassMode::Direct for ptx kernels for now)
 - #117270 (Hide internal methods from documentation)
 - #117281 (std::thread : add SAFETY comment)
 - #117287 (fix miri target information for Test step)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_parse/src/parser')
-rw-r--r--compiler/rustc_parse/src/parser/diagnostics.rs26
1 files changed, 13 insertions, 13 deletions
diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs
index 9f8361a4b1e..7b5bb319ed8 100644
--- a/compiler/rustc_parse/src/parser/diagnostics.rs
+++ b/compiler/rustc_parse/src/parser/diagnostics.rs
@@ -504,8 +504,10 @@ impl<'a> Parser<'a> {
 
         // Special-case "expected `;`" errors
         if expected.contains(&TokenType::Token(token::Semi)) {
-            if self.prev_token == token::Question && self.maybe_recover_from_ternary_operator() {
-                return Ok(true);
+            // If the user is trying to write a ternary expression, recover it and
+            // return an Err to prevent a cascade of irrelevant diagnostics
+            if self.prev_token == token::Question && let Err(e) = self.maybe_recover_from_ternary_operator() {
+                return Err(e);
             }
 
             if self.token.span == DUMMY_SP || self.prev_token.span == DUMMY_SP {
@@ -1428,10 +1430,10 @@ 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. Returns
-    /// whether it was a ternary operator.
-    pub(super) fn maybe_recover_from_ternary_operator(&mut self) -> bool {
+    /// an err if this appears to be a ternary expression.
+    pub(super) fn maybe_recover_from_ternary_operator(&mut self) -> PResult<'a, ()> {
         if self.prev_token != token::Question {
-            return false;
+            return PResult::Ok(());
         }
 
         let lo = self.prev_token.span.lo();
@@ -1449,20 +1451,18 @@ impl<'a> Parser<'a> {
             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;
+                        return Err(self
+                            .sess
+                            .create_err(TernaryOperator { span: self.token.span.with_lo(lo) }));
                     }
                     Err(err) => {
                         err.cancel();
-                        self.restore_snapshot(snapshot);
                     }
                 };
             }
-        } else {
-            self.restore_snapshot(snapshot);
-        };
-
-        false
+        }
+        self.restore_snapshot(snapshot);
+        Ok(())
     }
 
     pub(super) fn maybe_recover_from_bad_type_plus(&mut self, ty: &Ty) -> PResult<'a, ()> {