Skip to content

Commit 62ecfd3

Browse files
committed
Implement -o in rustc_driver_impl using pretty::write_or_print
1 parent a32f1ae commit 62ecfd3

File tree

4 files changed

+14
-29
lines changed

4 files changed

+14
-29
lines changed

compiler/rustc_driver_impl/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ 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
1818
19-
driver_impl_unpretty_dump_fail = pretty-print failed to write `{$path}` due to error `{$err}`
19+
driver_impl_unpretty_dump_fail = failed to write `{$path}` due to error `{$err}`

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ use std::cmp::max;
5151
use std::collections::BTreeMap;
5252
use std::env;
5353
use std::ffi::OsString;
54-
use std::fmt;
55-
use std::fs::{self, File};
54+
use std::fmt::Write as _;
55+
use std::fs;
5656
use std::io::{self, IsTerminal, Read, Write};
5757
use std::panic::{self, catch_unwind};
5858
use std::path::PathBuf;
@@ -750,30 +750,12 @@ fn print_crate_info(
750750
None
751751
};
752752

753-
let mut output_io: Box<dyn Write> = match &sess.io.output_file {
754-
Some(OutFileName::Real(output_file_path)) => match File::create(output_file_path) {
755-
Ok(output_file) => Box::new(output_file),
756-
Err(err) => handler.early_error(format!(
757-
"failed to create {}: {}",
758-
output_file_path.display(),
759-
err,
760-
)),
761-
},
762-
None | Some(OutFileName::Stdout) => Box::new(io::stdout()),
763-
};
764-
765-
fn write_output(output_io: &mut dyn Write, args: fmt::Arguments<'_>) {
766-
if let Err(_) = output_io.write_fmt(args) {
767-
rustc_errors::FatalError.raise();
768-
}
769-
}
770-
771-
macro_rules! println_info {
772-
($($arg:tt)*) => {
773-
write_output(&mut *output_io, format_args!("{}\n", format_args!($($arg)*)))
774-
};
753+
let mut crate_info = String::new();
754+
macro println_info($($arg:tt)*) {
755+
crate_info.write_fmt(format_args!("{}\n", format_args!($($arg)*))).unwrap()
775756
}
776757

758+
let mut how_to_proceed = Compilation::Stop;
777759
for req in &sess.opts.prints {
778760
match *req {
779761
TargetList => {
@@ -798,7 +780,8 @@ fn print_crate_info(
798780
FileNames | CrateName => {
799781
let Some(attrs) = attrs.as_ref() else {
800782
// no crate attributes, print out an error and exit
801-
return Compilation::Continue;
783+
how_to_proceed = Compilation::Continue;
784+
break;
802785
};
803786
let t_outputs = rustc_interface::util::build_output_filenames(attrs, sess);
804787
let id = rustc_session::output::find_crate_name(sess, attrs);
@@ -888,7 +871,9 @@ fn print_crate_info(
888871
}
889872
}
890873
}
891-
Compilation::Stop
874+
875+
pretty::write_or_print(&crate_info, sess);
876+
how_to_proceed
892877
}
893878

894879
/// Prints version information

compiler/rustc_driver_impl/src/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ fn get_source(sess: &Session) -> (String, FileName) {
357357
(src, src_name)
358358
}
359359

360-
fn write_or_print(out: &str, sess: &Session) {
360+
pub fn write_or_print(out: &str, sess: &Session) {
361361
match &sess.io.output_file {
362362
None | Some(OutFileName::Stdout) => print!("{out}"),
363363
Some(OutFileName::Real(p)) => {

tests/ui/unpretty/avoid-crash.stderr

Lines changed: 1 addition & 1 deletion
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)