Skip to content

Commit f7240e1

Browse files
Improve error display for codeblocks in rustdoc
1 parent 20dc0c5 commit f7240e1

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

src/librustdoc/html/highlight.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,21 @@ pub fn render_with_highlighting(src: &str, class: Option<&str>,
4444
}
4545
write_header(class, &mut out).unwrap();
4646

47-
let mut classifier = Classifier::new(lexer::StringReader::new(&sess, fm, None),
48-
sess.source_map());
47+
let lexer = match lexer::StringReader::new_without_err(&sess, fm, None) {
48+
Ok(l) => l,
49+
Err(_) => {
50+
let first_line = src.lines().next().unwrap_or_else(|| "");
51+
let mut err = sess.span_diagnostic
52+
.struct_warn(&format!("Invalid doc comment starting with: `{}`\n\
53+
(Ignoring this codeblock)",
54+
first_line));
55+
err.emit();
56+
return String::new();
57+
}
58+
};
59+
let mut classifier = Classifier::new(lexer, sess.source_map());
4960
if classifier.write_source(&mut out).is_err() {
61+
classifier.lexer.emit_fatal_errors();
5062
return format!("<pre>{}</pre>", src);
5163
}
5264

@@ -162,11 +174,10 @@ impl<'a> Classifier<'a> {
162174
match self.lexer.try_next_token() {
163175
Ok(tas) => Ok(tas),
164176
Err(_) => {
165-
self.lexer.emit_fatal_errors();
166-
self.lexer.sess.span_diagnostic
167-
.struct_warn("Backing out of syntax highlighting")
168-
.note("You probably did not intend to render this as a rust code-block")
169-
.emit();
177+
let mut err = self.lexer.sess.span_diagnostic
178+
.struct_warn("Backing out of syntax highlighting");
179+
err.note("You probably did not intend to render this as a rust code-block");
180+
err.emit();
170181
Err(io::Error::new(io::ErrorKind::Other, ""))
171182
}
172183
}

src/libsyntax/parse/lexer/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,17 @@ impl<'a> StringReader<'a> {
238238
sr
239239
}
240240

241+
pub fn new_without_err(sess: &'a ParseSess,
242+
source_file: Lrc<syntax_pos::SourceFile>,
243+
override_span: Option<Span>) -> Result<Self, ()> {
244+
let mut sr = StringReader::new_raw(sess, source_file, override_span);
245+
if sr.advance_token().is_err() {
246+
sr.emit_fatal_errors();
247+
return Err(());
248+
}
249+
Ok(sr)
250+
}
251+
241252
pub fn retokenize(sess: &'a ParseSess, mut span: Span) -> Self {
242253
let begin = sess.source_map().lookup_byte_offset(span.lo());
243254
let end = sess.source_map().lookup_byte_offset(span.hi());

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

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-pass
12+
// compile-flags: --error-format=human
13+
14+
/// ```
15+
/// \__________pkt->size___________/ \_result->size_/ \__pkt->size__/
16+
/// ```
17+
pub fn foo() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error: unknown start of token: /
2+
--> <stdin>:1:1
3+
|
4+
1 | /__________pkt->size___________/ /_result->size_/ /__pkt->size__/
5+
| ^
6+
7+
warning: Invalid doc comment starting with: `/__________pkt->size___________/ /_result->size_/ /__pkt->size__/`
8+
(Ignoring this codeblock)
9+

0 commit comments

Comments
 (0)