diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index aa52b769c38ed..5bea1b5614159 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -41,7 +41,7 @@ pub fn render_with_highlighting( let fm = sess .source_map() .new_source_file(FileName::Custom(String::from("rustdoc-highlighting")), src.to_owned()); - let highlight_result = { + let highlight_result = rustc_driver::catch_fatal_errors(|| { let lexer = lexer::StringReader::new(&sess, fm, None); let mut classifier = Classifier::new(lexer, sess.source_map()); @@ -51,7 +51,8 @@ pub fn render_with_highlighting( } else { Ok(String::from_utf8_lossy(&highlighted_source).into_owned()) } - }; + }) + .unwrap_or(Err(())); match highlight_result { Ok(highlighted_source) => { diff --git a/src/librustdoc/passes/check_code_block_syntax.rs b/src/librustdoc/passes/check_code_block_syntax.rs index 0bab4423b3dfd..2903fd9dcd660 100644 --- a/src/librustdoc/passes/check_code_block_syntax.rs +++ b/src/librustdoc/passes/check_code_block_syntax.rs @@ -40,7 +40,7 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> { dox[code_block.code].to_owned(), ); - let validation_status = { + let validation_status = rustc_driver::catch_fatal_errors(|| { let mut has_syntax_errors = false; let mut only_whitespace = true; // even if there is a syntax error, we need to run the lexer over the whole file @@ -61,7 +61,8 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> { } else { None } - }; + }) + .unwrap_or(Some(CodeBlockInvalid::SyntaxError)); if let Some(code_block_invalid) = validation_status { let mut diag = if let Some(sp) = diff --git a/src/test/rustdoc-ui/invalid-syntax.rs b/src/test/rustdoc-ui/invalid-syntax.rs index 34e92c421047d..72037dd74be35 100644 --- a/src/test/rustdoc-ui/invalid-syntax.rs +++ b/src/test/rustdoc-ui/invalid-syntax.rs @@ -93,3 +93,9 @@ pub fn empty_rust_with_whitespace() {} /// pub fn indent_after_fenced() {} //~^^^ WARNING could not parse code block as Rust code + +/// ``` +/// "invalid +/// ``` +pub fn invalid() {} +//~^^^^ WARNING could not parse code block as Rust code diff --git a/src/test/rustdoc-ui/invalid-syntax.stderr b/src/test/rustdoc-ui/invalid-syntax.stderr index 32cc20755ecf4..a90d3bbb979f6 100644 --- a/src/test/rustdoc-ui/invalid-syntax.stderr +++ b/src/test/rustdoc-ui/invalid-syntax.stderr @@ -132,3 +132,18 @@ LL | /// \____/ | = note: error from rustc: unknown start of token: \ +warning: could not parse code block as Rust code + --> $DIR/invalid-syntax.rs:97:5 + | +LL | /// ``` + | _____^ +LL | | /// "invalid +LL | | /// ``` + | |_______^ + | + = note: error from rustc: unterminated double quote string +help: mark blocks that do not contain Rust code as text + | +LL | /// ```text + | ^^^^^^^ + diff --git a/src/test/rustdoc/bad-codeblock-syntax.rs b/src/test/rustdoc/bad-codeblock-syntax.rs index ae8fbe4a2a800..afef86ec9c77f 100644 --- a/src/test/rustdoc/bad-codeblock-syntax.rs +++ b/src/test/rustdoc/bad-codeblock-syntax.rs @@ -33,3 +33,10 @@ pub fn ok() {} /// /// ``` pub fn escape() {} + +// @has bad_codeblock_syntax/fn.unterminated.html +// @has - '//*[@class="docblock"]/pre/code' '"unterminated' +/// ``` +/// "unterminated +/// ``` +pub fn unterminated() {}