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,22 @@ 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 ! ( err. contains( "formatting trait implementation returned an error" ) , "unexpected panic: {}" , err) ;
39+
40+ // Writer error when there's some string before the first `{}`
4041 let res = write ! ( ErrorWriter , "abc {}" , ErrorDisplay ) ;
4142 assert ! ( res. is_err( ) , "writer error did not propagate" ) ;
4243 assert_eq ! ( res. unwrap_err( ) . kind( ) , WRITER_ERROR ) ;
4344
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 ) ;
45+ // Formatter error when the `{}` comes first
46+ let res = catch_unwind ( || write ! ( ErrorWriter , "{} abc" , ErrorDisplay ) ) ;
47+ let err = res. expect_err ( "formatter error did not lead to panic" ) . downcast :: < & str > ( ) . unwrap ( ) ;
48+ assert ! ( err . contains ( "formatting trait implementation returned an error" ) , "unexpected panic: {}" , err ) ;
4849}
0 commit comments