2
2
3
3
use log:: debug;
4
4
use smallvec:: { smallvec, SmallVec } ;
5
+ use rustc_target:: spec:: PanicStrategy ;
5
6
use syntax:: ast:: { self , Ident } ;
6
7
use syntax:: attr;
7
8
use syntax:: entry:: { self , EntryPointType } ;
@@ -25,6 +26,7 @@ struct Test {
25
26
26
27
struct TestCtxt < ' a > {
27
28
ext_cx : ExtCtxt < ' a > ,
29
+ panic_strategy : PanicStrategy ,
28
30
def_site : Span ,
29
31
test_cases : Vec < Test > ,
30
32
reexport_test_harness_main : Option < Symbol > ,
@@ -40,6 +42,9 @@ pub fn inject(
40
42
krate : & mut ast:: Crate ,
41
43
span_diagnostic : & errors:: Handler ,
42
44
features : & Features ,
45
+ panic_strategy : PanicStrategy ,
46
+ platform_panic_strategy : PanicStrategy ,
47
+ enable_panic_abort_tests : bool ,
43
48
) {
44
49
// Check for #![reexport_test_harness_main = "some_name"] which gives the
45
50
// main test function the name `some_name` without hygiene. This needs to be
@@ -53,8 +58,22 @@ pub fn inject(
53
58
let test_runner = get_test_runner ( span_diagnostic, & krate) ;
54
59
55
60
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
+ } ;
56
75
generate_test_harness ( sess, resolver, reexport_test_harness_main,
57
- krate, features, test_runner)
76
+ krate, features, panic_strategy , test_runner)
58
77
}
59
78
}
60
79
@@ -183,6 +202,7 @@ fn generate_test_harness(sess: &ParseSess,
183
202
reexport_test_harness_main : Option < Symbol > ,
184
203
krate : & mut ast:: Crate ,
185
204
features : & Features ,
205
+ panic_strategy : PanicStrategy ,
186
206
test_runner : Option < ast:: Path > ) {
187
207
let mut econfig = ExpansionConfig :: default ( "test" . to_string ( ) ) ;
188
208
econfig. features = Some ( features) ;
@@ -203,6 +223,7 @@ fn generate_test_harness(sess: &ParseSess,
203
223
204
224
let cx = TestCtxt {
205
225
ext_cx,
226
+ panic_strategy,
206
227
def_site,
207
228
test_cases : Vec :: new ( ) ,
208
229
reexport_test_harness_main,
@@ -248,9 +269,14 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
248
269
let ecx = & cx. ext_cx ;
249
270
let test_id = Ident :: new ( sym:: test, sp) ;
250
271
272
+ let runner_name = match cx. panic_strategy {
273
+ PanicStrategy :: Unwind => "test_main_static" ,
274
+ PanicStrategy :: Abort => "test_main_static_abort" ,
275
+ } ;
276
+
251
277
// test::test_main_static(...)
252
278
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) ] ) ) ;
254
280
255
281
test_runner. span = sp;
256
282
0 commit comments