22
33use log:: debug;
44use smallvec:: { smallvec, SmallVec } ;
5+ use rustc_target:: spec:: PanicStrategy ;
56use syntax:: ast:: { self , Ident } ;
67use syntax:: attr;
78use syntax:: entry:: { self , EntryPointType } ;
@@ -25,6 +26,7 @@ struct Test {
2526
2627struct TestCtxt < ' a > {
2728 ext_cx : ExtCtxt < ' a > ,
29+ panic_strategy : PanicStrategy ,
2830 def_site : Span ,
2931 test_cases : Vec < Test > ,
3032 reexport_test_harness_main : Option < Symbol > ,
@@ -40,6 +42,9 @@ pub fn inject(
4042 krate : & mut ast:: Crate ,
4143 span_diagnostic : & errors:: Handler ,
4244 features : & Features ,
45+ panic_strategy : PanicStrategy ,
46+ platform_panic_strategy : PanicStrategy ,
47+ enable_panic_abort_tests : bool ,
4348) {
4449 // Check for #![reexport_test_harness_main = "some_name"] which gives the
4550 // main test function the name `some_name` without hygiene. This needs to be
@@ -53,8 +58,22 @@ pub fn inject(
5358 let test_runner = get_test_runner ( span_diagnostic, & krate) ;
5459
5560 if should_test {
61+ let panic_strategy = match ( panic_strategy, enable_panic_abort_tests) {
62+ ( PanicStrategy :: Abort , true ) =>
63+ PanicStrategy :: Abort ,
64+ ( PanicStrategy :: Abort , false ) if panic_strategy == platform_panic_strategy => {
65+ // Silently allow compiling with panic=abort on these platforms,
66+ // but with old behavior (abort if a test fails).
67+ PanicStrategy :: Unwind
68+ }
69+ ( PanicStrategy :: Abort , false ) => {
70+ span_diagnostic. err ( "building tests with panic=abort is not yet supported" ) ;
71+ PanicStrategy :: Unwind
72+ }
73+ ( PanicStrategy :: Unwind , _) => PanicStrategy :: Unwind ,
74+ } ;
5675 generate_test_harness ( sess, resolver, reexport_test_harness_main,
57- krate, features, test_runner)
76+ krate, features, panic_strategy , test_runner)
5877 }
5978}
6079
@@ -183,6 +202,7 @@ fn generate_test_harness(sess: &ParseSess,
183202 reexport_test_harness_main : Option < Symbol > ,
184203 krate : & mut ast:: Crate ,
185204 features : & Features ,
205+ panic_strategy : PanicStrategy ,
186206 test_runner : Option < ast:: Path > ) {
187207 let mut econfig = ExpansionConfig :: default ( "test" . to_string ( ) ) ;
188208 econfig. features = Some ( features) ;
@@ -203,6 +223,7 @@ fn generate_test_harness(sess: &ParseSess,
203223
204224 let cx = TestCtxt {
205225 ext_cx,
226+ panic_strategy,
206227 def_site,
207228 test_cases : Vec :: new ( ) ,
208229 reexport_test_harness_main,
@@ -248,9 +269,14 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
248269 let ecx = & cx. ext_cx ;
249270 let test_id = Ident :: new ( sym:: test, sp) ;
250271
272+ let runner_name = match cx. panic_strategy {
273+ PanicStrategy :: Unwind => "test_main_static" ,
274+ PanicStrategy :: Abort => "test_main_static_abort" ,
275+ } ;
276+
251277 // test::test_main_static(...)
252278 let mut test_runner = cx. test_runner . clone ( ) . unwrap_or (
253- ecx. path ( sp, vec ! [ test_id, ecx. ident_of( "test_main_static" , sp) ] ) ) ;
279+ ecx. path ( sp, vec ! [ test_id, ecx. ident_of( runner_name , sp) ] ) ) ;
254280
255281 test_runner. span = sp;
256282
0 commit comments