Skip to content

Commit 06c6894

Browse files
committed
Auto merge of #64158 - tmandry:libtest-panic-abort, r=alexcrichton
panic=abort support in libtest Add experimental support for tests compiled with panic=abort. Enabled with `-Z panic_abort_tests`. r? @alexcrichton cc @cramertj
2 parents d046ffd + 3f0254e commit 06c6894

File tree

15 files changed

+438
-118
lines changed

15 files changed

+438
-118
lines changed

src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
12791279
"show extended diagnostic help"),
12801280
terminal_width: Option<usize> = (None, parse_opt_uint, [UNTRACKED],
12811281
"set the current terminal width"),
1282+
panic_abort_tests: bool = (false, parse_bool, [TRACKED],
1283+
"support compiling tests with panic=abort"),
12821284
continue_parse_after_error: bool = (false, parse_bool, [TRACKED],
12831285
"attempt to recover from parse errors (experimental)"),
12841286
dep_tasks: bool = (false, parse_bool, [UNTRACKED],

src/librustc_interface/passes.rs

+3
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@ fn configure_and_expand_inner<'a>(
440440
&mut krate,
441441
sess.diagnostic(),
442442
&sess.features_untracked(),
443+
sess.panic_strategy(),
444+
sess.target.target.options.panic_strategy,
445+
sess.opts.debugging_opts.panic_abort_tests,
443446
)
444447
});
445448

src/libsyntax_ext/test_harness.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use log::debug;
44
use smallvec::{smallvec, SmallVec};
5+
use rustc_target::spec::PanicStrategy;
56
use syntax::ast::{self, Ident};
67
use syntax::attr;
78
use syntax::entry::{self, EntryPointType};
@@ -25,6 +26,7 @@ struct Test {
2526

2627
struct 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

src/libtest/formatters/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,12 @@ pub(crate) trait OutputFormatter {
2222
) -> io::Result<()>;
2323
fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool>;
2424
}
25+
26+
pub(crate) fn write_stderr_delimiter(test_output: &mut Vec<u8>, test_name: &TestName) {
27+
match test_output.last() {
28+
Some(b'\n') => (),
29+
Some(_) => test_output.push(b'\n'),
30+
None => (),
31+
}
32+
write!(test_output, "---- {} stderr ----\n", test_name).unwrap();
33+
}

0 commit comments

Comments
 (0)