Skip to content

Commit 6438f4a

Browse files
authored
Rollup merge of #100787 - chenyukang:fix-100770-pretty-crash, r=petrochenkov
Pretty printing give proper error message without panic Fixes #100770
2 parents 4fd4de7 + 77eb1ae commit 6438f4a

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

compiler/rustc_driver/src/pretty.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! The various pretty-printing routines.
22
3+
use crate::session_diagnostics::UnprettyDumpFail;
34
use rustc_ast as ast;
45
use rustc_ast_pretty::pprust;
56
use rustc_errors::ErrorGuaranteed;
@@ -357,12 +358,15 @@ fn get_source(input: &Input, sess: &Session) -> (String, FileName) {
357358
(src, src_name)
358359
}
359360

360-
fn write_or_print(out: &str, ofile: Option<&Path>) {
361+
fn write_or_print(out: &str, ofile: Option<&Path>, sess: &Session) {
361362
match ofile {
362363
None => print!("{}", out),
363364
Some(p) => {
364365
if let Err(e) = std::fs::write(p, out) {
365-
panic!("print-print failed to write {} due to {}", p.display(), e);
366+
sess.emit_fatal(UnprettyDumpFail {
367+
path: p.display().to_string(),
368+
err: e.to_string(),
369+
});
366370
}
367371
}
368372
}
@@ -402,7 +406,7 @@ pub fn print_after_parsing(
402406
_ => unreachable!(),
403407
};
404408

405-
write_or_print(&out, ofile);
409+
write_or_print(&out, ofile, sess);
406410
}
407411

408412
pub fn print_after_hir_lowering<'tcx>(
@@ -468,7 +472,7 @@ pub fn print_after_hir_lowering<'tcx>(
468472
_ => unreachable!(),
469473
};
470474

471-
write_or_print(&out, ofile);
475+
write_or_print(&out, ofile, tcx.sess);
472476
}
473477

474478
// In an ideal world, this would be a public function called by the driver after
@@ -512,7 +516,7 @@ fn print_with_analysis(
512516
_ => unreachable!(),
513517
};
514518

515-
write_or_print(&out, ofile);
519+
write_or_print(&out, ofile, tcx.sess);
516520

517521
Ok(())
518522
}

compiler/rustc_driver/src/session_diagnostics.rs

+7
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ pub(crate) struct RLinkRustcVersionMismatch<'a> {
3131
#[derive(SessionDiagnostic)]
3232
#[diag(driver::rlink_no_a_file)]
3333
pub(crate) struct RlinkNotAFile;
34+
35+
#[derive(SessionDiagnostic)]
36+
#[diag(driver::unpretty_dump_fail)]
37+
pub(crate) struct UnprettyDumpFail {
38+
pub path: String,
39+
pub err: String,
40+
}

compiler/rustc_error_messages/locales/en-US/driver.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ driver_rlink_encoding_version_mismatch = .rlink file was produced with encoding
99
driver_rlink_rustc_version_mismatch = .rlink file was produced by rustc version `{$rustc_version}`, but the current version is `{$current_version}`
1010
1111
driver_rlink_no_a_file = rlink must be a file
12+
13+
driver_unpretty_dump_fail = pretty-print failed to write `{$path}` due to error `{$err}`

src/test/ui/unpretty/avoid-crash.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// normalize-stderr-test "error `.*`" -> "$$ERROR_MESSAGE"
2+
// compile-flags: -o/tmp/ -Zunpretty=ast-tree
3+
4+
fn main() {}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
error: pretty-print failed to write `/tmp/` due to $ERROR_MESSAGE
2+
3+
error: aborting due to previous error
4+

0 commit comments

Comments
 (0)