Skip to content

Commit 35135e2

Browse files
committed
rustdoc: Remove DoctestVisitor::get_line
This was used to get the line number of the first line from the current docstring, which was then used together with an offset within the docstring. It's simpler to just pass the offset to the visitor and have it do the math because it's clearer and this calculation only needs to be done in one place (the Rust doctest visitor).
1 parent 473c14b commit 35135e2

File tree

6 files changed

+44
-26
lines changed

6 files changed

+44
-26
lines changed

src/librustdoc/doctest.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use std::sync::{Arc, Mutex};
3434
use tempfile::{Builder as TempFileBuilder, TempDir};
3535

3636
use crate::config::Options as RustdocOptions;
37-
use crate::html::markdown::{ErrorCodes, Ignore, LangString};
37+
use crate::html::markdown::{ErrorCodes, Ignore, LangString, MdRelLine};
3838
use crate::lint::init_lints;
3939

4040
use self::rust::HirCollector;
@@ -962,10 +962,7 @@ struct ScrapedDoctest {
962962
}
963963

964964
pub(crate) trait DoctestVisitor {
965-
fn visit_test(&mut self, test: String, config: LangString, line: usize);
966-
fn get_line(&self) -> usize {
967-
0
968-
}
965+
fn visit_test(&mut self, test: String, config: LangString, rel_line: MdRelLine);
969966
fn visit_header(&mut self, _name: &str, _level: u32) {}
970967
}
971968

