11//@ run-pass
2+ //@ needs-unwind
23
34#![ feature( io_error_uncategorized) ]
45
56use std:: fmt;
67use std:: io:: { self , Error , Write , sink} ;
8+ use std:: panic:: catch_unwind;
79
810struct ErrorDisplay ;
911
@@ -15,7 +17,6 @@ impl fmt::Display for ErrorDisplay {
1517
1618struct ErrorWriter ;
1719
18- const FORMAT_ERROR : io:: ErrorKind = io:: ErrorKind :: Uncategorized ;
1920const WRITER_ERROR : io:: ErrorKind = io:: ErrorKind :: NotConnected ;
2021
2122impl Write for ErrorWriter {
@@ -27,22 +28,28 @@ impl Write for ErrorWriter {
2728}
2829
2930fn main ( ) {
30- // Test that the error from the formatter is propagated.
31- let res = write ! ( sink( ) , "{} {} {}" , 1 , ErrorDisplay , "bar" ) ;
32- assert ! ( res. is_err( ) , "formatter error did not propagate" ) ;
33- assert_eq ! ( res. unwrap_err( ) . kind( ) , FORMAT_ERROR ) ;
34-
3531 // Test that an underlying error is propagated
3632 let res = write ! ( ErrorWriter , "abc" ) ;
3733 assert ! ( res. is_err( ) , "writer error did not propagate" ) ;
3834
39- // Writer error
35+ // Test that the error from the formatter is detected.
36+ let res = catch_unwind ( || write ! ( sink( ) , "{} {} {}" , 1 , ErrorDisplay , "bar" ) ) ;
37+ let err = res. expect_err ( "formatter error did not lead to panic" ) . downcast :: < & str > ( ) . unwrap ( ) ;
38+ assert ! (
39+ err. contains( "formatting trait implementation returned an error" ) ,
40+ "unexpected panic: {}" , err
41+ ) ;
42+
43+ // Writer error when there's some string before the first `{}`
4044 let res = write ! ( ErrorWriter , "abc {}" , ErrorDisplay ) ;
4145 assert ! ( res. is_err( ) , "writer error did not propagate" ) ;
4246 assert_eq ! ( res. unwrap_err( ) . kind( ) , WRITER_ERROR ) ;
4347
44- // Formatter error
45- let res = write ! ( ErrorWriter , "{} abc" , ErrorDisplay ) ;
46- assert ! ( res. is_err( ) , "formatter error did not propagate" ) ;
47- assert_eq ! ( res. unwrap_err( ) . kind( ) , FORMAT_ERROR ) ;
48+ // Formatter error when the `{}` comes first
49+ let res = catch_unwind ( || write ! ( ErrorWriter , "{} abc" , ErrorDisplay ) ) ;
50+ let err = res. expect_err ( "formatter error did not lead to panic" ) . downcast :: < & str > ( ) . unwrap ( ) ;
51+ assert ! (
52+ err. contains( "formatting trait implementation returned an error" ) ,
53+ "unexpected panic: {}" , err
54+ ) ;
4855}
0 commit comments