about summary refs log tree commit diff
path: root/src/tools/rustfmt
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2024-02-23 13:20:33 +1100
committerNicholas Nethercote <n.nethercote@gmail.com>2024-02-23 16:09:51 +1100
commit41da3d6f2fa3ef3eb49c576d82c6879d4b336ef8 (patch)
tree3a232cf7cc06e3f50937f397e3f5864b5a1b1ae8 /src/tools/rustfmt
parentc5f69bdd5173a948e0131f934fa7c4cbf5e0b55f (diff)
downloadrust-41da3d6f2fa3ef3eb49c576d82c6879d4b336ef8.tar.gz
rust-41da3d6f2fa3ef3eb49c576d82c6879d4b336ef8.zip
Explicitly call `emit_stashed_diagnostics`.
Commit 72b172b in #121206 changed things so that
`emit_stashed_diagnostics` is only called from `run_compiler`. But
rustfmt doesn't use `run_compiler`, so it needs to call
`emit_stashed_diagnostics` itself to avoid an abort in
`DiagCtxtInner::drop` when stashed diagnostics occur.

Fixes #121450.
Diffstat (limited to 'src/tools/rustfmt')
-rw-r--r--src/tools/rustfmt/src/parse/parser.rs18
-rw-r--r--src/tools/rustfmt/src/test/parser.rs7
-rw-r--r--src/tools/rustfmt/tests/parser/stashed-diag.rs3
3 files changed, 23 insertions, 5 deletions
diff --git a/src/tools/rustfmt/src/parse/parser.rs b/src/tools/rustfmt/src/parse/parser.rs
index 31226cf8c30..cca14353b5c 100644
--- a/src/tools/rustfmt/src/parse/parser.rs
+++ b/src/tools/rustfmt/src/parse/parser.rs
@@ -163,13 +163,21 @@ impl<'a> Parser<'a> {
     fn parse_crate_mod(&mut self) -> Result<ast::Crate, ParserError> {
         let mut parser = AssertUnwindSafe(&mut self.parser);
 
-        match catch_unwind(move || parser.parse_crate_mod()) {
-            Ok(Ok(k)) => Ok(k),
-            Ok(Err(db)) => {
+        // rustfmt doesn't use `run_compiler` like other tools, so it must emit
+        // any stashed diagnostics itself, otherwise the `DiagCtxt` will assert
+        // when dropped. The final result here combines the parsing result and
+        // the `emit_stashed_diagnostics` result.
+        let parse_res = catch_unwind(move || parser.parse_crate_mod());
+        let stashed_res = self.parser.dcx().emit_stashed_diagnostics();
+        let err = Err(ParserError::ParsePanicError);
+        match (parse_res, stashed_res) {
+            (Ok(Ok(k)), None) => Ok(k),
+            (Ok(Ok(_)), Some(_guar)) => err,
+            (Ok(Err(db)), _) => {
                 db.emit();
-                Err(ParserError::ParseError)
+                err
             }
-            Err(_) => Err(ParserError::ParsePanicError),
+            (Err(_), _) => err,
         }
     }
 }
diff --git a/src/tools/rustfmt/src/test/parser.rs b/src/tools/rustfmt/src/test/parser.rs
index ae4a4f94d92..da2a2ba62e0 100644
--- a/src/tools/rustfmt/src/test/parser.rs
+++ b/src/tools/rustfmt/src/test/parser.rs
@@ -55,3 +55,10 @@ fn crate_parsing_errors_on_unclosed_delims() {
     let filename = "tests/parser/unclosed-delims/issue_4466.rs";
     assert_parser_error(filename);
 }
+
+#[test]
+fn crate_parsing_stashed_diag() {
+    // See also https://github.com/rust-lang/rust/issues/121450
+    let filename = "tests/parser/stashed-diag.rs";
+    assert_parser_error(filename);
+}
diff --git a/src/tools/rustfmt/tests/parser/stashed-diag.rs b/src/tools/rustfmt/tests/parser/stashed-diag.rs
new file mode 100644
index 00000000000..3b0b543e610
--- /dev/null
+++ b/src/tools/rustfmt/tests/parser/stashed-diag.rs
@@ -0,0 +1,3 @@
+#![u={static N;}]
+
+fn main() {}