Skip to content

Commit e527def

Browse files
committed
Replace File::create and write_all with fs::write
Also don't convert to u8 buffers and back when we are only creating strings.
1 parent 42a4673 commit e527def

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

compiler/rustc_driver/src/pretty.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ use rustc_span::symbol::Ident;
1515
use rustc_span::FileName;
1616

1717
use std::cell::Cell;
18-
use std::fs::File;
19-
use std::io::Write;
2018
use std::path::Path;
2119

2220
pub use self::PpMode::*;
@@ -375,13 +373,14 @@ fn get_source(input: &Input, sess: &Session) -> (String, FileName) {
375373
(src, src_name)
376374
}
377375

378-
fn write_output(out: Vec<u8>, ofile: Option<&Path>) {
376+
fn write_or_print(out: &str, ofile: Option<&Path>) {
379377
match ofile {
380-
None => print!("{}", String::from_utf8(out).unwrap()),
381-
Some(p) => match File::create(p) {
382-
Ok(mut w) => w.write_all(&out).unwrap(),
383-
Err(e) => panic!("print-print failed to open {} due to {}", p.display(), e),
384-
},
378+
None => print!("{}", out),
379+
Some(p) => {
380+
if let Err(e) = std::fs::write(p, out) {
381+
panic!("print-print failed to write {} due to {}", p.display(), e);
382+
}
383+
}
385384
}
386385
}
387386

@@ -417,7 +416,7 @@ pub fn print_after_parsing(
417416
unreachable!();
418417
};
419418

420-
write_output(out.into_bytes(), ofile);
419+
write_or_print(&out, ofile);
421420
}
422421

423422
pub fn print_after_hir_lowering<'tcx>(
@@ -477,7 +476,7 @@ pub fn print_after_hir_lowering<'tcx>(
477476
_ => unreachable!(),
478477
}
479478

480-
write_output(out.into_bytes(), ofile);
479+
write_or_print(&out, ofile);
481480
}
482481

483482
// In an ideal world, this would be a public function called by the driver after
@@ -503,7 +502,8 @@ fn print_with_analysis(
503502
}
504503
.unwrap();
505504

506-
write_output(out, ofile);
505+
let out = std::str::from_utf8(&out).unwrap();
506+
write_or_print(out, ofile);
507507

508508
Ok(())
509509
}

0 commit comments

Comments
 (0)