From ab3d90e037a3f37e6130b1aec0aca89f2e0de90b Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 9 Jul 2024 12:07:18 +0200 Subject: [PATCH] Unify naming of `DocTest` --- src/librustdoc/doctest.rs | 47 ++++++++++--------- src/librustdoc/doctest/markdown.rs | 12 ++--- src/librustdoc/doctest/runner.rs | 10 ++-- src/librustdoc/doctest/rust.rs | 13 +++-- src/librustdoc/html/markdown.rs | 4 +- .../passes/check_doc_test_visibility.rs | 2 +- 6 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 24e5e876112..91b93a99903 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -163,7 +163,7 @@ pub(crate) fn run(dcx: DiagCtxtHandle<'_>, options: RustdocOptions) -> Result<() let args_path = temp_dir.path().join("rustdoc-cfgs"); crate::wrap_return(dcx, generate_args_file(&args_path, &options))?; - let CreateRunnableDoctests { + let CreateRunnableDocTests { standalone_tests, mergeable_tests, rustdoc_options, @@ -179,7 +179,7 @@ pub(crate) fn run(dcx: DiagCtxtHandle<'_>, options: RustdocOptions) -> Result<() let opts = scrape_test_config(crate_name, crate_attrs, args_path); let enable_per_target_ignores = options.enable_per_target_ignores; - let mut collector = CreateRunnableDoctests::new(options, opts); + let mut collector = CreateRunnableDocTests::new(options, opts); let hir_collector = HirCollector::new( &compiler.sess, tcx.hir(), @@ -250,7 +250,7 @@ pub(crate) fn run_tests( rustdoc_options: &Arc, unused_extern_reports: &Arc>>, mut standalone_tests: Vec, - mergeable_tests: FxHashMap>, + mergeable_tests: FxHashMap>, ) { let mut test_args = Vec::with_capacity(rustdoc_options.test_args.len() + 1); test_args.insert(0, "rustdoctest".to_string()); @@ -432,8 +432,13 @@ fn wrapped_rustc_command(rustc_wrappers: &[PathBuf], rustc_binary: &Path) -> Com command } -/// This struct contains information needed for running a doctest. -struct RunnableDoctest { +/// Information needed for running a bundle of doctests. +/// +/// This data structure contains the "full" test code, including the wrappers +/// (if multiple doctests are merged), `main` function, +/// and everything needed to calculate the compiler's command-line arguments. +/// The `# ` prefix on boring lines has also been stripped. +struct RunnableDocTest { full_test_code: String, full_test_line_offset: usize, test_opts: IndividualTestOptions, @@ -444,14 +449,14 @@ struct RunnableDoctest { no_run: bool, } -impl RunnableDoctest { +impl RunnableDocTest { fn path_for_merged_doctest(&self) -> PathBuf { self.test_opts.outdir.path().join(&format!("doctest_{}.rs", self.edition)) } } fn run_test( - doctest: RunnableDoctest, + doctest: RunnableDocTest, rustdoc_options: &RustdocOptions, supports_color: bool, is_multiple_tests: bool, @@ -700,7 +705,7 @@ fn new(options: &RustdocOptions, test_id: &Option, test_path: PathBuf) - } /// A doctest scraped from the code, ready to be turned into a runnable test. -pub(crate) struct ScrapedDoctest { +pub(crate) struct ScrapedDocTest { filename: FileName, line: usize, langstr: LangString, @@ -708,7 +713,7 @@ pub(crate) struct ScrapedDoctest { name: String, } -impl ScrapedDoctest { +impl ScrapedDocTest { fn new( filename: FileName, line: usize, @@ -748,14 +753,14 @@ fn path(&self) -> PathBuf { } } -pub(crate) trait DoctestVisitor { +pub(crate) trait DocTestVisitor { fn visit_test(&mut self, test: String, config: LangString, rel_line: MdRelLine); fn visit_header(&mut self, _name: &str, _level: u32) {} } -struct CreateRunnableDoctests { +struct CreateRunnableDocTests { standalone_tests: Vec, - mergeable_tests: FxHashMap>, + mergeable_tests: FxHashMap>, rustdoc_options: Arc, opts: GlobalTestOptions, @@ -765,10 +770,10 @@ struct CreateRunnableDoctests { can_merge_doctests: bool, } -impl CreateRunnableDoctests { - fn new(rustdoc_options: RustdocOptions, opts: GlobalTestOptions) -> CreateRunnableDoctests { +impl CreateRunnableDocTests { + fn new(rustdoc_options: RustdocOptions, opts: GlobalTestOptions) -> CreateRunnableDocTests { let can_merge_doctests = rustdoc_options.edition >= Edition::Edition2024; - CreateRunnableDoctests { + CreateRunnableDocTests { standalone_tests: Vec::new(), mergeable_tests: FxHashMap::default(), rustdoc_options: Arc::new(rustdoc_options), @@ -780,7 +785,7 @@ fn new(rustdoc_options: RustdocOptions, opts: GlobalTestOptions) -> CreateRunnab } } - fn add_test(&mut self, scraped_test: ScrapedDoctest) { + fn add_test(&mut self, scraped_test: ScrapedDocTest) { // For example `module/file.rs` would become `module_file_rs` let file = scraped_test .filename @@ -829,7 +834,7 @@ fn add_test(&mut self, scraped_test: ScrapedDoctest) { fn generate_test_desc_and_fn( &mut self, test: DocTestBuilder, - scraped_test: ScrapedDoctest, + scraped_test: ScrapedDocTest, ) -> test::TestDescAndFn { if !scraped_test.langstr.compile_fail { self.compiling_test_count.fetch_add(1, Ordering::SeqCst); @@ -847,7 +852,7 @@ fn generate_test_desc_and_fn( fn generate_test_desc_and_fn( test: DocTestBuilder, - scraped_test: ScrapedDoctest, + scraped_test: ScrapedDocTest, opts: GlobalTestOptions, rustdoc_options: Arc, unused_externs: Arc>>, @@ -894,7 +899,7 @@ fn doctest_run_fn( test_opts: IndividualTestOptions, global_opts: GlobalTestOptions, doctest: DocTestBuilder, - scraped_test: ScrapedDoctest, + scraped_test: ScrapedDocTest, rustdoc_options: Arc, unused_externs: Arc>>, ) -> Result<(), String> { @@ -907,7 +912,7 @@ fn doctest_run_fn( &global_opts, Some(&global_opts.crate_name), ); - let runnable_test = RunnableDoctest { + let runnable_test = RunnableDocTest { full_test_code, full_test_line_offset, test_opts, @@ -980,7 +985,7 @@ fn doctest_run_fn( } #[cfg(test)] // used in tests -impl DoctestVisitor for Vec { +impl DocTestVisitor for Vec { fn visit_test(&mut self, _test: String, _config: LangString, rel_line: MdRelLine) { self.push(1 + rel_line.offset()); } diff --git a/src/librustdoc/doctest/markdown.rs b/src/librustdoc/doctest/markdown.rs index 5c9fd1e949c..4806d865589 100644 --- a/src/librustdoc/doctest/markdown.rs +++ b/src/librustdoc/doctest/markdown.rs @@ -7,23 +7,23 @@ use tempfile::tempdir; use super::{ - generate_args_file, CreateRunnableDoctests, DoctestVisitor, GlobalTestOptions, ScrapedDoctest, + generate_args_file, CreateRunnableDocTests, DocTestVisitor, GlobalTestOptions, ScrapedDocTest, }; use crate::config::Options; use crate::html::markdown::{find_testable_code, ErrorCodes, LangString, MdRelLine}; struct MdCollector { - tests: Vec, + tests: Vec, cur_path: Vec, filename: FileName, } -impl DoctestVisitor for MdCollector { +impl DocTestVisitor for MdCollector { 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::new(filename, line, self.cur_path.clone(), config, test)); + self.tests.push(ScrapedDocTest::new(filename, line, self.cur_path.clone(), config, test)); } fn visit_header(&mut self, name: &str, level: u32) { @@ -113,9 +113,9 @@ pub(crate) fn test(options: Options) -> Result<(), String> { None, ); - let mut collector = CreateRunnableDoctests::new(options.clone(), opts); + let mut collector = CreateRunnableDocTests::new(options.clone(), opts); md_collector.tests.into_iter().for_each(|t| collector.add_test(t)); - let CreateRunnableDoctests { opts, rustdoc_options, standalone_tests, mergeable_tests, .. } = + let CreateRunnableDocTests { opts, rustdoc_options, standalone_tests, mergeable_tests, .. } = collector; crate::doctest::run_tests( opts, diff --git a/src/librustdoc/doctest/runner.rs b/src/librustdoc/doctest/runner.rs index 46122bbeafd..0d73e02cbd7 100644 --- a/src/librustdoc/doctest/runner.rs +++ b/src/librustdoc/doctest/runner.rs @@ -4,8 +4,8 @@ use std::fmt::Write; use crate::doctest::{ - run_test, DocTestBuilder, GlobalTestOptions, IndividualTestOptions, RunnableDoctest, - RustdocOptions, ScrapedDoctest, TestFailure, UnusedExterns, + run_test, DocTestBuilder, GlobalTestOptions, IndividualTestOptions, RunnableDocTest, + RustdocOptions, ScrapedDocTest, TestFailure, UnusedExterns, }; use crate::html::markdown::{Ignore, LangString}; @@ -32,7 +32,7 @@ pub(crate) fn new() -> Self { pub(crate) fn add_test( &mut self, doctest: &DocTestBuilder, - scraped_test: &ScrapedDoctest, + scraped_test: &ScrapedDocTest, target_str: &str, ) { let ignore = match scraped_test.langstr.ignore { @@ -175,7 +175,7 @@ fn main() -> std::process::ExitCode {{ ids = self.ids, ) .expect("failed to generate test code"); - let runnable_test = RunnableDoctest { + let runnable_test = RunnableDocTest { full_test_code: code, full_test_line_offset: 0, test_opts: test_options, @@ -199,7 +199,7 @@ fn main() -> std::process::ExitCode {{ /// Push new doctest content into `output`. Returns the test ID for this doctest. fn generate_mergeable_doctest( doctest: &DocTestBuilder, - scraped_test: &ScrapedDoctest, + scraped_test: &ScrapedDocTest, ignore: bool, id: usize, output: &mut String, diff --git a/src/librustdoc/doctest/rust.rs b/src/librustdoc/doctest/rust.rs index 17c29ba413a..243fb5d05fd 100644 --- a/src/librustdoc/doctest/rust.rs +++ b/src/librustdoc/doctest/rust.rs @@ -14,14 +14,13 @@ use rustc_span::source_map::SourceMap; use rustc_span::{BytePos, FileName, Pos, Span, DUMMY_SP}; -use super::{DoctestVisitor, ScrapedDoctest}; -use crate::clean::types::AttributesExt; -use crate::clean::Attributes; +use super::{DocTestVisitor, ScrapedDocTest}; +use crate::clean::{types::AttributesExt, Attributes}; use crate::html::markdown::{self, ErrorCodes, LangString, MdRelLine}; struct RustCollector { source_map: Lrc, - tests: Vec, + tests: Vec, cur_path: Vec, position: Span, } @@ -48,10 +47,10 @@ fn get_base_line(&self) -> usize { } } -impl DoctestVisitor for RustCollector { +impl DocTestVisitor for RustCollector { 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::new( + self.tests.push(ScrapedDocTest::new( self.get_filename(), line, self.cur_path.clone(), @@ -89,7 +88,7 @@ pub fn new( Self { sess, map, codes, enable_per_target_ignores, tcx, collector } } - pub fn collect_crate(mut self) -> Vec { + pub fn collect_crate(mut self) -> Vec { let tcx = self.tcx; self.visit_testable("".to_string(), CRATE_DEF_ID, tcx.hir().span(CRATE_HIR_ID), |this| { tcx.hir().walk_toplevel_module(this) diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index bc30f7fa43b..7bfe5d87d39 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -738,7 +738,7 @@ pub(crate) const fn offset(self) -> usize { } } -pub(crate) fn find_testable_code( +pub(crate) fn find_testable_code( doc: &str, tests: &mut T, error_codes: ErrorCodes, @@ -748,7 +748,7 @@ pub(crate) fn find_testable_code( find_codes(doc, tests, error_codes, enable_per_target_ignores, extra_info, false) } -pub(crate) fn find_codes( +pub(crate) fn find_codes( doc: &str, tests: &mut T, error_codes: ErrorCodes, diff --git a/src/librustdoc/passes/check_doc_test_visibility.rs b/src/librustdoc/passes/check_doc_test_visibility.rs index d78afdffc62..5015d665955 100644 --- a/src/librustdoc/passes/check_doc_test_visibility.rs +++ b/src/librustdoc/passes/check_doc_test_visibility.rs @@ -45,7 +45,7 @@ pub(crate) struct Tests { pub(crate) found_tests: usize, } -impl crate::doctest::DoctestVisitor for Tests { +impl crate::doctest::DocTestVisitor for Tests { fn visit_test(&mut self, _: String, config: LangString, _: MdRelLine) { if config.rust && config.ignore == Ignore::None { self.found_tests += 1;