@@ -132,7 +132,7 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
132
132
path : self . cx . path . clone ( ) ,
133
133
bench : is_bench_fn ( & self . cx , & i) ,
134
134
ignore : is_ignored ( & i) ,
135
- should_panic : should_panic ( & i)
135
+ should_panic : should_panic ( & i, & self . cx )
136
136
} ;
137
137
self . cx . testfns . push ( test) ;
138
138
self . tests . push ( i. ident ) ;
@@ -395,14 +395,44 @@ fn is_ignored(i: &ast::Item) -> bool {
395
395
i. attrs . iter ( ) . any ( |attr| attr. check_name ( "ignore" ) )
396
396
}
397
397
398
- fn should_panic ( i : & ast:: Item ) -> ShouldPanic {
398
+ fn should_panic ( i : & ast:: Item , cx : & TestCtxt ) -> ShouldPanic {
399
399
match i. attrs . iter ( ) . find ( |attr| attr. check_name ( "should_panic" ) ) {
400
400
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
+ }
406
436
}
407
437
None => ShouldPanic :: No ,
408
438
}
0 commit comments