Skip to content

Commit 3811364

Browse files
Correctly handle should_panic doctest attribute
1 parent a413f77 commit 3811364

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/librustdoc/doctest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ fn run_test(
773773
match result {
774774
Err(e) => return Err(TestFailure::ExecutionError(e)),
775775
Ok(out) => {
776-
if langstr.should_panic && out.status.success() {
776+
if langstr.should_panic && out.status.code() != Some(101) {
777777
return Err(TestFailure::UnexpectedRunPass);
778778
} else if !langstr.should_panic && !out.status.success() {
779779
return Err(TestFailure::ExecutionFailure(out));
@@ -1083,7 +1083,7 @@ fn doctest_run_fn(
10831083
eprint!("Test compiled successfully, but it's marked `compile_fail`.");
10841084
}
10851085
TestFailure::UnexpectedRunPass => {
1086-
eprint!("Test executable succeeded, but it's marked `should_panic`.");
1086+
eprint!("Test didn't panic, but it's marked `should_panic`.");
10871087
}
10881088
TestFailure::MissingErrorCodes(codes) => {
10891089
eprint!("Some expected error codes were not found: {codes:?}");

src/librustdoc/doctest/runner.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,15 @@ mod __doctest_mod {{
129129
}}
130130
131131
#[allow(unused)]
132-
pub fn doctest_runner(bin: &std::path::Path, test_nb: usize) -> ExitCode {{
132+
pub fn doctest_runner(bin: &std::path::Path, test_nb: usize, should_panic: bool) -> ExitCode {{
133133
let out = std::process::Command::new(bin)
134134
.env(self::RUN_OPTION, test_nb.to_string())
135135
.args(std::env::args().skip(1).collect::<Vec<_>>())
136136
.output()
137137
.expect(\"failed to run command\");
138-
if !out.status.success() {{
138+
if should_panic && out.status.code() != Some(101) {{
139+
eprintln!(\"Test didn't panic, but it's marked `should_panic`.\");
140+
}} else if !out.status.success() {{
139141
if let Some(code) = out.status.code() {{
140142
eprintln!(\"Test executable failed (exit status: {{code}}).\");
141143
}} else {{
@@ -257,7 +259,7 @@ fn main() {returns_result} {{
257259
"
258260
mod {test_id} {{
259261
pub const TEST: test::TestDescAndFn = test::TestDescAndFn::new_doctest(
260-
{test_name:?}, {ignore}, {file:?}, {line}, {no_run}, {should_panic},
262+
{test_name:?}, {ignore}, {file:?}, {line}, {no_run}, false,
261263
test::StaticTestFn(
262264
|| {{{runner}}},
263265
));
@@ -266,7 +268,6 @@ test::StaticTestFn(
266268
file = scraped_test.path(),
267269
line = scraped_test.line,
268270
no_run = scraped_test.langstr.no_run,
269-
should_panic = !scraped_test.langstr.no_run && scraped_test.langstr.should_panic,
270271
// Setting `no_run` to `true` in `TestDesc` still makes the test run, so we simply
271272
// don't give it the function to run.
272273
runner = if not_running {
@@ -275,11 +276,12 @@ test::StaticTestFn(
275276
format!(
276277
"
277278
if let Some(bin_path) = crate::__doctest_mod::doctest_path() {{
278-
test::assert_test_result(crate::__doctest_mod::doctest_runner(bin_path, {id}))
279+
test::assert_test_result(crate::__doctest_mod::doctest_runner(bin_path, {id}, {should_panic}))
279280
}} else {{
280281
test::assert_test_result(doctest_bundle::{test_id}::__main_fn())
281282
}}
282283
",
284+
should_panic = !scraped_test.langstr.no_run && scraped_test.langstr.should_panic,
283285
)
284286
},
285287
)

0 commit comments

Comments
 (0)