Skip to content

Pretty printing give proper error message without panic #100787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions compiler/rustc_driver/src/pretty.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -357,12 +358,15 @@ 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);
sess.emit_fatal(UnprettyDumpFail {
path: p.display().to_string(),
err: e.to_string(),
});
}
}
}
Expand Down Expand Up @@ -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>(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -512,7 +516,7 @@ fn print_with_analysis(
_ => unreachable!(),
};

write_or_print(&out, ofile);
write_or_print(&out, ofile, tcx.sess);

Ok(())
}
7 changes: 7 additions & 0 deletions compiler/rustc_driver/src/session_diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
2 changes: 2 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/driver.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
4 changes: 4 additions & 0 deletions src/test/ui/unpretty/avoid-crash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// normalize-stderr-test "error `.*`" -> "$$ERROR_MESSAGE"
// compile-flags: -o/tmp/ -Zunpretty=ast-tree

fn main() {}
4 changes: 4 additions & 0 deletions src/test/ui/unpretty/avoid-crash.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
error: pretty-print failed to write `/tmp/` due to $ERROR_MESSAGE

error: aborting due to previous error