Skip to content

Commit dfc4264

Browse files
Improve error messages for failing should_panic doctests
Added missing FIXME comments
1 parent b70e20a commit dfc4264

File tree

5 files changed

+82
-19
lines changed

5 files changed

+82
-19
lines changed

src/librustdoc/doctest.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ enum TestFailure {
466466
/// This typically means an assertion in the test failed or another form of panic occurred.
467467
ExecutionFailure(process::Output),
468468
/// The test is marked `should_panic` but the test binary executed successfully.
469-
UnexpectedRunPass,
469+
NoPanic(Option<String>),
470470
}
471471

472472
enum DirState {
@@ -812,6 +812,9 @@ fn run_test(
812812
// (cfg!(target_family = "wasm") || cfg!(target_os = "zkvm"))
813813
// && !cfg!(target_os = "emscripten")
814814
// ```
815+
//
816+
// FIXME: All this code is terrible and doesn't take into account `TargetTuple::TargetJson`.
817+
// If `libtest` doesn't allow to handle this case, we'll need to use a rustc's API instead.
815818
&& let TargetTuple::TargetTuple(ref s) = rustdoc_options.target
816819
&& let mut iter = s.split('-')
817820
&& let Some(arch) = iter.next()
@@ -876,12 +879,41 @@ fn run_test(
876879
#[cfg(windows)]
877880
Some(STATUS_FAIL_FAST_EXCEPTION) => {}
878881
#[cfg(unix)]
879-
None if out.status.signal() == Some(SIGABRT) => {}
882+
None => match out.status.signal() {
883+
Some(SIGABRT) => {}
884+
Some(signal) => {
885+
return (
886+
duration,
887+
Err(TestFailure::NoPanic(Some(format!(
888+
"Test didn't panic, but it's marked `should_panic` (exit signal: {signal}).",
889+
)))),
890+
);
891+
}
892+
None => {
893+
return (
894+
duration,
895+
Err(TestFailure::NoPanic(Some(format!(
896+
"Test didn't panic, but it's marked `should_panic` and exited with no error code and no signal.",
897+
)))),
898+
);
899+
}
900+
},
901+
#[cfg(not(unix))]
902+
None => return (duration, Err(TestFailure::NoPanic(None))),
880903
// Upon an abort, Fuchsia returns the status code
881904
// `ZX_TASK_RETCODE_EXCEPTION_KILL`.
882905
#[cfg(target_os = "fuchsia")]
883906
Some(ZX_TASK_RETCODE_EXCEPTION_KILL) => {}
884-
_ => return (duration, Err(TestFailure::UnexpectedRunPass)),
907+
Some(exit_code) => {
908+
let err_msg = if !out.status.success() {
909+
Some(format!(
910+
"Test didn't panic, but it's marked `should_panic` (exit status: {exit_code}).",
911+
))
912+
} else {
913+
None
914+
};
915+
return (duration, Err(TestFailure::NoPanic(err_msg)));
916+
}
885917
}
886918
} else if !out.status.success() {
887919
return (duration, Err(TestFailure::ExecutionFailure(out)));
@@ -1190,8 +1222,12 @@ fn doctest_run_fn(
11901222
TestFailure::UnexpectedCompilePass => {
11911223
eprint!("Test compiled successfully, but it's marked `compile_fail`.");
11921224
}
1193-
TestFailure::UnexpectedRunPass => {
1194-
eprint!("Test didn't panic, but it's marked `should_panic`.");
1225+
TestFailure::NoPanic(msg) => {
1226+
if let Some(msg) = msg {
1227+
eprint!("{msg}");
1228+
} else {
1229+
eprint!("Test didn't panic, but it's marked `should_panic`.");
1230+
}
11951231
}
11961232
TestFailure::MissingErrorCodes(codes) => {
11971233
eprint!("Some expected error codes were not found: {codes:?}");

src/librustdoc/doctest/runner.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,31 @@ mod __doctest_mod {{
169169
#[cfg(windows)]
170170
Some(STATUS_FAIL_FAST_EXCEPTION) => ExitCode::SUCCESS,
171171
#[cfg(unix)]
172-
None if out.status.signal() == Some(SIGABRT) => ExitCode::SUCCESS,
172+
None => match out.status.signal() {{
173+
Some(SIGABRT) => ExitCode::SUCCESS,
174+
Some(signal) => {{
175+
eprintln!(\"Test didn't panic, but it's marked `should_panic` (exit signal: {{signal}}).\");
176+
ExitCode::FAILURE
177+
}}
178+
None => {{
179+
eprintln!(\"Test didn't panic, but it's marked `should_panic` and exited with no error code and no signal.\");
180+
ExitCode::FAILURE
181+
}}
182+
}},
183+
#[cfg(not(unix))]
184+
None => {{
185+
eprintln!(\"Test didn't panic, but it's marked `should_panic`.\");
186+
ExitCode::FAILURE
187+
}}
173188
// Upon an abort, Fuchsia returns the status code ZX_TASK_RETCODE_EXCEPTION_KILL.
174189
#[cfg(target_os = \"fuchsia\")]
175190
Some(ZX_TASK_RETCODE_EXCEPTION_KILL) => ExitCode::SUCCESS,
176-
_ => {{
177-
eprintln!(\"Test didn't panic, but it's marked `should_panic`.\");
191+
Some(exit_code) => {{
192+
if !out.status.success() {{
193+
eprintln!(\"Test didn't panic, but it's marked `should_panic` (exit status: {{exit_code}}).\");
194+
}} else {{
195+
eprintln!(\"Test didn't panic, but it's marked `should_panic`.\");
196+
}}
178197
ExitCode::FAILURE
179198
}}
180199
}}

tests/run-make/rustdoc-should-panic/rmake.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn check_output(edition: &str, panic_abort: bool) {
1919
"---- test.rs - bad_exit_code (line 1) stdout ----
2020
Test executable failed (exit status: 1).",
2121
"---- test.rs - did_not_panic (line 6) stdout ----
22-
Test didn't panic, but it's marked `should_panic`.",
22+
Test didn't panic, but it's marked `should_panic` (exit status: 1).",
2323
"test result: FAILED. 1 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out;",
2424
];
2525
for text in should_contain {

tests/rustdoc-ui/doctest/failed-doctest-should-panic.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
// adapted to use that, and that normalize line can go away
33

44
//@ edition: 2024
5-
//@ compile-flags:--test
5+
//@ compile-flags:--test --test-args=--test-threads=1
66
//@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR"
77
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
88
//@ normalize-stdout: "ran in \d+\.\d+s" -> "ran in $$TIME"
99
//@ normalize-stdout: "compilation took \d+\.\d+s" -> "compilation took $$TIME"
1010
//@ failure-status: 101
1111

12-
/// ```should_panic
13-
/// println!("Hello, world!");
14-
/// ```
15-
pub struct Foo;
12+
//! ```should_panic
13+
//! println!("Hello, world!");
14+
//! ```
15+
//!
16+
//! ```should_panic
17+
//! std::process::exit(2);
18+
//! ```
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11

2-
running 1 test
3-
test $DIR/failed-doctest-should-panic.rs - Foo (line 12) ... FAILED
2+
running 2 tests
3+
test $DIR/failed-doctest-should-panic.rs - (line 12) ... FAILED
4+
test $DIR/failed-doctest-should-panic.rs - (line 16) ... FAILED
45

56
failures:
67

7-
---- $DIR/failed-doctest-should-panic.rs - Foo (line 12) stdout ----
8+
---- $DIR/failed-doctest-should-panic.rs - (line 12) stdout ----
89
Test didn't panic, but it's marked `should_panic`.
910

11+
---- $DIR/failed-doctest-should-panic.rs - (line 16) stdout ----
12+
Test didn't panic, but it's marked `should_panic` (exit status: 2).
13+
1014

1115
failures:
12-
$DIR/failed-doctest-should-panic.rs - Foo (line 12)
16+
$DIR/failed-doctest-should-panic.rs - (line 12)
17+
$DIR/failed-doctest-should-panic.rs - (line 16)
1318

14-
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
19+
test result: FAILED. 0 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
1520

1621
all doctests ran in $TIME; merged doctests compilation took $TIME

0 commit comments

Comments
 (0)