Skip to content

Commit 33640f0

Browse files
committed
Auto merge of #67563 - euclio:rustdoc-buffer-lexer, r=GuillaumeGomez
buffer lexer errors in rustdoc syntax checking The code isn't ideal (I really would like to display the errors inline), but this at least gets us to where we were before #63017.
2 parents 0731573 + 1ad928e commit 33640f0

File tree

2 files changed

+48
-112
lines changed

2 files changed

+48
-112
lines changed

src/librustdoc/passes/check_code_block_syntax.rs

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use errors::Applicability;
1+
use errors::{emitter::Emitter, Applicability, Diagnostic, Handler};
2+
use rustc_data_structures::sync::{Lock, Lrc};
23
use rustc_parse::lexer::StringReader as Lexer;
3-
use rustc_span::source_map::FilePathMapping;
4+
use rustc_span::source_map::{FilePathMapping, SourceMap};
45
use rustc_span::{FileName, InnerSpan};
56
use syntax::sess::ParseSess;
67
use syntax::token;
@@ -27,7 +28,13 @@ struct SyntaxChecker<'a, 'tcx> {
2728

2829
impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
2930
fn check_rust_syntax(&self, item: &clean::Item, dox: &str, code_block: RustCodeBlock) {
30-
let sess = ParseSess::new(FilePathMapping::empty());
31+
let buffered_messages = Lrc::new(Lock::new(vec![]));
32+
33+
let emitter = BufferEmitter { messages: Lrc::clone(&buffered_messages) };
34+
35+
let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
36+
let handler = Handler::with_emitter(false, None, Box::new(emitter));
37+
let sess = ParseSess::with_span_handler(handler, cm);
3138
let source_file = sess.source_map().new_source_file(
3239
FileName::Custom(String::from("doctest")),
3340
dox[code_block.code].to_owned(),
@@ -93,6 +100,11 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
93100
diag
94101
};
95102

103+
// FIXME(#67563): Provide more context for these errors by displaying the spans inline.
104+
for message in buffered_messages.borrow().iter() {
105+
diag.note(&message);
106+
}
107+
96108
diag.emit();
97109
}
98110
}
@@ -110,6 +122,20 @@ impl<'a, 'tcx> DocFolder for SyntaxChecker<'a, 'tcx> {
110122
}
111123
}
112124

