about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAndy Russell <arussell123@gmail.com>2019-04-23 17:39:26 -0400
committerAndy Russell <arussell123@gmail.com>2019-04-24 00:03:53 -0400
commita912664113394fdf3a7c26a93504ebfe54a66e5c (patch)
treed98aceb3cf678f74b4a8f875d02247fe35c0070d
parenta850a426491e14186af2250549bf41256b5938d2 (diff)
downloadrust-a912664113394fdf3a7c26a93504ebfe54a66e5c.tar.gz
rust-a912664113394fdf3a7c26a93504ebfe54a66e5c.zip
report fatal errors during doctest parsing
-rw-r--r--src/librustdoc/test.rs23
-rw-r--r--src/test/rustdoc-ui/failed-doctest-output.stdout4
-rw-r--r--src/test/rustdoc-ui/unparseable-doc-test.rs10
-rw-r--r--src/test/rustdoc-ui/unparseable-doc-test.stdout24
4 files changed, 55 insertions, 6 deletions
diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs
index abf74158c93..8064c3ebb8a 100644
--- a/src/librustdoc/test.rs
+++ b/src/librustdoc/test.rs
@@ -166,9 +166,18 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, line: usize,
             compile_fail: bool, mut error_codes: Vec<String>, opts: &TestOptions,
             maybe_sysroot: Option<PathBuf>, linker: Option<PathBuf>, edition: Edition,
             persist_doctests: Option<PathBuf>) {
-    // The test harness wants its own `main` and top-level functions, so
-    // never wrap the test in `fn main() { ... }`.
-    let (test, line_offset) = make_test(test, Some(cratename), as_test_harness, opts);
+    let (test, line_offset) = match panic::catch_unwind(|| {
+        make_test(test, Some(cratename), as_test_harness, opts)
+    }) {
+        Ok((test, line_offset)) => (test, line_offset),
+        Err(cause) if cause.is::<errors::FatalErrorMarker>() => {
+            // If the parser used by `make_test` panicked due to a fatal error, pass the test code
+            // through unchanged. The error will be reported during compilation.
+            (test.to_owned(), 0)
+        },
+        Err(cause) => panic::resume_unwind(cause),
+    };
+
     // FIXME(#44940): if doctests ever support path remapping, then this filename
     // needs to be the result of `SourceMap::span_to_unmapped_path`.
     let path = match filename {
@@ -337,7 +346,13 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, line: usize,
     }
 }
 
-/// Makes the test file. Also returns the number of lines before the code begins
+/// Transforms a test into code that can be compiled into a Rust binary, and returns the number of
+/// lines before the test code begins.
+///
+/// # Panics
+///
+/// This function uses the compiler's parser internally. The parser will panic if it encounters a
+/// fatal error while parsing the test.
 pub fn make_test(s: &str,
                  cratename: Option<&str>,
                  dont_insert_main: bool,
diff --git a/src/test/rustdoc-ui/failed-doctest-output.stdout b/src/test/rustdoc-ui/failed-doctest-output.stdout
index c9f59405ce0..7b1cd702731 100644
--- a/src/test/rustdoc-ui/failed-doctest-output.stdout
+++ b/src/test/rustdoc-ui/failed-doctest-output.stdout
@@ -15,7 +15,7 @@ error[E0425]: cannot find value `no` in this scope
 error: aborting due to previous error
 
 For more information about this error, try `rustc --explain E0425`.
-thread '$DIR/failed-doctest-output.rs - OtherStruct (line 17)' panicked at 'couldn't compile the test', src/librustdoc/test.rs:310:13
+thread '$DIR/failed-doctest-output.rs - OtherStruct (line 17)' panicked at 'couldn't compile the test', src/librustdoc/test.rs:319:13
 note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
 
 ---- $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
 thread 'main' panicked at 'oh no', $DIR/failed-doctest-output.rs:3:1
 note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
 
-', src/librustdoc/test.rs:332:17
+', src/librustdoc/test.rs:341:17
 
 
 failures:
diff --git a/src/test/rustdoc-ui/unparseable-doc-test.rs b/src/test/rustdoc-ui/unparseable-doc-test.rs
new file mode 100644
index 00000000000..18d6b32bf40
--- /dev/null
+++ b/src/test/rustdoc-ui/unparseable-doc-test.rs
@@ -0,0 +1,10 @@
+// compile-flags: --test
+// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
+// failure-status: 101
+// rustc-env: RUST_BACKTRACE=0
+
+/// ```rust
+/// let x = 7;
+/// "unterminated
+/// ```
+pub fn foo() {}
diff --git a/src/test/rustdoc-ui/unparseable-doc-test.stdout b/src/test/rustdoc-ui/unparseable-doc-test.stdout
new file mode 100644
index 00000000000..7048ef2c589
--- /dev/null
+++ b/src/test/rustdoc-ui/unparseable-doc-test.stdout
@@ -0,0 +1,24 @@
+
+running 1 test
+test $DIR/unparseable-doc-test.rs - foo (line 6) ... FAILED
+
+failures:
+
+---- $DIR/unparseable-doc-test.rs - foo (line 6) stdout ----
+error: unterminated double quote string
+ --> $DIR/unparseable-doc-test.rs:8:1
+  |
+2 | "unterminated
+  | ^^^^^^^^^^^^^
+
+error: aborting due to previous error
+
+thread '$DIR/unparseable-doc-test.rs - foo (line 6)' panicked at 'couldn't compile the test', src/librustdoc/test.rs:319:13
+note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
+
+
+failures:
+    $DIR/unparseable-doc-test.rs - foo (line 6)
+
+test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
+