Skip to content

rustdoc: Catch fatal errors when syntax highlighting #68326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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() {}