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).
This commit is contained in:
Noah Lev 2024-05-31 16:56:04 -07:00 committed by Guillaume Gomez
parent 46d2aa5a8f
commit 790b7e9cbf
5 changed files with 42 additions and 24 deletions

View File

@ -34,7 +34,7 @@
use tempfile::{Builder as TempFileBuilder, TempDir};
use crate::config::Options as RustdocOptions;
use crate::html::markdown::{ErrorCodes, Ignore, LangString};
use crate::html::markdown::{ErrorCodes, Ignore, LangString, MdRelLine};
use crate::lint::init_lints;
use self::rust::HirCollector;
@ -961,10 +961,7 @@ struct ScrapedDoctest {
}
pub(crate) trait DoctestVisitor {
fn visit_test(&mut self, test: String, config: LangString, line: usize);
fn get_line(&self) -> usize {
0
}
fn visit_test(&mut self, test: String, config: LangString, rel_line: MdRelLine);
fn visit_header(&mut self, _name: &str, _level: u32) {}
}
@ -1187,8 +1184,8 @@ fn doctest_run_fn(
#[cfg(test)] // used in tests
impl DoctestVisitor for Vec<usize> {
fn visit_test(&mut self, _test: String, _config: LangString, line: usize) {
self.push(line);
fn visit_test(&mut self, _test: String, _config: LangString, rel_line: MdRelLine) {
self.push(1 + rel_line.offset());
}
}

View File

@ -9,7 +9,7 @@
generate_args_file, CreateRunnableDoctests, DoctestVisitor, GlobalTestOptions, ScrapedDoctest,
};
use crate::config::Options;
use crate::html::markdown::{find_testable_code, ErrorCodes, LangString};
use crate::html::markdown::{find_testable_code, ErrorCodes, LangString, MdRelLine};
struct MdCollector {
tests: Vec<ScrapedDoctest>,
@ -18,8 +18,10 @@ struct MdCollector {
}
impl DoctestVisitor for MdCollector {
fn visit_test(&mut self, test: String, config: LangString, line: usize) {
fn visit_test(&mut self, test: String, config: LangString, rel_line: MdRelLine) {
let filename = self.filename.clone();
// First line of Markdown is line 1.
let line = 1 + rel_line.offset();
self.tests.push(ScrapedDoctest {
filename,
line,
@ -29,10 +31,6 @@ fn visit_test(&mut self, test: String, config: LangString, line: usize) {
});
}
fn get_line(&self) -> usize {
0
}
fn visit_header(&mut self, name: &str, level: u32) {
// We use these headings as test names, so it's good if
// they're valid identifiers.

View File

@ -15,7 +15,7 @@
use super::{DoctestVisitor, ScrapedDoctest};
use crate::clean::{types::AttributesExt, Attributes};
use crate::html::markdown::{self, ErrorCodes, LangString};
use crate::html::markdown::{self, ErrorCodes, LangString, MdRelLine};
struct RustCollector {
source_map: Lrc<SourceMap>,
@ -36,10 +36,17 @@ fn get_filename(&self) -> FileName {
}
filename
}
fn get_base_line(&self) -> usize {
let sp_lo = self.position.lo().to_usize();
let loc = self.source_map.lookup_char_pos(BytePos(sp_lo as u32));
loc.line
}
}
impl DoctestVisitor for RustCollector {
fn visit_test(&mut self, test: String, config: LangString, line: usize) {
fn visit_test(&mut self, test: String, config: LangString, rel_line: MdRelLine) {
let line = self.get_base_line() + rel_line.offset();
self.tests.push(ScrapedDoctest {
filename: self.get_filename(),
line,
@ -49,12 +56,6 @@ fn visit_test(&mut self, test: String, config: LangString, line: usize) {
});
}
fn get_line(&self) -> usize {
let line = self.position.lo().to_usize();
let line = self.source_map.lookup_char_pos(BytePos(line as u32)).line;
if line > 0 { line - 1 } else { line }
}
fn visit_header(&mut self, _name: &str, _level: u32) {}
}

View File

@ -710,6 +710,28 @@ fn next(&mut self) -> Option<Self::Item> {
}
}
/// A newtype that represents a relative line number in Markdown.
///
/// In other words, this represents an offset from the first line of Markdown
/// in a doc comment or other source. If the first Markdown line appears on line 32,
/// and the `MdRelLine` is 3, then the absolute line for this one is 35. I.e., it's
/// a zero-based offset.
pub(crate) struct MdRelLine {
offset: usize,
}
impl MdRelLine {
/// See struct docs.
pub(crate) const fn new(offset: usize) -> Self {
Self { offset }
}
/// See struct docs.
pub(crate) const fn offset(self) -> usize {
self.offset
}
}
pub(crate) fn find_testable_code<T: doctest::DoctestVisitor>(
doc: &str,
tests: &mut T,
@ -772,7 +794,7 @@ pub(crate) fn find_codes<T: doctest::DoctestVisitor>(
if nb_lines != 0 && !&doc[prev_offset..offset.start].ends_with('\n') {
nb_lines -= 1;
}
let line = tests.get_line() + nb_lines + 1;
let line = MdRelLine::new(nb_lines);
tests.visit_test(text, block_info, line);
prev_offset = offset.start;
}

View File

@ -10,7 +10,7 @@
use crate::clean::utils::inherits_doc_hidden;
use crate::clean::*;
use crate::core::DocContext;
use crate::html::markdown::{find_testable_code, ErrorCodes, Ignore, LangString};
use crate::html::markdown::{find_testable_code, ErrorCodes, Ignore, LangString, MdRelLine};
use crate::visit::DocVisitor;
use rustc_hir as hir;
use rustc_middle::lint::LintLevelSource;
@ -45,7 +45,7 @@ pub(crate) struct Tests {
}
impl crate::doctest::DoctestVisitor for Tests {
fn visit_test(&mut self, _: String, config: LangString, _: usize) {
fn visit_test(&mut self, _: String, config: LangString, _: MdRelLine) {
if config.rust && config.ignore == Ignore::None {
self.found_tests += 1;
}