Skip to content

Commit

Permalink
rustdoc: Catch fatal errors when syntax highlighting
Browse files Browse the repository at this point in the history
For some errors the lexer will unwind so we need to handle that in addition to handling `token::Unknown`.
  • Loading branch information
ollie27 committed Jan 17, 2020
1 parent 689fca0 commit 79061d0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/librustdoc/html/highlight.rs
Expand Up @@ -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());

Expand All @@ -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) => {
Expand Down
5 changes: 3 additions & 2 deletions src/librustdoc/passes/check_code_block_syntax.rs
Expand Up @@ -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
Expand All @@ -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) =
Expand Down
6 changes: 6 additions & 0 deletions src/test/rustdoc-ui/invalid-syntax.rs
Expand Up @@ -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
15 changes: 15 additions & 0 deletions src/test/rustdoc-ui/invalid-syntax.stderr
Expand Up @@ -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
| ^^^^^^^

7 changes: 7 additions & 0 deletions src/test/rustdoc/bad-codeblock-syntax.rs
Expand Up @@ -33,3 +33,10 @@ pub fn ok() {}
/// <script>alert("not valid Rust");</script>
/// ```
pub fn escape() {}

// @has bad_codeblock_syntax/fn.unterminated.html
// @has - '//*[@class="docblock"]/pre/code' '"unterminated'
/// ```
/// "unterminated
/// ```
pub fn unterminated() {}

0 comments on commit 79061d0

Please sign in to comment.