Skip to content

Commit 849a0e9

Browse files
committed
Auto merge of #53933 - GuillaumeGomez:codeblock-error-display, r=QuietMisdreavus
Improve error display for codeblocks in rustdoc Part of #53919. r? @QuietMisdreavus
2 parents 8dc554a + 322e469 commit 849a0e9

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-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, "Output from rustc:") {
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

+13
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,19 @@ 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>,
244+
prepend_error_text: &str) -> Result<Self, ()> {
245+
let mut sr = StringReader::new_raw(sess, source_file, override_span);
246+
if sr.advance_token().is_err() {
247+
eprintln!("{}", prepend_error_text);
248+
sr.emit_fatal_errors();
249+
return Err(());
250+
}
251+
Ok(sr)
252+
}
253+
241254
pub fn retokenize(sess: &'a ParseSess, mut span: Span) -> Self {
242255
let begin = sess.source_map().lookup_byte_offset(span.lo());
243256
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() {}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Output from rustc:
2+
error: unknown start of token: /
3+
--> <stdin>:1:1
4+
|
5+
1 | /__________pkt->size___________/ /_result->size_/ /__pkt->size__/
6+
| ^
7+
8+
warning: Invalid doc comment starting with: `/__________pkt->size___________/ /_result->size_/ /__pkt->size__/`
9+
(Ignoring this codeblock)
10+

0 commit comments

Comments
 (0)