Skip to content

Commit 06e1d88

Browse files
authored
Rollup merge of rust-lang#60220 - euclio:rustdoc-test-fatal-parsing-errors, r=QuietMisdreavus
report fatal errors during doctest parsing Fixes rust-lang#59557.
2 parents e9509f8 + a912664 commit 06e1d88

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

src/librustdoc/test.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,18 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, line: usize,
166166
compile_fail: bool, mut error_codes: Vec<String>, opts: &TestOptions,
167167
maybe_sysroot: Option<PathBuf>, linker: Option<PathBuf>, edition: Edition,
168168
persist_doctests: Option<PathBuf>) {
169-
// The test harness wants its own `main` and top-level functions, so
170-
// never wrap the test in `fn main() { ... }`.
171-
let (test, line_offset) = make_test(test, Some(cratename), as_test_harness, opts);
169+
let (test, line_offset) = match panic::catch_unwind(|| {
170+
make_test(test, Some(cratename), as_test_harness, opts)
171+
}) {
172+
Ok((test, line_offset)) => (test, line_offset),
173+
Err(cause) if cause.is::<errors::FatalErrorMarker>() => {
174+
// If the parser used by `make_test` panicked due to a fatal error, pass the test code
175+
// through unchanged. The error will be reported during compilation.
176+
(test.to_owned(), 0)
177+
},
178+
Err(cause) => panic::resume_unwind(cause),
179+
};
180+
172181
// FIXME(#44940): if doctests ever support path remapping, then this filename
173182
// needs to be the result of `SourceMap::span_to_unmapped_path`.
174183
let path = match filename {
@@ -337,7 +346,13 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, line: usize,
337346
}
338347
}
339348

340-
/// Makes the test file. Also returns the number of lines before the code begins
349+
/// Transforms a test into code that can be compiled into a Rust binary, and returns the number of
350+
/// lines before the test code begins.
351+
///
352+
/// # Panics
353+
///
354+
/// This function uses the compiler's parser internally. The parser will panic if it encounters a
355+
/// fatal error while parsing the test.
341356
pub fn make_test(s: &str,
342357
cratename: Option<&str>,
343358
dont_insert_main: bool,

src/test/rustdoc-ui/failed-doctest-output.stdout

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ error[E0425]: cannot find value `no` in this scope
1515
error: aborting due to previous error
1616

1717
For more information about this error, try `rustc --explain E0425`.
18-
thread '$DIR/failed-doctest-output.rs - OtherStruct (line 17)' panicked at 'couldn't compile the test', src/librustdoc/test.rs:310:13
18+
thread '$DIR/failed-doctest-output.rs - OtherStruct (line 17)' panicked at 'couldn't compile the test', src/librustdoc/test.rs:319:13
1919
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
2020

2121
---- $DIR/failed-doctest-output.rs - SomeStruct (line 11) stdout ----
@@ -24,7 +24,7 @@ thread '$DIR/failed-doctest-output.rs - SomeStruct (line 11)' panicked at 'test
2424
thread 'main' panicked at 'oh no', $DIR/failed-doctest-output.rs:3:1
2525
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
2626

27-
', src/librustdoc/test.rs:332:17
27+
', src/librustdoc/test.rs:341:17
2828

2929

3030
failures:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// compile-flags: --test
2+
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
3+
// failure-status: 101
4+
// rustc-env: RUST_BACKTRACE=0
5+
6+
/// ```rust
7+
/// let x = 7;
8+
/// "unterminated
9+
/// ```
10+
pub fn foo() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
running 1 test
3+
test $DIR/unparseable-doc-test.rs - foo (line 6) ... FAILED
4+
5+
failures:
6+
7+
---- $DIR/unparseable-doc-test.rs - foo (line 6) stdout ----
8+
error: unterminated double quote string
9+
--> $DIR/unparseable-doc-test.rs:8:1
10+
|
11+
2 | "unterminated
12+
| ^^^^^^^^^^^^^
13+
14+
error: aborting due to previous error
15+
16+
thread '$DIR/unparseable-doc-test.rs - foo (line 6)' panicked at 'couldn't compile the test', src/librustdoc/test.rs:319:13
17+
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
18+
19+
20+
failures:
21+
$DIR/unparseable-doc-test.rs - foo (line 6)
22+
23+
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
24+

0 commit comments

Comments
 (0)