@@ -132,7 +132,7 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
132132 path : self . cx . path . clone ( ) ,
133133 bench : is_bench_fn ( & self . cx , & i) ,
134134 ignore : is_ignored ( & i) ,
135- should_panic : should_panic ( & i)
135+ should_panic : should_panic ( & i, & self . cx )
136136 } ;
137137 self . cx . testfns . push ( test) ;
138138 self . tests . push ( i. ident ) ;
@@ -395,14 +395,44 @@ fn is_ignored(i: &ast::Item) -> bool {
395395 i. attrs . iter ( ) . any ( |attr| attr. check_name ( "ignore" ) )
396396}
397397
398- fn should_panic ( i : & ast:: Item ) -> ShouldPanic {
398+ fn should_panic ( i : & ast:: Item , cx : & TestCtxt ) -> ShouldPanic {
399399 match i. attrs . iter ( ) . find ( |attr| attr. check_name ( "should_panic" ) ) {
400400 Some ( attr) => {
401- let msg = attr. meta_item_list ( )
402- . and_then ( |list| list. iter ( ) . find ( |mi| mi. check_name ( "expected" ) ) )
403- . and_then ( |li| li. meta_item ( ) )
404- . and_then ( |mi| mi. value_str ( ) ) ;
405- ShouldPanic :: Yes ( msg)
401+ let sd = cx. span_diagnostic ;
402+ if attr. is_value_str ( ) {
403+ sd. struct_span_warn (
404+ attr. span ( ) ,
405+ "attribute must be of the form: \
406+ `#[should_panic]` or \
407+ `#[should_panic(expected = \" error message\" )]`"
408+ ) . note ( "Errors in this attribute were erroneously allowed \
409+ and will become a hard error in a future release.")
410+ . emit ( ) ;
411+ return ShouldPanic :: Yes ( None ) ;
412+ }
413+ match attr. meta_item_list ( ) {
414+ // Handle #[should_panic]
415+ None => ShouldPanic :: Yes ( None ) ,
416+ // Handle #[should_panic(expected = "foo")]
417+ Some ( list) => {
418+ let msg = list. iter ( )
419+ . find ( |mi| mi. check_name ( "expected" ) )
420+ . and_then ( |mi| mi. meta_item ( ) )
421+ . and_then ( |mi| mi. value_str ( ) ) ;
422+ if list. len ( ) != 1 || msg. is_none ( ) {
423+ sd. struct_span_warn (
424+ attr. span ( ) ,
425+ "argument must be of the form: \
426+ `expected = \" error message\" `"
427+ ) . note ( "Errors in this attribute were erroneously \
428+ allowed and will become a hard error in a \
429+ future release.") . emit ( ) ;
430+ ShouldPanic :: Yes ( None )
431+ } else {
432+ ShouldPanic :: Yes ( msg)
433+ }
434+ } ,
435+ }
406436 }
407437 None => ShouldPanic :: No ,
408438 }
0 commit comments