@@ -15,8 +15,6 @@ use rustc_span::symbol::Ident;
15
15
use rustc_span:: FileName ;
16
16
17
17
use std:: cell:: Cell ;
18
- use std:: fs:: File ;
19
- use std:: io:: Write ;
20
18
use std:: path:: Path ;
21
19
22
20
pub use self :: PpMode :: * ;
@@ -375,13 +373,14 @@ fn get_source(input: &Input, sess: &Session) -> (String, FileName) {
375
373
( src, src_name)
376
374
}
377
375
378
- fn write_output ( out : Vec < u8 > , ofile : Option < & Path > ) {
376
+ fn write_or_print ( out : & str , ofile : Option < & Path > ) {
379
377
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
+ }
385
384
}
386
385
}
387
386
@@ -417,7 +416,7 @@ pub fn print_after_parsing(
417
416
unreachable ! ( ) ;
418
417
} ;
419
418
420
- write_output ( out. into_bytes ( ) , ofile) ;
419
+ write_or_print ( & out, ofile) ;
421
420
}
422
421
423
422
pub fn print_after_hir_lowering < ' tcx > (
@@ -477,7 +476,7 @@ pub fn print_after_hir_lowering<'tcx>(
477
476
_ => unreachable ! ( ) ,
478
477
}
479
478
480
- write_output ( out. into_bytes ( ) , ofile) ;
479
+ write_or_print ( & out, ofile) ;
481
480
}
482
481
483
482
// In an ideal world, this would be a public function called by the driver after
@@ -503,7 +502,8 @@ fn print_with_analysis(
503
502
}
504
503
. unwrap ( ) ;
505
504
506
- write_output ( out, ofile) ;
505
+ let out = std:: str:: from_utf8 ( & out) . unwrap ( ) ;
506
+ write_or_print ( out, ofile) ;
507
507
508
508
Ok ( ( ) )
509
509
}
0 commit comments