Skip to content

Commit f2e3d3f

Browse files
committed
Move OutFileName writing into rustc_session
1 parent 32cac2e commit f2e3d3f

File tree

7 files changed

+24
-22
lines changed

7 files changed

+24
-22
lines changed

compiler/rustc_driver_impl/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,3 @@ driver_impl_rlink_rustc_version_mismatch = .rlink file was produced by rustc ver
1515
driver_impl_rlink_unable_to_read = failed to read rlink file: `{$err}`
1616
1717
driver_impl_rlink_wrong_file_type = The input does not look like a .rlink file
18-
19-
driver_impl_unpretty_dump_fail = pretty-print failed to write `{$path}` due to error `{$err}`

compiler/rustc_driver_impl/src/pretty.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! The various pretty-printing routines.
22
3-
use crate::session_diagnostics::UnprettyDumpFail;
43
use rustc_ast as ast;
54
use rustc_ast_pretty::pprust;
65
use rustc_errors::ErrorGuaranteed;
@@ -358,17 +357,7 @@ fn get_source(sess: &Session) -> (String, FileName) {
358357
}
359358

360359
fn write_or_print(out: &str, sess: &Session) {
361-
match &sess.io.output_file {
362-
None | Some(OutFileName::Stdout) => print!("{out}"),
363-
Some(OutFileName::Real(p)) => {
364-
if let Err(e) = std::fs::write(p, out) {
365-
sess.emit_fatal(UnprettyDumpFail {
366-
path: p.display().to_string(),
367-
err: e.to_string(),
368-
});
369-
}
370-
}
371-
}
360+
sess.io.output_file.as_ref().unwrap_or(&OutFileName::Stdout).overwrite(out, sess);
372361
}
373362

374363
pub fn print_after_parsing(sess: &Session, krate: &ast::Crate, ppm: PpMode) {

compiler/rustc_driver_impl/src/session_diagnostics.rs

-7
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ pub(crate) struct RLinkRustcVersionMismatch<'a> {
3232
#[diag(driver_impl_rlink_no_a_file)]
3333
pub(crate) struct RlinkNotAFile;
3434

35-
#[derive(Diagnostic)]
36-
#[diag(driver_impl_unpretty_dump_fail)]
37-
pub(crate) struct UnprettyDumpFail {
38-
pub path: String,
39-
pub err: String,
40-
}
41-
4235
#[derive(Diagnostic)]
4336
#[diag(driver_impl_ice)]
4437
pub(crate) struct Ice;

compiler/rustc_session/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ session_feature_gate_error = {$explain}
2626
2727
session_file_is_not_writeable = output file {$file} is not writeable -- check its permissions
2828
29+
session_file_write_fail = failed to write `{$path}` due to error `{$err}`
30+
2931
session_hexadecimal_float_literal_not_supported = hexadecimal float literal is not supported
3032
3133
session_incompatible_linker_flavor = linker flavor `{$flavor}` is incompatible with the current target

compiler/rustc_session/src/config.rs

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
pub use crate::options::*;
55

6+
use crate::errors::FileWriteFail;
67
use crate::search_paths::SearchPath;
78
use crate::utils::{CanonicalizedPath, NativeLib, NativeLibKind};
89
use crate::{lint, HashStableContext};
@@ -31,6 +32,7 @@ use std::collections::btree_map::{
3132
use std::collections::{BTreeMap, BTreeSet};
3233
use std::ffi::OsStr;
3334
use std::fmt;
35+
use std::fs;
3436
use std::hash::Hash;
3537
use std::iter;
3638
use std::path::{Path, PathBuf};
@@ -861,6 +863,17 @@ impl OutFileName {
861863
OutFileName::Stdout => outputs.temp_path(flavor, codegen_unit_name),
862864
}
863865
}
866+
867+
pub fn overwrite(&self, content: &str, sess: &Session) {
868+
match self {
869+
OutFileName::Stdout => print!("{content}"),
870+
OutFileName::Real(path) => {
871+
if let Err(e) = fs::write(path, content) {
872+
sess.emit_fatal(FileWriteFail { path, err: e.to_string() });
873+
}
874+
}
875+
}
876+
}
864877
}
865878

866879
#[derive(Clone, Hash, Debug, HashStable_Generic)]

compiler/rustc_session/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ pub struct FileIsNotWriteable<'a> {
163163
pub file: &'a std::path::Path,
164164
}
165165

166+
#[derive(Diagnostic)]
167+
#[diag(session_file_write_fail)]
168+
pub(crate) struct FileWriteFail<'a> {
169+
pub path: &'a std::path::Path,
170+
pub err: String,
171+
}
172+
166173
#[derive(Diagnostic)]
167174
#[diag(session_crate_name_does_not_match)]
168175
pub struct CrateNameDoesNotMatch {

tests/ui/unpretty/avoid-crash.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: pretty-print failed to write `/tmp/` due to $ERROR_MESSAGE
1+
error: failed to write `/tmp/` due to $ERROR_MESSAGE
22

33
error: aborting due to previous error
44

0 commit comments

Comments
 (0)