From 9678751a880bc87a9beae7585ace926306a291e6 Mon Sep 17 00:00:00 2001 From: yukang Date: Mon, 22 Aug 2022 21:45:05 +0800 Subject: [PATCH 1/2] pretty printing give proper erro message without panic --- compiler/rustc_driver/src/pretty.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_driver/src/pretty.rs b/compiler/rustc_driver/src/pretty.rs index f66b1a2976f1c..21a3a077a10c4 100644 --- a/compiler/rustc_driver/src/pretty.rs +++ b/compiler/rustc_driver/src/pretty.rs @@ -357,12 +357,16 @@ fn get_source(input: &Input, sess: &Session) -> (String, FileName) { (src, src_name) } -fn write_or_print(out: &str, ofile: Option<&Path>) { +fn write_or_print(out: &str, ofile: Option<&Path>, sess: &Session) { match ofile { None => print!("{}", out), Some(p) => { if let Err(e) = std::fs::write(p, out) { - panic!("print-print failed to write {} due to {}", p.display(), e); + let mut err = sess.struct_fatal(&format!( + "pretty-print failed to write {} due to error `{e}`", + p.display() + )); + err.emit(); } } } @@ -402,7 +406,7 @@ pub fn print_after_parsing( _ => unreachable!(), }; - write_or_print(&out, ofile); + write_or_print(&out, ofile, sess); } pub fn print_after_hir_lowering<'tcx>( @@ -468,7 +472,7 @@ pub fn print_after_hir_lowering<'tcx>( _ => unreachable!(), }; - write_or_print(&out, ofile); + write_or_print(&out, ofile, tcx.sess); } // In an ideal world, this would be a public function called by the driver after @@ -512,7 +516,7 @@ fn print_with_analysis( _ => unreachable!(), }; - write_or_print(&out, ofile); + write_or_print(&out, ofile, tcx.sess); Ok(()) } From 77eb1aef52c3ec99220347dc56f87911fb3c0198 Mon Sep 17 00:00:00 2001 From: yukang Date: Tue, 30 Aug 2022 09:31:52 +0800 Subject: [PATCH 2/2] add UI test for unpretty --- compiler/rustc_driver/src/pretty.rs | 10 +++++----- compiler/rustc_driver/src/session_diagnostics.rs | 7 +++++++ compiler/rustc_error_messages/locales/en-US/driver.ftl | 2 ++ src/test/ui/unpretty/avoid-crash.rs | 4 ++++ src/test/ui/unpretty/avoid-crash.stderr | 4 ++++ 5 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 src/test/ui/unpretty/avoid-crash.rs create mode 100644 src/test/ui/unpretty/avoid-crash.stderr diff --git a/compiler/rustc_driver/src/pretty.rs b/compiler/rustc_driver/src/pretty.rs index 21a3a077a10c4..faeacd3e41040 100644 --- a/compiler/rustc_driver/src/pretty.rs +++ b/compiler/rustc_driver/src/pretty.rs @@ -1,5 +1,6 @@ //! The various pretty-printing routines. +use crate::session_diagnostics::UnprettyDumpFail; use rustc_ast as ast; use rustc_ast_pretty::pprust; use rustc_errors::ErrorGuaranteed; @@ -362,11 +363,10 @@ fn write_or_print(out: &str, ofile: Option<&Path>, sess: &Session) { None => print!("{}", out), Some(p) => { if let Err(e) = std::fs::write(p, out) { - let mut err = sess.struct_fatal(&format!( - "pretty-print failed to write {} due to error `{e}`", - p.display() - )); - err.emit(); + sess.emit_fatal(UnprettyDumpFail { + path: p.display().to_string(), + err: e.to_string(), + }); } } } diff --git a/compiler/rustc_driver/src/session_diagnostics.rs b/compiler/rustc_driver/src/session_diagnostics.rs index fe64d0fca9b20..e9696792d051f 100644 --- a/compiler/rustc_driver/src/session_diagnostics.rs +++ b/compiler/rustc_driver/src/session_diagnostics.rs @@ -31,3 +31,10 @@ pub(crate) struct RLinkRustcVersionMismatch<'a> { #[derive(SessionDiagnostic)] #[diag(driver::rlink_no_a_file)] pub(crate) struct RlinkNotAFile; + +#[derive(SessionDiagnostic)] +#[diag(driver::unpretty_dump_fail)] +pub(crate) struct UnprettyDumpFail { + pub path: String, + pub err: String, +} diff --git a/compiler/rustc_error_messages/locales/en-US/driver.ftl b/compiler/rustc_error_messages/locales/en-US/driver.ftl index 73f084cf3290b..8ad198c86c933 100644 --- a/compiler/rustc_error_messages/locales/en-US/driver.ftl +++ b/compiler/rustc_error_messages/locales/en-US/driver.ftl @@ -9,3 +9,5 @@ driver_rlink_encoding_version_mismatch = .rlink file was produced with encoding driver_rlink_rustc_version_mismatch = .rlink file was produced by rustc version `{$rustc_version}`, but the current version is `{$current_version}` driver_rlink_no_a_file = rlink must be a file + +driver_unpretty_dump_fail = pretty-print failed to write `{$path}` due to error `{$err}` diff --git a/src/test/ui/unpretty/avoid-crash.rs b/src/test/ui/unpretty/avoid-crash.rs new file mode 100644 index 0000000000000..fd84b70d944ca --- /dev/null +++ b/src/test/ui/unpretty/avoid-crash.rs @@ -0,0 +1,4 @@ +// normalize-stderr-test "error `.*`" -> "$$ERROR_MESSAGE" +// compile-flags: -o/tmp/ -Zunpretty=ast-tree + +fn main() {} diff --git a/src/test/ui/unpretty/avoid-crash.stderr b/src/test/ui/unpretty/avoid-crash.stderr new file mode 100644 index 0000000000000..11cd3866fa869 --- /dev/null +++ b/src/test/ui/unpretty/avoid-crash.stderr @@ -0,0 +1,4 @@ +error: pretty-print failed to write `/tmp/` due to $ERROR_MESSAGE + +error: aborting due to previous error +