Improve naming of variables in DocTestBuilder::generate_unique_doctest

Improve code
This commit is contained in:
Guillaume Gomez 2025-05-23 14:32:25 +02:00
parent 21aabafbd1
commit eb61be325f
3 changed files with 40 additions and 9 deletions

View file

@ -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,

View file

@ -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,
}

View file

@ -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,
)
}