Skip to content

Commit 8a07c73

Browse files
Improve naming of variables in DocTestBuilder::generate_unique_doctest
Improve code
1 parent b282299 commit 8a07c73

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

src/librustdoc/doctest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,14 +1044,14 @@ fn doctest_run_fn(
10441044
let report_unused_externs = |uext| {
10451045
unused_externs.lock().unwrap().push(uext);
10461046
};
1047-
let (wrapper, full_test_line_offset) = doctest.generate_unique_doctest(
1047+
let (wrapped, full_test_line_offset) = doctest.generate_unique_doctest(
10481048
&scraped_test.text,
10491049
scraped_test.langstr.test_harness,
10501050
&global_opts,
10511051
Some(&global_opts.crate_name),
10521052
);
10531053
let runnable_test = RunnableDocTest {
1054-
full_test_code: wrapper.to_string(),
1054+
full_test_code: wrapped.to_string(),
10551055
full_test_line_offset,
10561056
test_opts,
10571057
global_opts,

src/librustdoc/doctest/extracted.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! This module contains the logic to extract doctests and output a JSON containing this
44
//! information.
55
6+
use rustc_span::edition::Edition;
67
use serde::Serialize;
78

89
use super::make::DocTestWrapResult;
@@ -35,15 +36,24 @@ impl ExtractedDocTests {
3536
options: &RustdocOptions,
3637
) {
3738
let edition = scraped_test.edition(options);
39+
self.add_test_with_edition(scraped_test, opts, edition)
40+
}
3841

42+
/// This method is used by unit tests to not have to provide a `RustdocOptions`.
43+
pub(crate) fn add_test_with_edition(
44+
&mut self,
45+
scraped_test: ScrapedDocTest,
46+
opts: &super::GlobalTestOptions,
47+
edition: Edition,
48+
) {
3949
let ScrapedDocTest { filename, line, langstr, text, name, .. } = scraped_test;
4050

4151
let doctest = BuildDocTestBuilder::new(&text)
4252
.crate_name(&opts.crate_name)
4353
.edition(edition)
4454
.lang_str(&langstr)
4555
.build(None);
46-
let (wrapper, _size) = doctest.generate_unique_doctest(
56+
let (wrapped, _size) = doctest.generate_unique_doctest(
4757
&text,
4858
langstr.test_harness,
4959
opts,
@@ -53,7 +63,7 @@ impl ExtractedDocTests {
5363
file: filename.prefer_remapped_unconditionaly().to_string(),
5464
line,
5565
doctest_attributes: langstr.into(),
56-
doctest_code: match wrapper {
66+
doctest_code: match wrapped {
5767
DocTestWrapResult::Valid { crate_level_code, wrapper, code } => Some(DocTest {
5868
crate_level: crate_level_code,
5969
code,
@@ -69,6 +79,11 @@ impl ExtractedDocTests {
6979
name,
7080
});
7181
}
82+
83+
#[cfg(test)]
84+
pub(crate) fn doctests(&self) -> &[ExtractedDocTest] {
85+
&self.doctests
86+
}
7287
}
7388

7489
#[derive(Serialize)]
@@ -82,7 +97,12 @@ pub(crate) struct WrapperInfo {
8297
pub(crate) struct DocTest {
8398
crate_level: String,
8499
code: String,
85-
wrapper: Option<WrapperInfo>,
100+
/// This field can be `None` if one of the following conditions is true:
101+
///
102+
/// * The doctest's codeblock has the `test_harness` attribute.
103+
/// * The doctest has a `main` function.
104+
/// * The doctest has the `![no_std]` attribute.
105+
pub(crate) wrapper: Option<WrapperInfo>,
86106
}
87107

88108
#[derive(Serialize)]
@@ -92,7 +112,7 @@ pub(crate) struct ExtractedDocTest {
92112
doctest_attributes: LangString,
93113
original_code: String,
94114
/// `None` if the code syntax is invalid.
95-
doctest_code: Option<DocTest>,
115+
pub(crate) doctest_code: Option<DocTest>,
96116
name: String,
97117
}
98118

src/librustdoc/doctest/make.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,14 @@ impl WrapperInfo {
202202
pub(crate) enum DocTestWrapResult {
203203
Valid {
204204
crate_level_code: String,
205+
/// This field can be `None` if one of the following conditions is true:
206+
///
207+
/// * The doctest's codeblock has the `test_harness` attribute.
208+
/// * The doctest has a `main` function.
209+
/// * The doctest has the `![no_std]` attribute.
205210
wrapper: Option<WrapperInfo>,
211+
/// Contains the doctest processed code without the wrappers (which are stored in the
212+
/// `wrapper` field).
206213
code: String,
207214
},
208215
/// Contains the original source code.
@@ -290,7 +297,7 @@ impl DocTestBuilder {
290297
}
291298
let mut line_offset = 0;
292299
let mut crate_level_code = String::new();
293-
let code = self.everything_else.trim().to_string();
300+
let processed_code = self.everything_else.trim().to_string();
294301
if opts.attrs.is_empty() {
295302
// If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
296303
// lints that are commonly triggered in doctests. The crate-level test attributes are
@@ -354,7 +361,7 @@ impl DocTestBuilder {
354361
{
355362
None
356363
} else {
357-
let returns_result = code.ends_with("(())");
364+
let returns_result = processed_code.ends_with("(())");
358365
// Give each doctest main function a unique name.
359366
// This is for example needed for the tooling around `-C instrument-coverage`.
360367
let inner_fn_name = if let Some(ref test_id) = self.test_id {
@@ -396,7 +403,7 @@ impl DocTestBuilder {
396403
})
397404
};
398405

399-
(DocTestWrapResult::Valid { code, wrapper, crate_level_code }, line_offset)
406+
(DocTestWrapResult::Valid { code: processed_code, wrapper, crate_level_code }, line_offset)
400407
}
401408
}
402409

0 commit comments

Comments
 (0)