@@ -999,7 +999,18 @@ fn print_to<T>(args: fmt::Arguments<'_>, global_s: fn() -> T, label: &str)
999
999
where
1000
1000
T : Write ,
1001
1001
{
1002
- if OUTPUT_CAPTURE_USED . load ( Ordering :: Relaxed )
1002
+ if print_to_buffer_if_capture_used ( args) {
1003
+ // Successfully wrote to capture buffer.
1004
+ return ;
1005
+ }
1006
+
1007
+ if let Err ( e) = global_s ( ) . write_fmt ( args) {
1008
+ panic ! ( "failed printing to {label}: {e}" ) ;
1009
+ }
1010
+ }
1011
+
1012
+ fn print_to_buffer_if_capture_used ( args : fmt:: Arguments < ' _ > ) -> bool {
1013
+ OUTPUT_CAPTURE_USED . load ( Ordering :: Relaxed )
1003
1014
&& OUTPUT_CAPTURE . try_with ( |s| {
1004
1015
// Note that we completely remove a local sink to write to in case
1005
1016
// our printing recursively panics/prints, so the recursive
@@ -1009,14 +1020,19 @@ where
1009
1020
s. set ( Some ( w) ) ;
1010
1021
} )
1011
1022
} ) == Ok ( Some ( ( ) ) )
1012
- {
1013
- // Successfully wrote to capture buffer.
1023
+ }
1024
+
1025
+ /// Used by impl Termination for Result to print error after `main` or a test
1026
+ /// has returned. Should avoid panicking, although we can't help it if one of
1027
+ /// the Display impls inside args decides to.
1028
+ pub ( crate ) fn attempt_print_to_stderr ( args : fmt:: Arguments < ' _ > ) {
1029
+ if print_to_buffer_if_capture_used ( args) {
1014
1030
return ;
1015
1031
}
1016
1032
1017
- if let Err ( e ) = global_s ( ) . write_fmt ( args ) {
1018
- panic ! ( "failed printing to {label}: {e}" ) ;
1019
- }
1033
+ // Ignore error if the write fails, for example because stderr is already
1034
+ // closed. There is not much point panicking at this point.
1035
+ let _ = stderr ( ) . write_fmt ( args ) ;
1020
1036
}
1021
1037
1022
1038
#[ unstable(
0 commit comments