about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2025-05-23 14:32:25 +0200
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2025-06-10 13:58:39 +0200
commiteb61be325fce758c3b77391c68a262f70ddca741 (patch)
tree508a7e32c341f0f4ee11948537ac5bb8182173f2
parent21aabafbd16f2547dbbf865a437ccd2cc3cd0093 (diff)
downloadrust-eb61be325fce758c3b77391c68a262f70ddca741.tar.gz
rust-eb61be325fce758c3b77391c68a262f70ddca741.zip
Improve naming of variables in `DocTestBuilder::generate_unique_doctest`
Improve code
-rw-r--r--src/librustdoc/doctest.rs4
-rw-r--r--src/librustdoc/doctest/extracted.rs28
-rw-r--r--src/librustdoc/doctest/make.rs17
3 files changed, 40 insertions, 9 deletions
diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs
index b024f3037b0..130fdff1afe 100644
--- a/src/librustdoc/doctest.rs
+++ b/src/librustdoc/doctest.rs
@@ -1053,14 +1053,14 @@ fn doctest_run_fn(
     let report_unused_externs = |uext| {
         unused_externs.lock().unwrap().push(uext);
     };
-    let (wrapper, full_test_line_offset) = doctest.generate_unique_doctest(
+    let (wrapped, full_test_line_offset) = doctest.generate_unique_doctest(
         &scraped_test.text,
         scraped_test.langstr.test_harness,
         &global_opts,
         Some(&global_opts.crate_name),
     );
     let runnable_test = RunnableDocTest {
-        full_test_code: wrapper.to_string(),
+        full_test_code: wrapped.to_string(),
         full_test_line_offset,
         test_opts,
         global_opts,
diff --git a/src/librustdoc/doctest/extracted.rs b/src/librustdoc/doctest/extracted.rs
index b856af32f1a..925fb6fee2c 100644
--- a/src/librustdoc/doctest/extracted.rs
+++ b/src/librustdoc/doctest/extracted.rs
@@ -3,6 +3,7 @@
 //! This module contains the logic to extract doctests and output a JSON containing this
 //! information.
 
+use rustc_span::edition::Edition;
 use serde::Serialize;
 
 use super::make::DocTestWrapResult;
@@ -35,7 +36,16 @@ impl ExtractedDocTests {
         options: &RustdocOptions,
     ) {
         let edition = scraped_test.edition(options);
+        self.add_test_with_edition(scraped_test, opts, edition)
+    }
 
+    /// This method is used by unit tests to not have to provide a `RustdocOptions`.
+    pub(crate) fn add_test_with_edition(
+        &mut self,
+        scraped_test: ScrapedDocTest,
+        opts: &super::GlobalTestOptions,
+        edition: Edition,
+    ) {
         let ScrapedDocTest { filename, line, langstr, text, name, global_crate_attrs, .. } =
             scraped_test;
 
@@ -45,7 +55,7 @@ impl ExtractedDocTests {
             .edition(edition)
             .lang_str(&langstr)
             .build(None);
-        let (wrapper, _size) = doctest.generate_unique_doctest(
+        let (wrapped, _size) = doctest.generate_unique_doctest(
             &text,
             langstr.test_harness,
             opts,
@@ -55,7 +65,7 @@ impl ExtractedDocTests {
             file: filename.prefer_remapped_unconditionaly().to_string(),
             line,
             doctest_attributes: langstr.into(),
-            doctest_code: match wrapper {
+            doctest_code: match wrapped {
                 DocTestWrapResult::Valid { crate_level_code, wrapper, code } => Some(DocTest {
                     crate_level: crate_level_code,
                     code,
@@ -71,6 +81,11 @@ impl ExtractedDocTests {
             name,
         });
     }
+
+    #[cfg(test)]
+    pub(crate) fn doctests(&self) -> &[ExtractedDocTest] {
+        &self.doctests
+    }
 }
 
 #[derive(Serialize)]
@@ -84,7 +99,12 @@ pub(crate) struct WrapperInfo {
 pub(crate) struct DocTest {
     crate_level: String,
     code: String,
-    wrapper: Option<WrapperInfo>,
+    /// This field can be `None` if one of the following conditions is true:
+    ///
+    /// * The doctest's codeblock has the `test_harness` attribute.
+    /// * The doctest has a `main` function.
+    /// * The doctest has the `![no_std]` attribute.
+    pub(crate) wrapper: Option<WrapperInfo>,
 }
 
 #[derive(Serialize)]
@@ -94,7 +114,7 @@ pub(crate) struct ExtractedDocTest {
     doctest_attributes: LangString,
     original_code: String,
     /// `None` if the code syntax is invalid.
-    doctest_code: Option<DocTest>,
+    pub(crate) doctest_code: Option<DocTest>,
     name: String,
 }
 
diff --git a/src/librustdoc/doctest/make.rs b/src/librustdoc/doctest/make.rs
index 0117cd03c77..3ff6828e52f 100644
--- a/src/librustdoc/doctest/make.rs
+++ b/src/librustdoc/doctest/make.rs
@@ -214,7 +214,14 @@ impl WrapperInfo {
 pub(crate) enum DocTestWrapResult {
     Valid {
         crate_level_code: String,
+        /// This field can be `None` if one of the following conditions is true:
+        ///
+        /// * The doctest's codeblock has the `test_harness` attribute.
+        /// * The doctest has a `main` function.
+        /// * The doctest has the `![no_std]` attribute.
         wrapper: Option<WrapperInfo>,
+        /// Contains the doctest processed code without the wrappers (which are stored in the
+        /// `wrapper` field).
         code: String,
     },
     /// Contains the original source code.
@@ -304,7 +311,7 @@ impl DocTestBuilder {
         }
         let mut line_offset = 0;
         let mut crate_level_code = String::new();
-        let code = self.everything_else.trim();
+        let processed_code = self.everything_else.trim();
         if self.global_crate_attrs.is_empty() {
             // If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
             // lints that are commonly triggered in doctests. The crate-level test attributes are
@@ -368,7 +375,7 @@ impl DocTestBuilder {
         {
             None
         } else {
-            let returns_result = code.ends_with("(())");
+            let returns_result = processed_code.ends_with("(())");
             // Give each doctest main function a unique name.
             // This is for example needed for the tooling around `-C instrument-coverage`.
             let inner_fn_name = if let Some(ref test_id) = self.test_id {
@@ -411,7 +418,11 @@ impl DocTestBuilder {
         };
 
         (
-            DocTestWrapResult::Valid { code: code.to_string(), wrapper, crate_level_code },
+            DocTestWrapResult::Valid {
+                code: processed_code.to_string(),
+                wrapper,
+                crate_level_code,
+            },
             line_offset,
         )
     }