Skip to content

Commit eb61be3

Browse files
Improve naming of variables in DocTestBuilder::generate_unique_doctest
Improve code
1 parent 21aabaf commit eb61be3

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

src/librustdoc/doctest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,14 +1053,14 @@ fn doctest_run_fn(
10531053
let report_unused_externs = |uext| {
10541054
unused_externs.lock().unwrap().push(uext);
10551055
};
1056-
let (wrapper, full_test_line_offset) = doctest.generate_unique_doctest(
1056+
let (wrapped, full_test_line_offset) = doctest.generate_unique_doctest(
10571057
&scraped_test.text,
10581058
scraped_test.langstr.test_harness,
10591059
&global_opts,
10601060
Some(&global_opts.crate_name),
10611061
);
10621062
let runnable_test = RunnableDocTest {
1063-
full_test_code: wrapper.to_string(),
1063+
full_test_code: wrapped.to_string(),
10641064
full_test_line_offset,
10651065
test_opts,
10661066
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,7 +36,16 @@ 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, global_crate_attrs, .. } =
4050
scraped_test;
4151

@@ -45,7 +55,7 @@ impl ExtractedDocTests {
4555
.edition(edition)
4656
.lang_str(&langstr)
4757
.build(None);
48-
let (wrapper, _size) = doctest.generate_unique_doctest(
58+
let (wrapped, _size) = doctest.generate_unique_doctest(
4959
&text,
5060
langstr.test_harness,
5161
opts,
@@ -55,7 +65,7 @@ impl ExtractedDocTests {
5565
file: filename.prefer_remapped_unconditionaly().to_string(),
5666
line,
5767
doctest_attributes: langstr.into(),
58-
doctest_code: match wrapper {
68+
doctest_code: match wrapped {
5969
DocTestWrapResult::Valid { crate_level_code, wrapper, code } => Some(DocTest {
6070
crate_level: crate_level_code,
6171
code,
@@ -71,6 +81,11 @@ impl ExtractedDocTests {
7181
name,
7282
});
7383
}
84+
85+
#[cfg(test)]
86+
pub(crate) fn doctests(&self) -> &[ExtractedDocTest] {
87+
&self.doctests
88+
}
7489
}
7590

7691
#[derive(Serialize)]
@@ -84,7 +99,12 @@ pub(crate) struct WrapperInfo {
8499
pub(crate) struct DocTest {
85100
crate_level: String,
86101
code: String,
87-
wrapper: Option<WrapperInfo>,
102+
/// This field can be `None` if one of the following conditions is true:
103+
///
104+
/// * The doctest's codeblock has the `test_harness` attribute.
105+
/// * The doctest has a `main` function.
106+
/// * The doctest has the `![no_std]` attribute.
107+
pub(crate) wrapper: Option<WrapperInfo>,
88108
}
89109

90110
#[derive(Serialize)]
@@ -94,7 +114,7 @@ pub(crate) struct ExtractedDocTest {
94114
doctest_attributes: LangString,
95115
original_code: String,
96116
/// `None` if the code syntax is invalid.
97-
doctest_code: Option<DocTest>,
117+
pub(crate) doctest_code: Option<DocTest>,
98118
name: String,
99119
}
100120

src/librustdoc/doctest/make.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,14 @@ impl WrapperInfo {
214214
pub(crate) enum DocTestWrapResult {
215215
Valid {
216216
crate_level_code: String,
217+
/// This field can be `None` if one of the following conditions is true:
218+
///
219+
/// * The doctest's codeblock has the `test_harness` attribute.
220+
/// * The doctest has a `main` function.
221+
/// * The doctest has the `![no_std]` attribute.
217222
wrapper: Option<WrapperInfo>,
223+
/// Contains the doctest processed code without the wrappers (which are stored in the
224+
/// `wrapper` field).
218225
code: String,
219226
},
220227
/// Contains the original source code.
@@ -304,7 +311,7 @@ impl DocTestBuilder {
304311
}
305312
let mut line_offset = 0;
306313
let mut crate_level_code = String::new();
307-
let code = self.everything_else.trim();
314+
let processed_code = self.everything_else.trim();
308315
if self.global_crate_attrs.is_empty() {
309316
// If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
310317
// lints that are commonly triggered in doctests. The crate-level test attributes are
@@ -368,7 +375,7 @@ impl DocTestBuilder {
368375
{
369376
None
370377
} else {
371-
let returns_result = code.ends_with("(())");
378+
let returns_result = processed_code.ends_with("(())");
372379
// Give each doctest main function a unique name.
373380
// This is for example needed for the tooling around `-C instrument-coverage`.
374381
let inner_fn_name = if let Some(ref test_id) = self.test_id {
@@ -411,7 +418,11 @@ impl DocTestBuilder {
411418
};
412419

413420
(
414-
DocTestWrapResult::Valid { code: code.to_string(), wrapper, crate_level_code },
421+
DocTestWrapResult::Valid {
422+
code: processed_code.to_string(),
423+
wrapper,
424+
crate_level_code,
425+
},
415426
line_offset,
416427
)
417428
}

0 commit comments

Comments
 (0)