Rollup merge of #139328 - GuillaumeGomez:fix-panic-output-137970, r=fmease

Fix 2024 edition doctest panic output

Fixes #137970.

The problem was that the output was actually displayed by rustc itself because we're exiting with `Result<(), String>`, and the display is really not great. So instead, we get the output, we print it and then we return an `ExitCode`.

r? ````@aDotInTheVoid````
This commit is contained in:
Matthias Krüger 2025-04-04 21:54:57 +02:00 committed by GitHub
commit cfc2297cfc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 39 additions and 3 deletions

View file

@ -113,6 +113,7 @@ impl DocTestRunner {
mod __doctest_mod {{
use std::sync::OnceLock;
use std::path::PathBuf;
use std::process::ExitCode;
pub static BINARY_PATH: OnceLock<PathBuf> = OnceLock::new();
pub const RUN_OPTION: &str = \"RUSTDOC_DOCTEST_RUN_NB_TEST\";
@ -123,16 +124,17 @@ mod __doctest_mod {{
}}
#[allow(unused)]
pub fn doctest_runner(bin: &std::path::Path, test_nb: usize) -> Result<(), String> {{
pub fn doctest_runner(bin: &std::path::Path, test_nb: usize) -> ExitCode {{
let out = std::process::Command::new(bin)
.env(self::RUN_OPTION, test_nb.to_string())
.args(std::env::args().skip(1).collect::<Vec<_>>())
.output()
.expect(\"failed to run command\");
if !out.status.success() {{
Err(String::from_utf8_lossy(&out.stderr).to_string())
eprint!(\"{{}}\", String::from_utf8_lossy(&out.stderr));
ExitCode::FAILURE
}} else {{
Ok(())
ExitCode::SUCCESS
}}
}}
}}

View file

@ -0,0 +1,14 @@
// This is a regression test for <https://github.com/rust-lang/rust/issues/137970>.
// The output must look nice and not like a `Debug` display of a `String`.
//@ edition: 2024
//@ compile-flags: --test
//@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR"
//@ normalize-stdout: "panicked at .+rs:" -> "panicked at $$TMP:"
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
//@ rustc-env:RUST_BACKTRACE=0
//@ failure-status: 101
//! ```rust
//! assert_eq!(2 + 2, 5);
//! ```

View file

@ -0,0 +1,20 @@
running 1 test
test $DIR/edition-2024-error-output.rs - (line 12) ... FAILED
failures:
---- $DIR/edition-2024-error-output.rs - (line 12) stdout ----
thread 'main' panicked at $TMP:6:1:
assertion `left == right` failed
left: 4
right: 5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
$DIR/edition-2024-error-output.rs - (line 12)
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME