Skip to content

Commit 03e34f8

Browse files
Remove dependency on error handling from find_testable_code
1 parent de5cebd commit 03e34f8

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ use std::fmt::{self, Write};
3434
use std::borrow::Cow;
3535
use std::ops::Range;
3636
use std::str;
37-
use syntax::feature_gate::UnstableFeatures;
38-
use syntax::codemap::Span;
39-
use errors;
4037

38+
use syntax::feature_gate::UnstableFeatures;
4139
use html::render::derive_id;
4240
use html::toc::TocBuilder;
4341
use html::highlight;
@@ -469,10 +467,17 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for Footnotes<'a, I> {
469467
}
470468
}
471469

472-
pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Span,
473-
handler: &errors::Handler) {
474-
tests.set_position(position);
470+
pub struct TestableCodeError(());
471+
472+
impl fmt::Display for TestableCodeError {
473+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
474+
write!(f, "invalid start of a new code block")
475+
}
476+
}
475477

478+
pub fn find_testable_code(
479+
doc: &str, tests: &mut test::Collector
480+
) -> Result<(), TestableCodeError> {
476481
let is_nightly = UnstableFeatures::from_environment().is_nightly_build();
477482
let mut parser = Parser::new(doc);
478483
let mut prev_offset = 0;
@@ -516,8 +521,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Sp
516521
tests.add_test(text, block_info, line);
517522
prev_offset = offset;
518523
} else {
519-
handler.span_warn(position, "invalid start of a new code block");
520-
break;
524+
return Err(TestableCodeError(()));
521525
}
522526
}
523527
Event::Start(Tag::Header(level)) => {
@@ -535,6 +539,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Sp
535539
_ => {}
536540
}
537541
}
542+
Ok(())
538543
}
539544

540545
#[derive(Eq, PartialEq, Clone, Debug)]

src/librustdoc/markdown.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ pub fn test(input: &str, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
156156
true, opts, maybe_sysroot, None,
157157
Some(PathBuf::from(input)),
158158
linker, edition);
159-
find_testable_code(&input_str, &mut collector, DUMMY_SP, diag);
159+
collector.set_position(DUMMY_SP);
160+
let res = find_testable_code(&input_str, &mut collector);
161+
if let Err(err) = res {
162+
diag.span_warn(DUMMY_SP, &err.to_string());
163+
}
160164
test_args.insert(0, "rustdoctest".to_string());
161165
testing::test_main(&test_args, collector.tests,
162166
testing::Options::new().display_output(display_warnings));

src/librustdoc/test.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -687,10 +687,12 @@ impl<'a, 'hir> HirCollector<'a, 'hir> {
687687
// the collapse-docs pass won't combine sugared/raw doc attributes, or included files with
688688
// anything else, this will combine them for us
689689
if let Some(doc) = attrs.collapsed_doc_value() {
690-
markdown::find_testable_code(&doc,
691-
self.collector,
692-
attrs.span.unwrap_or(DUMMY_SP),
693-
self.sess.diagnostic());
690+
self.collector.set_position(attrs.span.unwrap_or(DUMMY_SP));
691+
let res = markdown::find_testable_code(&doc, self.collector);
692+
if let Err(err) = res {
693+
self.sess.diagnostic().span_warn(attrs.span.unwrap_or(DUMMY_SP),
694+
&err.to_string());
695+
}
694696
}
695697

696698
nested(self);

0 commit comments

Comments
 (0)