Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: do not panic all the way when lexing a source snippet fails #33510

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use html::escape::Escape;

use std::fmt::Display;
use std::io;
use std::panic::catch_unwind;
use std::io::prelude::*;

use syntax::codemap::{CodeMap, Span};
Expand All @@ -34,20 +35,28 @@ use syntax::parse;
/// Highlights `src`, returning the HTML output.
pub fn render_with_highlighting(src: &str, class: Option<&str>, id: Option<&str>) -> String {
debug!("highlighting: ================\n{}\n==============", src);
let sess = parse::ParseSess::new();
let fm = sess.codemap().new_filemap("<stdin>".to_string(), src.to_string());

let mut out = Vec::new();
write_header(class, id, &mut out).unwrap();

let mut classifier = Classifier::new(lexer::StringReader::new(&sess.span_diagnostic, fm),
sess.codemap());
if let Err(_) = classifier.write_source(&mut out) {
return format!("<pre>{}</pre>", src);
// As long as the lexer panics...
let result = catch_unwind(|| {
let sess = parse::ParseSess::new();
let fm = sess.codemap().new_filemap("<stdin>".to_string(), src.to_string());
let mut out = Vec::new();
write_header(class, id, &mut out).unwrap();
let mut classifier = Classifier::new(lexer::StringReader::new(&sess.span_diagnostic, fm),
sess.codemap());
classifier.write_source(&mut out).unwrap();
write_footer(&mut out).unwrap();
out
});
match result {
// catches both panics from the lexer and the unwrap()s
Err(_) => {
println!("warning: error during source highlighting (see above), \
passing source through unhighlighted");
format!("<pre>{}</pre>", src)
},
Ok(out) => String::from_utf8_lossy(&out[..]).into_owned(),
}

write_footer(&mut out).unwrap();
String::from_utf8_lossy(&out[..]).into_owned()
}

/// Highlights `src`, returning the HTML output. Returns only the inner html to
Expand Down
18 changes: 18 additions & 0 deletions src/test/rustdoc/issue-30032.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// This used to panic due to the invalid source, so just checking for
// existence of the output file is enough.

// @has issue_30032/fn.main.html
/// ```ignore
/// let invalid = 'abc';
/// ```
pub fn main() {}