Skip to content

Commit 5c67f7b

Browse files
authored
Unrolled build for #147912
Rollup merge of #147912 - GuillaumeGomez:graceful-doctest-error-handling, r=lolbinarycat [rustdoc] Gracefully handle error in case we cannot run the compiler in doctests Fixes bug reported in [this comment](#102981 (comment)). r? ``@lolbinarycat``
2 parents 401ae55 + 2c3c82c commit 5c67f7b

File tree

6 files changed

+48
-4
lines changed

6 files changed

+48
-4
lines changed

src/librustdoc/doctest.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,13 @@ fn run_test(
671671

672672
debug!("compiler invocation for doctest: {compiler:?}");
673673

674-
let mut child = compiler.spawn().expect("Failed to spawn rustc process");
674+
let mut child = match compiler.spawn() {
675+
Ok(child) => child,
676+
Err(error) => {
677+
eprintln!("Failed to spawn {:?}: {error:?}", compiler.get_program());
678+
return (Duration::default(), Err(TestFailure::CompileError));
679+
}
680+
};
675681
let output = if let Some(merged_test_code) = &doctest.merged_test_code {
676682
// compile-fail tests never get merged, so this should always pass
677683
let status = child.wait().expect("Failed to wait");
@@ -733,7 +739,13 @@ fn run_test(
733739
let status = if !status.success() {
734740
status
735741
} else {
736-
let mut child_runner = runner_compiler.spawn().expect("Failed to spawn rustc process");
742+
let mut child_runner = match runner_compiler.spawn() {
743+
Ok(child) => child,
744+
Err(error) => {
745+
eprintln!("Failed to spawn {:?}: {error:?}", runner_compiler.get_program());
746+
return (Duration::default(), Err(TestFailure::CompileError));
747+
}
748+
};
737749
child_runner.wait().expect("Failed to wait")
738750
};
739751

src/tools/run-make-support/src/command.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,13 @@ impl CompletedProcess {
387387
self
388388
}
389389

390+
/// Checks that `stderr` doesn't contain the Internal Compiler Error message.
391+
#[track_caller]
392+
pub fn assert_not_ice(&self) -> &Self {
393+
self.assert_stderr_not_contains("error: the compiler unexpectedly panicked. this is a bug");
394+
self
395+
}
396+
390397
/// Checks that `stderr` does not contain the regex pattern `unexpected`.
391398
#[track_caller]
392399
pub fn assert_stderr_not_contains_regex<S: AsRef<str>>(&self, unexpected: S) -> &Self {

tests/run-make/diagnostics-traits-from-duplicate-crates/rmake.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ fn main() {
4343
.extern_("minibevy", "libminibevy-b.rmeta")
4444
.extern_("minirapier", "libminirapier.rmeta")
4545
.run_fail()
46-
.assert_stderr_not_contains("error: the compiler unexpectedly panicked. this is a bug");
46+
.assert_not_ice();
4747
}

tests/run-make/rustdoc-merge-no-input-finalize/rmake.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ fn main() {
2424
.arg(format!("--include-parts-dir={}", parts_out_dir.display()))
2525
.arg("--merge=finalize")
2626
.run();
27-
output.assert_stderr_not_contains("error: the compiler unexpectedly panicked. this is a bug.");
27+
output.assert_not_ice();
2828
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//! ```
2+
//! let x = 12;
3+
//! ```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This test ensures that if the rustdoc test binary is not executable, it will
2+
// gracefully fail and not panic.
3+
4+
//@ needs-target-std
5+
6+
use run_make_support::{path, rfs, rustdoc};
7+
8+
fn main() {
9+
let absolute_path = path("foo.rs").canonicalize().expect("failed to get absolute path");
10+
let output = rustdoc()
11+
.input("foo.rs")
12+
.arg("--test")
13+
.arg("-Zunstable-options")
14+
.arg("--test-builder")
15+
.arg(&absolute_path)
16+
.run_fail();
17+
18+
// We check that rustdoc outputs the error correctly...
19+
output.assert_stdout_contains("Failed to spawn ");
20+
// ... and that we didn't panic.
21+
output.assert_not_ice();
22+
}

0 commit comments

Comments
 (0)