Skip to content

Commit

Permalink
buffer lexer errors in rustdoc syntax checking
Browse files Browse the repository at this point in the history
  • Loading branch information
euclio committed Jan 5, 2020
1 parent 760ce94 commit 1ad928e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 112 deletions.
32 changes: 29 additions & 3 deletions src/librustdoc/passes/check_code_block_syntax.rs
@@ -1,6 +1,7 @@
use errors::Applicability;
use errors::{emitter::Emitter, Applicability, Diagnostic, Handler};
use rustc_data_structures::sync::{Lock, Lrc};
use rustc_parse::lexer::StringReader as Lexer;
use rustc_span::source_map::FilePathMapping;
use rustc_span::source_map::{FilePathMapping, SourceMap};
use rustc_span::{FileName, InnerSpan};
use syntax::sess::ParseSess;
use syntax::token;
Expand All @@ -27,7 +28,13 @@ struct SyntaxChecker<'a, 'tcx> {

impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
fn check_rust_syntax(&self, item: &clean::Item, dox: &str, code_block: RustCodeBlock) {
let sess = ParseSess::new(FilePathMapping::empty());
let buffered_messages = Lrc::new(Lock::new(vec![]));

let emitter = BufferEmitter { messages: Lrc::clone(&buffered_messages) };

let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
let handler = Handler::with_emitter(false, None, Box::new(emitter));
let sess = ParseSess::with_span_handler(handler, cm);
let source_file = sess.source_map().new_source_file(
FileName::Custom(String::from("doctest")),
dox[code_block.code].to_owned(),
Expand Down Expand Up @@ -93,6 +100,11 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
diag
};

// FIXME(#67563): Provide more context for these errors by displaying the spans inline.
for message in buffered_messages.borrow().iter() {
diag.note(&message);
}

diag.emit();
}
}
Expand All @@ -110,6 +122,20 @@ impl<'a, 'tcx> DocFolder for SyntaxChecker<'a, 'tcx> {
}
}

struct BufferEmitter {
messages: Lrc<Lock<Vec<String>>>,
}

impl Emitter for BufferEmitter {
fn emit_diagnostic(&mut self, diag: &Diagnostic) {
self.messages.borrow_mut().push(format!("error from rustc: {}", diag.message[0].0));
}

fn source_map(&self) -> Option<&Lrc<SourceMap>> {
None
}
}

