Skip to content

Commit

Permalink
Rollup merge of rust-lang#48596 - GuillaumeGomez:invalid-code-block-s…
Browse files Browse the repository at this point in the history
…tart, r=QuietMisdreavus

Add warning for invalid start of code blocks in rustdoc

Follow up of rust-lang#48382.

Still two things to consider:
 1. Adding test for rustdoc output (but where? In UI or in rustdoc tests?).
 2. Try to fix the span issue.

r? @QuietMisdreavus
  • Loading branch information
kennytm committed Mar 21, 2018
2 parents c19264f + 7786f70 commit 82e2f36
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/librustdoc/html/markdown.rs
Expand Up @@ -27,6 +27,7 @@

#![allow(non_camel_case_types)]

use rustc::session;
use std::cell::RefCell;
use std::collections::{HashMap, VecDeque};
use std::default::Default;
Expand Down Expand Up @@ -434,7 +435,8 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for Footnotes<'a, I> {
}
}

pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Span) {
pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Span,
sess: Option<&session::Session>) {
tests.set_position(position);

let mut parser = Parser::new(doc);
Expand Down Expand Up @@ -484,6 +486,9 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Sp
line, filename, block_info.allow_fail);
prev_offset = offset;
} else {
if let Some(ref sess) = sess {
sess.span_warn(position, "invalid start of a new code block");
}
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/markdown.rs
Expand Up @@ -152,7 +152,7 @@ pub fn test(input: &str, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
true, opts, maybe_sysroot, None,
Some(PathBuf::from(input)),
linker);
find_testable_code(&input_str, &mut collector, DUMMY_SP);
find_testable_code(&input_str, &mut collector, DUMMY_SP, None);
test_args.insert(0, "rustdoctest".to_string());
testing::test_main(&test_args, collector.tests,
testing::Options::new().display_output(display_warnings));
Expand Down
6 changes: 4 additions & 2 deletions src/librustdoc/test.rs
Expand Up @@ -645,8 +645,10 @@ impl<'a, 'hir> HirCollector<'a, 'hir> {
// the collapse-docs pass won't combine sugared/raw doc attributes, or included files with
// anything else, this will combine them for us
if let Some(doc) = attrs.collapsed_doc_value() {
markdown::find_testable_code(&doc, self.collector,
attrs.span.unwrap_or(DUMMY_SP));
markdown::find_testable_code(&doc,
self.collector,
attrs.span.unwrap_or(DUMMY_SP),
Some(self.sess));
}

nested(self);
Expand Down

0 comments on commit 82e2f36

Please sign in to comment.