Skip to content

Commit 0cdf587

Browse files
committed
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.
1 parent bb4d149 commit 0cdf587

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) {
@@ -580,7 +582,7 @@ impl Collector {
580582
}
581583

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

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

0 commit comments

Comments
 (0)