enum CodeBlockInvalid {
SyntaxError,
Empty,
Expand Down
128 changes: 19 additions & 109 deletions src/test/rustdoc-ui/invalid-syntax.stderr
@@ -1,21 +1,3 @@
error: unknown start of token: \
--> <doctest>:1:1
|
1 | \__________pkt->size___________/ \_result->size_/ \__pkt->size__/
| ^

error: unknown start of token: \
--> <doctest>:1:43
|
1 | \__________pkt->size___________/ \_result->size_/ \__pkt->size__/
| ^

error: unknown start of token: \
--> <doctest>:1:60
|
1 | \__________pkt->size___________/ \_result->size_/ \__pkt->size__/
| ^

warning: could not parse code block as Rust code
--> $DIR/invalid-syntax.rs:3:5
|
Expand All @@ -25,33 +7,14 @@ LL | | /// \__________pkt->size___________/ \_result->size_/ \__pkt->si
LL | | /// ```
| |_______^
|
= note: error from rustc: unknown start of token: \
= note: error from rustc: unknown start of token: \
= note: error from rustc: unknown start of token: \
help: mark blocks that do not contain Rust code as text
|
LL | /// ```text
| ^^^^^^^

error: unknown start of token: `
--> <doctest>:3:30
|
3 | | ^^^^^^ did you mean `baz::foobar`?
| ^
|
help: Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not
|
3 | | ^^^^^^ did you mean 'baz::foobar`?
| ^

error: unknown start of token: `
--> <doctest>:3:42
|
3 | | ^^^^^^ did you mean `baz::foobar`?
| ^
|
help: Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not
|
3 | | ^^^^^^ did you mean `baz::foobar'?
| ^

warning: could not parse code block as Rust code
--> $DIR/invalid-syntax.rs:9:5
|
Expand All @@ -63,17 +26,13 @@ LL | | /// | ^^^^^^ did you mean `baz::foobar`?
LL | | /// ```
| |_______^
|
= note: error from rustc: unknown start of token: `
= note: error from rustc: unknown start of token: `
help: mark blocks that do not contain Rust code as text
|
LL | /// ```text
| ^^^^^^^

error: unknown start of token: \
--> <doctest>:1:1
|
1 | \_
| ^

warning: could not parse code block as Rust code
--> $DIR/invalid-syntax.rs:21:5
|
Expand All @@ -83,17 +42,12 @@ LL | | /// \_
LL | | /// ```
| |_______^
|
= note: error from rustc: unknown start of token: \
help: mark blocks that do not contain Rust code as text
|
LL | /// ```text
| ^^^^^^^

error: unknown start of token: \
--> <doctest>:1:1
|
1 | \_
| ^

warning: could not parse code block as Rust code
--> $DIR/invalid-syntax.rs:35:5
|
Expand All @@ -102,12 +56,8 @@ LL | /// ```rust
LL | | /// \_
LL | | /// ```
| |_______^

error: unknown start of token: \
--> <doctest>:2:5
|
2 | \_
| ^
|
= note: error from rustc: unknown start of token: \

warning: could not parse code block as Rust code
--> $DIR/invalid-syntax.rs:45:9
Expand All @@ -116,51 +66,18 @@ LL | /// code with bad syntax
| _________^
LL | | /// \_
| |__________^

error: unknown start of token: `
--> <doctest>:1:1
|
1 | ```
| ^
|
help: Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not
|
1 | '``
| ^

error: unknown start of token: `
--> <doctest>:1:2
|
1 | ```
| ^
|
help: Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not
|
1 | `'`
| ^

error: unknown start of token: `
--> <doctest>:1:3
|
1 | ```
| ^
|
help: Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not
|
1 | ``'
| ^
|
= note: error from rustc: unknown start of token: \

warning: could not parse code block as Rust code
--> $DIR/invalid-syntax.rs:60:9
|
LL | /// ```
| ^^^

error: unknown start of token: \
--> <doctest>:1:1
|
1 | \_
| ^
|
= note: error from rustc: unknown start of token: `
= note: error from rustc: unknown start of token: `
= note: error from rustc: unknown start of token: `

warning: could not parse code block as Rust code
--> $DIR/invalid-syntax.rs:64:5
Expand All @@ -170,12 +87,8 @@ LL | /// ```edition2018
LL | | /// \_
LL | | /// ```
| |_______^

error: unknown start of token: \
--> <doctest>:1:1
|
1 | \_
| ^
|
= note: error from rustc: unknown start of token: \

warning: doc comment contains an invalid Rust code block
--> $DIR/invalid-syntax.rs:70:1
Expand All @@ -186,6 +99,7 @@ LL | | #[doc = "```"]
| |______________^
|
= help: mark blocks that do not contain Rust code as text: ```text
= note: error from rustc: unknown start of token: \

warning: Rust code block is empty
--> $DIR/invalid-syntax.rs:76:5
Expand All @@ -210,15 +124,11 @@ help: mark blocks that do not contain Rust code as text
LL | /// ```text
| ^^^^^^^

error: unknown start of token: \
--> <doctest>:1:1
|
1 | \____/
| ^

warning: could not parse code block as Rust code
--> $DIR/invalid-syntax.rs:92:9
|
LL | /// \____/
| ^^^^^^
|
= note: error from rustc: unknown start of token: \

0 comments on commit 1ad928e

Please sign in to comment.