From 0cdf587ab356128d07f96e3d2dff255e1ebb5097 Mon Sep 17 00:00:00 2001 From: kennytm Date: Sat, 16 Sep 2017 15:59:15 +0800 Subject: [PATCH] doc-test: In Markdown tests, Use all of `

` to `

` as the test name. This mainly simplifies debugging error index tests, as the error codes are `

`s in the huge document containing all codes. --- src/librustc_resolve/diagnostics.rs | 6 +-- src/librustdoc/test.rs | 74 ++++++++++++++++++----------- 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 9193ac0fcd66e..564626ac39885 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -43,7 +43,7 @@ parameter if so. "##, E0154: r##" -## Note: this error code is no longer emitted by the compiler. +#### Note: this error code is no longer emitted by the compiler. Imports (`use` statements) are not allowed after non-item statements, such as variable declarations and expression statements. @@ -79,7 +79,7 @@ https://doc.rust-lang.org/reference.html#statements "##, E0251: r##" -## Note: this error code is no longer emitted by the compiler. +#### Note: this error code is no longer emitted by the compiler. Two items of the same name cannot be imported without rebinding one of the items under a new local name. @@ -268,7 +268,7 @@ fn main() { "##, E0256: r##" -## Note: this error code is no longer emitted by the compiler. +#### Note: this error code is no longer emitted by the compiler. You can't import a type or module when the name of the item being imported is the same as another type or submodule defined in the module. diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index 5ce73d38fdf0e..2cd1261e7953d 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -407,13 +407,33 @@ pub struct Collector { pub tests: Vec, // to be removed when hoedown will be definitely gone pub old_tests: HashMap>, + + // The name of the test displayed to the user, separated by `::`. + // + // In tests from Rust source, this is the path to the item + // e.g. `["std", "vec", "Vec", "push"]`. + // + // In tests from a markdown file, this is the titles of all headers (h1~h6) + // of the sections that contain the code block, e.g. if the markdown file is + // written as: + // + // ``````markdown + // # Title + // + // ## Subtitle + // + // ```rust + // assert!(true); + // ``` + // `````` + // + // the `names` vector of that test will be `["Title", "Subtitle"]`. names: Vec, + cfgs: Vec, libs: SearchPaths, externs: Externs, - cnt: usize, use_headers: bool, - current_header: Option, cratename: String, opts: TestOptions, maybe_sysroot: Option, @@ -436,9 +456,7 @@ impl Collector { cfgs, libs, externs, - cnt: 0, use_headers, - current_header: None, cratename, opts, maybe_sysroot, @@ -450,28 +468,12 @@ impl Collector { } fn generate_name(&self, line: usize, filename: &str) -> String { - if self.use_headers { - if let Some(ref header) = self.current_header { - format!("{} - {} (line {})", filename, header, line) - } else { - format!("{} - (line {})", filename, line) - } - } else { - format!("{} - {} (line {})", filename, self.names.join("::"), line) - } + format!("{} - {} (line {})", filename, self.names.join("::"), line) } // to be removed once hoedown is gone fn generate_name_beginning(&self, filename: &str) -> String { - if self.use_headers { - if let Some(ref header) = self.current_header { - format!("{} - {} (line", filename, header) - } else { - format!("{} - (line", filename) - } - } else { - format!("{} - {} (line", filename, self.names.join("::")) - } + format!("{} - {} (line", filename, self.names.join("::")) } pub fn add_old_test(&mut self, test: String, filename: String) { @@ -580,7 +582,7 @@ impl Collector { } pub fn register_header(&mut self, name: &str, level: u32) { - if self.use_headers && level == 1 { + if self.use_headers { // we use these headings as test names, so it's good if // they're valid identifiers. let name = name.chars().enumerate().map(|(i, c)| { @@ -592,9 +594,28 @@ impl Collector { } }).collect::(); - // new header => reset count. - self.cnt = 0; - self.current_header = Some(name); + // Here we try to efficiently assemble the header titles into the + // test name in the form of `h1::h2::h3::h4::h5::h6`. + // + // Suppose originally `self.names` contains `[h1, h2, h3]`... + let level = level as usize; + if level <= self.names.len() { + // ... Consider `level == 2`. All headers in the lower levels + // are irrelevant in this new level. So we should reset + // `self.names` to contain headers until

, and replace that + // slot with the new name: `[h1, name]`. + self.names.truncate(level); + self.names[level - 1] = name; + } else { + // ... On the other hand, consider `level == 5`. This means we + // need to extend `self.names` to contain five headers. We fill + // in the missing level (

) with `_`. Thus `self.names` will + // become `[h1, h2, h3, "_", name]`. + if level - 1 > self.names.len() { + self.names.resize(level - 1, "_".to_owned()); + } + self.names.push(name); + } } } } @@ -625,7 +646,6 @@ impl<'a, 'hir> HirCollector<'a, 'hir> { attrs.collapse_doc_comments(); attrs.unindent_doc_comments(); if let Some(doc) = attrs.doc_value() { - self.collector.cnt = 0; if self.collector.render_type == RenderType::Pulldown { markdown::old_find_testable_code(doc, self.collector, attrs.span.unwrap_or(DUMMY_SP));