@@ -1188,8 +1185,8 @@ fn doctest_run_fn(
11881185

11891186
#[cfg(test)] // used in tests
11901187
impl DoctestVisitor for Vec<usize> {
1191-
fn visit_test(&mut self, _test: String, _config: LangString, line: usize) {
1192-
self.push(line);
1188+
fn visit_test(&mut self, _test: String, _config: LangString, rel_line: MdRelLine) {
1189+
self.push(1 + rel_line.offset());
11931190
}
11941191
}
11951192

src/librustdoc/doctest/markdown.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use super::{
99
generate_args_file, CreateRunnableDoctests, DoctestVisitor, GlobalTestOptions, ScrapedDoctest,
1010
};
1111
use crate::config::Options;
12-
use crate::html::markdown::{find_testable_code, ErrorCodes, LangString};
12+
use crate::html::markdown::{find_testable_code, ErrorCodes, LangString, MdRelLine};
1313

1414
struct MdCollector {
1515
tests: Vec<ScrapedDoctest>,
@@ -18,8 +18,10 @@ struct MdCollector {
1818
}
1919

2020
impl DoctestVisitor for MdCollector {
21-
fn visit_test(&mut self, test: String, config: LangString, line: usize) {
21+
fn visit_test(&mut self, test: String, config: LangString, rel_line: MdRelLine) {
2222
let filename = self.filename.clone();
23+
// First line of Markdown is line 1.
24+
let line = 1 + rel_line.offset();
2325
self.tests.push(ScrapedDoctest {
2426
filename,
2527
line,
@@ -29,10 +31,6 @@ impl DoctestVisitor for MdCollector {
2931
});
3032
}
3133

32-
fn get_line(&self) -> usize {
33-
0
34-
}
35-
3634
fn visit_header(&mut self, name: &str, level: u32) {
3735
// We use these headings as test names, so it's good if
3836
// they're valid identifiers.

src/librustdoc/doctest/rust.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_span::{BytePos, FileName, Pos, Span, DUMMY_SP};
1515

1616
use super::{DoctestVisitor, ScrapedDoctest};
1717
use crate::clean::{types::AttributesExt, Attributes};
18-
use crate::html::markdown::{self, ErrorCodes, LangString};
18+
use crate::html::markdown::{self, ErrorCodes, LangString, MdRelLine};
1919

2020
struct RustCollector {
2121
source_map: Lrc<SourceMap>,
@@ -36,10 +36,17 @@ impl RustCollector {
3636
}
3737
filename
3838
}
39+
40+
fn get_base_line(&self) -> usize {
41+
let sp_lo = self.position.lo().to_usize();
42+
let loc = self.source_map.lookup_char_pos(BytePos(sp_lo as u32));
43+
loc.line
44+
}
3945
}
4046

4147
impl DoctestVisitor for RustCollector {
42-
fn visit_test(&mut self, test: String, config: LangString, line: usize) {
48+
fn visit_test(&mut self, test: String, config: LangString, rel_line: MdRelLine) {
49+
let line = self.get_base_line() + rel_line.offset();
4350
self.tests.push(ScrapedDoctest {
4451
filename: self.get_filename(),
4552
line,
@@ -49,12 +56,6 @@ impl DoctestVisitor for RustCollector {
4956
});
5057
}
5158

52-
fn get_line(&self) -> usize {
53-
let line = self.position.lo().to_usize();
54-
let line = self.source_map.lookup_char_pos(BytePos(line as u32)).line;
55-
if line > 0 { line - 1 } else { line }
56-
}
57-
5859
fn visit_header(&mut self, _name: &str, _level: u32) {}
5960
}
6061

src/librustdoc/html/markdown.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,28 @@ impl<'a, I: Iterator<Item = SpannedEvent<'a>>> Iterator for Footnotes<'a, I> {
727727
}
728728
}
729729

730+
/// A newtype that represents a relative line number in Markdown.
731+
///
732+
/// In other words, this represents an offset from the first line of Markdown
733+
/// in a doc comment or other source. If the first Markdown line appears on line 32,
734+
/// and the `MdRelLine` is 3, then the absolute line for this one is 35. I.e., it's
735+
/// a zero-based offset.
736+
pub(crate) struct MdRelLine {
737+
offset: usize,
738+
}
739+
740+
impl MdRelLine {
741+
/// See struct docs.
742+
pub(crate) const fn new(offset: usize) -> Self {
743+
Self { offset }
744+
}
745+
746+
/// See struct docs.
747+
pub(crate) const fn offset(self) -> usize {
748+
self.offset
749+
}
750+
}
751+
730752
pub(crate) fn find_testable_code<T: doctest::DoctestVisitor>(
731753
doc: &str,
732754
tests: &mut T,
@@ -800,7 +822,7 @@ pub(crate) fn find_codes<T: doctest::DoctestVisitor>(
800822
if nb_lines != 0 && !&doc[prev_offset..offset.start].ends_with('\n') {
801823
nb_lines -= 1;
802824
}
803-
let line = tests.get_line() + nb_lines + 1;
825+
let line = MdRelLine::new(nb_lines);
804826
tests.visit_test(text, block_info, line);
805827
prev_offset = offset.start;
806828
}

src/librustdoc/passes/check_custom_code_classes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::Pass;
77
use crate::clean::{Crate, Item};
88
use crate::core::DocContext;
99
use crate::fold::DocFolder;
10-
use crate::html::markdown::{find_codes, ErrorCodes, LangString};
10+
use crate::html::markdown::{find_codes, ErrorCodes, LangString, MdRelLine};
1111

1212
use rustc_errors::StashKey;
1313
use rustc_feature::GateIssue;
@@ -47,7 +47,7 @@ struct TestsWithCustomClasses {
4747
}
4848

4949
impl crate::doctest::DoctestVisitor for TestsWithCustomClasses {
50-
fn visit_test(&mut self, _: String, config: LangString, _: usize) {
50+
fn visit_test(&mut self, _: String, config: LangString, _: MdRelLine) {
5151
self.custom_classes_found.extend(config.added_classes);
5252
}
5353
}

src/librustdoc/passes/check_doc_test_visibility.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::clean;
1010
use crate::clean::utils::inherits_doc_hidden;
1111
use crate::clean::*;
1212
use crate::core::DocContext;
13-
use crate::html::markdown::{find_testable_code, ErrorCodes, Ignore, LangString};
13+
use crate::html::markdown::{find_testable_code, ErrorCodes, Ignore, LangString, MdRelLine};
1414
use crate::visit::DocVisitor;
1515
use rustc_hir as hir;
1616
use rustc_middle::lint::LintLevelSource;
@@ -45,7 +45,7 @@ pub(crate) struct Tests {
4545
}
4646

4747
impl crate::doctest::DoctestVisitor for Tests {
48-
fn visit_test(&mut self, _: String, config: LangString, _: usize) {
48+
fn visit_test(&mut self, _: String, config: LangString, _: MdRelLine) {
4949
if config.rust && config.ignore == Ignore::None {
5050
self.found_tests += 1;
5151
}

0 commit comments

Comments
 (0)