Skip to content

Commit 0dc2557

Browse files
authoredJan 20, 2020
Rollup merge of #68326 - ollie27:rustdoc_hightlight_fatal_errors, r=GuillaumeGomez
rustdoc: Catch fatal errors when syntax highlighting For some errors the lexer will unwind so we need to handle that in addition to handling `token::Unknown`. Fixes #56885 r? @GuillaumeGomez
2 parents 29b854f + 79061d0 commit 0dc2557

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed
 

‎src/librustdoc/html/highlight.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn render_with_highlighting(
4141
let fm = sess
4242
.source_map()
4343
.new_source_file(FileName::Custom(String::from("rustdoc-highlighting")), src.to_owned());
44-
let highlight_result = {
44+
let highlight_result = rustc_driver::catch_fatal_errors(|| {
4545
let lexer = lexer::StringReader::new(&sess, fm, None);
4646
let mut classifier = Classifier::new(lexer, sess.source_map());
4747

@@ -51,7 +51,8 @@ pub fn render_with_highlighting(
5151
} else {
5252
Ok(String::from_utf8_lossy(&highlighted_source).into_owned())
5353
}
54-
};
54+
})
55+
.unwrap_or(Err(()));
5556

5657
match highlight_result {
5758
Ok(highlighted_source) => {

‎src/librustdoc/passes/check_code_block_syntax.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
4040
dox[code_block.code].to_owned(),
4141
);
4242

43-
let validation_status = {
43+
let validation_status = rustc_driver::catch_fatal_errors(|| {
4444
let mut has_syntax_errors = false;
4545
let mut only_whitespace = true;
4646
// 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> {
6161
} else {
6262
None
6363
}
64-
};
64+
})
65+
.unwrap_or(Some(CodeBlockInvalid::SyntaxError));
6566

6667
if let Some(code_block_invalid) = validation_status {
6768
let mut diag = if let Some(sp) =

‎src/test/rustdoc-ui/invalid-syntax.rs

+6
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,9 @@ pub fn empty_rust_with_whitespace() {}
9393
///
9494
pub fn indent_after_fenced() {}
9595
//~^^^ WARNING could not parse code block as Rust code
96+
97+
/// ```
98+
/// "invalid
99+
/// ```
100+
pub fn invalid() {}
101+
//~^^^^ WARNING could not parse code block as Rust code

‎src/test/rustdoc-ui/invalid-syntax.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,18 @@ LL | /// \____/
132132
|
133133
= note: error from rustc: unknown start of token: \
134134

135+
warning: could not parse code block as Rust code
136+
--> $DIR/invalid-syntax.rs:97:5
137+
|
138+
LL | /// ```
139+
| _____^
140+
LL | | /// "invalid
141+
LL | | /// ```
142+
| |_______^
143+
|
144+
= note: error from rustc: unterminated double quote string
145+
help: mark blocks that do not contain Rust code as text
146+
|
147+
LL | /// ```text
148+
| ^^^^^^^
149+

‎src/test/rustdoc/bad-codeblock-syntax.rs

+7
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,10 @@ pub fn ok() {}
3333
/// <script>alert("not valid Rust");</script>
3434
/// ```
3535
pub fn escape() {}
36+
37+
// @has bad_codeblock_syntax/fn.unterminated.html
38+
// @has - '//*[@class="docblock"]/pre/code' '"unterminated'
39+
/// ```
40+
/// "unterminated
41+
/// ```
42+
pub fn unterminated() {}

0 commit comments

Comments
 (0)
Please sign in to comment.