Skip to content

Commit 202a05b

Browse files
Correctly handle should_panic doctest attribute
1 parent f618a05 commit 202a05b

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/librustdoc/doctest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ fn run_test(
828828
match result {
829829
Err(e) => return (duration, Err(TestFailure::ExecutionError(e))),
830830
Ok(out) => {
831-
if langstr.should_panic && out.status.success() {
831+
if langstr.should_panic && out.status.code() != Some(101) {
832832
return (duration, Err(TestFailure::UnexpectedRunPass));
833833
} else if !langstr.should_panic && !out.status.success() {
834834
return (duration, Err(TestFailure::ExecutionFailure(out)));
@@ -1138,7 +1138,7 @@ fn doctest_run_fn(
11381138
eprint!("Test compiled successfully, but it's marked `compile_fail`.");
11391139
}
11401140
TestFailure::UnexpectedRunPass => {
1141-
eprint!("Test executable succeeded, but it's marked `should_panic`.");
1141+
eprint!("Test didn't panic, but it's marked `should_panic`.");
11421142
}
11431143
TestFailure::MissingErrorCodes(codes) => {
11441144
eprint!("Some expected error codes were not found: {codes:?}");

src/librustdoc/doctest/runner.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,20 @@ mod __doctest_mod {{
136136
}}
137137
138138
#[allow(unused)]
139-
pub fn doctest_runner(bin: &std::path::Path, test_nb: usize) -> ExitCode {{
139+
pub fn doctest_runner(bin: &std::path::Path, test_nb: usize, should_panic: bool) -> ExitCode {{
140140
let out = std::process::Command::new(bin)
141141
.env(self::RUN_OPTION, test_nb.to_string())
142142
.args(std::env::args().skip(1).collect::<Vec<_>>())
143143
.output()
144144
.expect(\"failed to run command\");
145-
if !out.status.success() {{
145+
if should_panic {{
146+
if out.status.code() != Some(101) {{
147+
eprintln!(\"Test didn't panic, but it's marked `should_panic`.\");
148+
ExitCode::FAILURE
149+
}} else {{
150+
ExitCode::SUCCESS
151+
}}
152+
}} else if !out.status.success() {{
146153
if let Some(code) = out.status.code() {{
147154
eprintln!(\"Test executable failed (exit status: {{code}}).\");
148155
}} else {{
@@ -265,7 +272,7 @@ fn main() {returns_result} {{
265272
"
266273
mod {test_id} {{
267274
pub const TEST: test::TestDescAndFn = test::TestDescAndFn::new_doctest(
268-
{test_name:?}, {ignore}, {file:?}, {line}, {no_run}, {should_panic},
275+
{test_name:?}, {ignore}, {file:?}, {line}, {no_run}, false,
269276
test::StaticTestFn(
270277
|| {{{runner}}},
271278
));
@@ -274,7 +281,6 @@ test::StaticTestFn(
274281
file = scraped_test.path(),
275282
line = scraped_test.line,
276283
no_run = scraped_test.no_run(opts),
277-
should_panic = !scraped_test.langstr.no_run && scraped_test.langstr.should_panic,
278284
// Setting `no_run` to `true` in `TestDesc` still makes the test run, so we simply
279285
// don't give it the function to run.
280286
runner = if not_running {
@@ -283,11 +289,12 @@ test::StaticTestFn(
283289
format!(
284290
"
285291
if let Some(bin_path) = crate::__doctest_mod::doctest_path() {{
286-
test::assert_test_result(crate::__doctest_mod::doctest_runner(bin_path, {id}))
292+
test::assert_test_result(crate::__doctest_mod::doctest_runner(bin_path, {id}, {should_panic}))
287293
}} else {{
288294
test::assert_test_result(doctest_bundle::{test_id}::__main_fn())
289295
}}
290296
",
297+
should_panic = scraped_test.langstr.should_panic,
291298
)
292299
},
293300
)

0 commit comments

Comments
 (0)