125+
struct BufferEmitter {
126+
messages: Lrc<Lock<Vec<String>>>,
127+
}
128+
129+
impl Emitter for BufferEmitter {
130+
fn emit_diagnostic(&mut self, diag: &Diagnostic) {
131+
self.messages.borrow_mut().push(format!("error from rustc: {}", diag.message[0].0));
132+
}
133+
134+
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
135+
None
136+
}
137+
}
138+
113139
enum CodeBlockInvalid {
114140
SyntaxError,
115141
Empty,
+19-109
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
1-
error: unknown start of token: \
2-
--> <doctest>:1:1
3-
|
4-
1 | \__________pkt->size___________/ \_result->size_/ \__pkt->size__/
5-
| ^
6-
7-
error: unknown start of token: \
8-
--> <doctest>:1:43
9-
|
10-
1 | \__________pkt->size___________/ \_result->size_/ \__pkt->size__/
11-
| ^
12-
13-
error: unknown start of token: \
14-
--> <doctest>:1:60
15-
|
16-
1 | \__________pkt->size___________/ \_result->size_/ \__pkt->size__/
17-
| ^
18-
191
warning: could not parse code block as Rust code
202
--> $DIR/invalid-syntax.rs:3:5
213
|
@@ -25,33 +7,14 @@ LL | | /// \__________pkt->size___________/ \_result->size_/ \__pkt->si
257
LL | | /// ```
268
| |_______^
279
|
10+
= note: error from rustc: unknown start of token: \
11+
= note: error from rustc: unknown start of token: \
12+
= note: error from rustc: unknown start of token: \
2813
help: mark blocks that do not contain Rust code as text
2914
|
3015
LL | /// ```text
3116
| ^^^^^^^
3217

33-
error: unknown start of token: `
34-
--> <doctest>:3:30
35-
|
36-
3 | | ^^^^^^ did you mean `baz::foobar`?
37-
| ^
38-
|
39-
help: Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not
40-
|
41-
3 | | ^^^^^^ did you mean 'baz::foobar`?
42-
| ^
43-
44-
error: unknown start of token: `
45-
--> <doctest>:3:42
46-
|
47-
3 | | ^^^^^^ did you mean `baz::foobar`?
48-
| ^
49-
|
50-
help: Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not
51-
|
52-
3 | | ^^^^^^ did you mean `baz::foobar'?
53-
| ^
54-
5518
warning: could not parse code block as Rust code
5619
--> $DIR/invalid-syntax.rs:9:5
5720
|
@@ -63,17 +26,13 @@ LL | | /// | ^^^^^^ did you mean `baz::foobar`?
6326
LL | | /// ```
6427
| |_______^
6528
|
29+
= note: error from rustc: unknown start of token: `
30+
= note: error from rustc: unknown start of token: `
6631
help: mark blocks that do not contain Rust code as text
6732
|
6833
LL | /// ```text
6934
| ^^^^^^^
7035

71-
error: unknown start of token: \
72-
--> <doctest>:1:1
73-
|
74-
1 | \_
75-
| ^
76-
7736
warning: could not parse code block as Rust code
7837
--> $DIR/invalid-syntax.rs:21:5
7938
|
@@ -83,17 +42,12 @@ LL | | /// \_
8342
LL | | /// ```
8443
| |_______^
8544
|
45+
= note: error from rustc: unknown start of token: \
8646
help: mark blocks that do not contain Rust code as text
8747
|
8848
LL | /// ```text
8949
| ^^^^^^^
9050

91-
error: unknown start of token: \
92-
--> <doctest>:1:1
93-
|
94-
1 | \_
95-
| ^
96-
9751
warning: could not parse code block as Rust code
9852
--> $DIR/invalid-syntax.rs:35:5
9953
|
@@ -102,12 +56,8 @@ LL | /// ```rust
10256
LL | | /// \_
10357
LL | | /// ```
10458
| |_______^
105-
106-
error: unknown start of token: \
107-
--> <doctest>:2:5
108-
|
109-
2 | \_
110-
| ^
59+
|
60+
= note: error from rustc: unknown start of token: \
11161

11262
warning: could not parse code block as Rust code
11363
--> $DIR/invalid-syntax.rs:45:9
@@ -116,51 +66,18 @@ LL | /// code with bad syntax
11666
| _________^
11767
LL | | /// \_
11868
| |__________^
119-
120-
error: unknown start of token: `
121-
--> <doctest>:1:1
122-
|
123-
1 | ```
124-
| ^
125-
|
126-
help: Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not
127-
|
128-
1 | '``
129-
| ^
130-
131-
error: unknown start of token: `
132-
--> <doctest>:1:2
133-
|
134-
1 | ```
135-
| ^
136-
|
137-
help: Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not
138-
|
139-
1 | `'`
140-
| ^
141-
142-
error: unknown start of token: `
143-
--> <doctest>:1:3
144-
|
145-
1 | ```
146-
| ^
147-
|
148-
help: Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not
149-
|
150-
1 | ``'
151-
| ^
69+
|
70+
= note: error from rustc: unknown start of token: \
15271

15372
warning: could not parse code block as Rust code
15473
--> $DIR/invalid-syntax.rs:60:9
15574
|
15675
LL | /// ```
15776
| ^^^
158-
159-
error: unknown start of token: \
160-
--> <doctest>:1:1
161-
|
162-
1 | \_
163-
| ^
77+
|
78+
= note: error from rustc: unknown start of token: `
79+
= note: error from rustc: unknown start of token: `
80+
= note: error from rustc: unknown start of token: `
16481

16582
warning: could not parse code block as Rust code
16683
--> $DIR/invalid-syntax.rs:64:5
@@ -170,12 +87,8 @@ LL | /// ```edition2018
17087
LL | | /// \_
17188
LL | | /// ```
17289
| |_______^
173-
174-
error: unknown start of token: \
175-
--> <doctest>:1:1
176-
|
177-
1 | \_
178-
| ^
90+
|
91+
= note: error from rustc: unknown start of token: \
17992

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

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

213-
error: unknown start of token: \
214-
--> <doctest>:1:1
215-
|
216-
1 | \____/
217-
| ^
218-
219127
warning: could not parse code block as Rust code
220128
--> $DIR/invalid-syntax.rs:92:9
221129
|
222130
LL | /// \____/
223131
| ^^^^^^
132+
|
133+
= note: error from rustc: unknown start of token: \
224134

0 commit comments

Comments
 (0)