@@ -15,8 +15,6 @@ use rustc_span::symbol::Ident;
1515use rustc_span:: FileName ;
1616
1717use std:: cell:: Cell ;
18- use std:: fs:: File ;
19- use std:: io:: Write ;
2018use std:: path:: Path ;
2119
2220pub 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
423422pub 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