Skip to content

Commit a457e29

Browse files
committed
Auto merge of rust-lang#44867 - kennytm:rustdoc-md-test-title, r=alexcrichton
doc-test: In Markdown tests, Use all of `<h1>` to `<h6>` as the test name This mainly simplifies debugging error index tests, as the error codes are `<h2>`s in the huge document containing all codes.
2 parents 7778906 + 0cdf587 commit a457e29

File tree

2 files changed

+50
-30
lines changed

2 files changed

+50
-30
lines changed

src/librustc_resolve/diagnostics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ parameter if so.
4343
"##,
4444

4545
E0154: r##"
46-
## Note: this error code is no longer emitted by the compiler.
46+
#### Note: this error code is no longer emitted by the compiler.
4747
4848
Imports (`use` statements) are not allowed after non-item statements, such as
4949
variable declarations and expression statements.
@@ -79,7 +79,7 @@ https://doc.rust-lang.org/reference.html#statements
7979
"##,
8080

8181
E0251: r##"
82-
## Note: this error code is no longer emitted by the compiler.
82+
#### Note: this error code is no longer emitted by the compiler.
8383
8484
Two items of the same name cannot be imported without rebinding one of the
8585
items under a new local name.
@@ -268,7 +268,7 @@ fn main() {
268268
"##,
269269

270270
E0256: r##"
271-
## Note: this error code is no longer emitted by the compiler.
271+
#### Note: this error code is no longer emitted by the compiler.
272272
273273
You can't import a type or module when the name of the item being imported is
274274
the same as another type or submodule defined in the module.

src/librustdoc/test.rs

+47-27
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,33 @@ pub struct Collector {
407407
pub tests: Vec<testing::TestDescAndFn>,
408408
// to be removed when hoedown will be definitely gone
409409
pub old_tests: HashMap<String, Vec<String>>,
410+
411+
// The name of the test displayed to the user, separated by `::`.
412+
//
413+
// In tests from Rust source, this is the path to the item
414+
// e.g. `["std", "vec", "Vec", "push"]`.
415+
//
416+
// In tests from a markdown file, this is the titles of all headers (h1~h6)
417+
// of the sections that contain the code block, e.g. if the markdown file is
418+
// written as:
419+
//
420+
// ``````markdown
421+
// # Title
422+
//
423+
// ## Subtitle
424+
//
425+
// ```rust
426+
// assert!(true);
427+
// ```
428+
// ``````
429+
//
430+
// the `names` vector of that test will be `["Title", "Subtitle"]`.
410431
names: Vec<String>,
432+
411433
cfgs: Vec<String>,
412434
libs: SearchPaths,
413435
externs: Externs,
414-
cnt: usize,
415436
use_headers: bool,
416-
current_header: Option<String>,
417437
cratename: String,
418438
opts: TestOptions,
419439
maybe_sysroot: Option<PathBuf>,
@@ -436,9 +456,7 @@ impl Collector {
436456
cfgs,
437457
libs,
438458
externs,
439-
cnt: 0,
440459
use_headers,
441-
current_header: None,
442460
cratename,
443461
opts,
444462
maybe_sysroot,
@@ -450,28 +468,12 @@ impl Collector {
450468
}
451469

452470
fn generate_name(&self, line: usize, filename: &str) -> String {
453-
if self.use_headers {
454-
if let Some(ref header) = self.current_header {
455-
format!("{} - {} (line {})", filename, header, line)
456-
} else {
457-
format!("{} - (line {})", filename, line)
458-
}
459-
} else {
460-
format!("{} - {} (line {})", filename, self.names.join("::"), line)
461-
}
471+
format!("{} - {} (line {})", filename, self.names.join("::"), line)
462472
}
463473

464474
// to be removed once hoedown is gone
465475
fn generate_name_beginning(&self, filename: &str) -> String {
466-
if self.use_headers {
467-
if let Some(ref header) = self.current_header {
468-
format!("{} - {} (line", filename, header)
469-
} else {
470-
format!("{} - (line", filename)
471-
}
472-
} else {
473-
format!("{} - {} (line", filename, self.names.join("::"))
474-
}
476+
format!("{} - {} (line", filename, self.names.join("::"))
475477
}
476478

477479
pub fn add_old_test(&mut self, test: String, filename: String) {
@@ -579,7 +581,7 @@ impl Collector {
579581
}
580582

581583
pub fn register_header(&mut self, name: &str, level: u32) {
582-
if self.use_headers && level == 1 {
584+
if self.use_headers {
583585
// we use these headings as test names, so it's good if
584586
// they're valid identifiers.
585587
let name = name.chars().enumerate().map(|(i, c)| {
@@ -591,9 +593,28 @@ impl Collector {
591593
}
592594
}).collect::<String>();
593595

594-
// new header => reset count.
595-
self.cnt = 0;
596-
self.current_header = Some(name);
596+
// Here we try to efficiently assemble the header titles into the
597+
// test name in the form of `h1::h2::h3::h4::h5::h6`.
598+
//
599+
// Suppose originally `self.names` contains `[h1, h2, h3]`...
600+
let level = level as usize;
601+
if level <= self.names.len() {
602+
// ... Consider `level == 2`. All headers in the lower levels
603+
// are irrelevant in this new level. So we should reset
604+
// `self.names` to contain headers until <h2>, and replace that
605+
// slot with the new name: `[h1, name]`.
606+
self.names.truncate(level);
607+
self.names[level - 1] = name;
608+
} else {
609+
// ... On the other hand, consider `level == 5`. This means we
610+
// need to extend `self.names` to contain five headers. We fill
611+
// in the missing level (<h4>) with `_`. Thus `self.names` will
612+
// become `[h1, h2, h3, "_", name]`.
613+
if level - 1 > self.names.len() {
614+
self.names.resize(level - 1, "_".to_owned());
615+
}
616+
self.names.push(name);
617+
}
597618
}
598619
}
599620
}
@@ -624,7 +645,6 @@ impl<'a, 'hir> HirCollector<'a, 'hir> {
624645
attrs.collapse_doc_comments();
625646
attrs.unindent_doc_comments();
626647
if let Some(doc) = attrs.doc_value() {
627-
self.collector.cnt = 0;
628648
if self.collector.render_type == RenderType::Pulldown {
629649
markdown::old_find_testable_code(doc, self.collector,
630650
attrs.span.unwrap_or(DUMMY_SP));

0 commit comments

Comments
 (0)