Skip to content

Commit 43f291c

Browse files
authored
Rollup merge of rust-lang#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 4ff32c0 + 8233b28 commit 43f291c

File tree

13 files changed

+402
-96
lines changed

13 files changed

+402
-96
lines changed

src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
12951295
"show extended diagnostic help"),
12961296
terminal_width: Option<usize> = (None, parse_opt_uint, [UNTRACKED],
12971297
"set the current terminal width"),
1298+
panic_abort_tests: bool = (false, parse_bool, [TRACKED],
1299+
"support compiling tests with panic=abort"),
12981300
continue_parse_after_error: bool = (false, parse_bool, [TRACKED],
12991301
"attempt to recover from parse errors (experimental)"),
13001302
dep_tasks: bool = (false, parse_bool, [UNTRACKED],

src/librustc_interface/passes.rs

+2
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,8 @@ fn configure_and_expand_inner<'a>(
446446
&mut krate,
447447
sess.diagnostic(),
448448
&sess.features_untracked(),
449+
sess.panic_strategy(),
450+
sess.opts.debugging_opts.panic_abort_tests,
449451
)
450452
});
451453

src/libsyntax_ext/test_harness.rs

+17-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,8 @@ pub fn inject(
4042
krate: &mut ast::Crate,
4143
span_diagnostic: &errors::Handler,
4244
features: &Features,
45+
panic_strategy: PanicStrategy,
46+
enable_panic_abort_tests: bool,
4347
) {
4448
// Check for #![reexport_test_harness_main = "some_name"] which gives the
4549
// main test function the name `some_name` without hygiene. This needs to be
@@ -52,9 +56,13 @@ pub fn inject(
5256
// even in non-test builds
5357
let test_runner = get_test_runner(span_diagnostic, &krate);
5458

59+
let panic_strategy = match (panic_strategy, enable_panic_abort_tests) {
60+
(PanicStrategy::Abort, true) => PanicStrategy::Abort,
61+
_ => PanicStrategy::Unwind,
62+
};
5563
if should_test {
5664
generate_test_harness(sess, resolver, reexport_test_harness_main,
57-
krate, features, test_runner)
65+
krate, features, panic_strategy, test_runner)
5866
}
5967
}
6068

@@ -183,6 +191,7 @@ fn generate_test_harness(sess: &ParseSess,
183191
reexport_test_harness_main: Option<Symbol>,
184192
krate: &mut ast::Crate,
185193
features: &Features,
194+
panic_strategy: PanicStrategy,
186195
test_runner: Option<ast::Path>) {
187196
let mut econfig = ExpansionConfig::default("test".to_string());
188197
econfig.features = Some(features);
@@ -203,6 +212,7 @@ fn generate_test_harness(sess: &ParseSess,
203212

204213
let cx = TestCtxt {
205214
ext_cx,
215+
panic_strategy,
206216
def_site,
207217
test_cases: Vec::new(),
208218
reexport_test_harness_main,
@@ -248,9 +258,14 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
248258
let ecx = &cx.ext_cx;
249259
let test_id = Ident::new(sym::test, sp);
250260

261+
let runner_name = match cx.panic_strategy {
262+
PanicStrategy::Unwind => "test_main_static",
263+
PanicStrategy::Abort => "test_main_static_abort",
264+
};
265+
251266
// test::test_main_static(...)
252267
let mut test_runner = cx.test_runner.clone().unwrap_or(
253-
ecx.path(sp, vec![test_id, ecx.ident_of("test_main_static", sp)]));
268+
ecx.path(sp, vec![test_id, ecx.ident_of(runner_name, sp)]));
254269

255270
test_runner.span = sp;
256271

src/libtest/formatters/mod.rs

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

0 commit comments

Comments
 (0)