Skip to content

Commit f4d41a5

Browse files
Create a builder for DocTestBuilder type
1 parent 5b86fa8 commit f4d41a5

File tree

5 files changed

+123
-72
lines changed

5 files changed

+123
-72
lines changed

src/librustdoc/doctest.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
1212
use std::sync::{Arc, Mutex};
1313
use std::{panic, str};
1414

15-
pub(crate) use make::DocTestBuilder;
15+
pub(crate) use make::{BuildDocTestBuilder, DocTestBuilder};
1616
pub(crate) use markdown::test as test_markdown;
1717
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
1818
use rustc_errors::emitter::HumanReadableErrorType;
@@ -972,16 +972,14 @@ impl CreateRunnableDocTests {
972972
);
973973

974974
let edition = scraped_test.edition(&self.rustdoc_options);
975-
let doctest = DocTestBuilder::new(
976-
&scraped_test.text,
977-
Some(&self.opts.crate_name),
978-
edition,
979-
self.can_merge_doctests,
980-
Some(test_id),
981-
Some(&scraped_test.langstr),
982-
dcx,
983-
scraped_test.span,
984-
);
975+
let doctest = BuildDocTestBuilder::new(&scraped_test.text)
976+
.crate_name(&self.opts.crate_name)
977+
.edition(edition)
978+
.can_merge_doctests(self.can_merge_doctests)
979+
.test_id(test_id)
980+
.lang_str(&scraped_test.langstr)
981+
.span(scraped_test.span)
982+
.build(dcx);
985983
let is_standalone = !doctest.can_be_merged
986984
|| scraped_test.langstr.compile_fail
987985
|| scraped_test.langstr.test_harness

src/librustdoc/doctest/extracted.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
//! This module contains the logic to extract doctests and output a JSON containing this
44
//! information.
55
6-
use rustc_span::DUMMY_SP;
76
use serde::Serialize;
87

9-
use super::{DocTestBuilder, ScrapedDocTest};
8+
use super::{BuildDocTestBuilder, ScrapedDocTest};
109
use crate::config::Options as RustdocOptions;
1110
use crate::html::markdown;
1211

@@ -38,16 +37,11 @@ impl ExtractedDocTests {
3837

3938
let ScrapedDocTest { filename, line, langstr, text, name, .. } = scraped_test;
4039

41-
let doctest = DocTestBuilder::new(
42-
&text,
43-
Some(&opts.crate_name),
44-
edition,
45-
false,
46-
None,
47-
Some(&langstr),
48-
None,
49-
DUMMY_SP,
50-
);
40+
let doctest = BuildDocTestBuilder::new(&text)
41+
.crate_name(&opts.crate_name)
42+
.edition(edition)
43+
.lang_str(&langstr)
44+
.build(None);
5145
let (full_test_code, size) = doctest.generate_unique_doctest(
5246
&text,
5347
langstr.test_harness,

src/librustdoc/doctest/make.rs

Lines changed: 93 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ use rustc_errors::emitter::stderr_destination;
1212
use rustc_errors::{ColorConfig, DiagCtxtHandle};
1313
use rustc_parse::new_parser_from_source_str;
1414
use rustc_session::parse::ParseSess;
15-
use rustc_span::edition::Edition;
15+
use rustc_span::edition::{DEFAULT_EDITION, Edition};
1616
use rustc_span::source_map::SourceMap;
1717
use rustc_span::symbol::sym;
18-
use rustc_span::{FileName, Span, kw};
18+
use rustc_span::{DUMMY_SP, FileName, Span, kw};
1919
use tracing::debug;
2020

2121
use super::GlobalTestOptions;
@@ -35,35 +35,78 @@ struct ParseSourceInfo {
3535
maybe_crate_attrs: String,
3636
}
3737

38-
/// This struct contains information about the doctest itself which is then used to generate
39-
/// doctest source code appropriately.
40-
pub(crate) struct DocTestBuilder {
41-
pub(crate) supports_color: bool,
42-
pub(crate) already_has_extern_crate: bool,
43-
pub(crate) has_main_fn: bool,
44-
pub(crate) crate_attrs: String,
45-
/// If this is a merged doctest, it will be put into `everything_else`, otherwise it will
46-
/// put into `crate_attrs`.
47-
pub(crate) maybe_crate_attrs: String,
48-
pub(crate) crates: String,
49-
pub(crate) everything_else: String,
50-
pub(crate) test_id: Option<String>,
51-
pub(crate) invalid_ast: bool,
52-
pub(crate) can_be_merged: bool,
38+
/// Builder type for `DocTestBuilder`.
39+
pub(crate) struct BuildDocTestBuilder<'a> {
40+
source: &'a str,
41+
crate_name: Option<&'a str>,
42+
edition: Edition,
43+
can_merge_doctests: bool,
44+
// If `test_id` is `None`, it means we're generating code for a code example "run" link.
45+
test_id: Option<String>,
46+
lang_str: Option<&'a LangString>,
47+
span: Span,
5348
}
5449

55-
impl DocTestBuilder {
56-
pub(crate) fn new(
57-
source: &str,
58-
crate_name: Option<&str>,
59-
edition: Edition,
60-
can_merge_doctests: bool,
61-
// If `test_id` is `None`, it means we're generating code for a code example "run" link.
62-
test_id: Option<String>,
63-
lang_str: Option<&LangString>,
64-
dcx: Option<DiagCtxtHandle<'_>>,
65-
span: Span,
66-
) -> Self {
50+
impl<'a> BuildDocTestBuilder<'a> {
51+
pub(crate) fn new(source: &'a str) -> Self {
52+
Self {
53+
source,
54+
crate_name: None,
55+
edition: DEFAULT_EDITION,
56+
can_merge_doctests: false,
57+
test_id: None,
58+
lang_str: None,
59+
span: DUMMY_SP,
60+
}
61+
}
62+
63+
#[inline]
64+
pub(crate) fn crate_name(mut self, crate_name: &'a str) -> Self {
65+
self.crate_name = Some(crate_name);
66+
self
67+
}
68+
69+
#[inline]
70+
pub(crate) fn can_merge_doctests(mut self, can_merge_doctests: bool) -> Self {
71+
self.can_merge_doctests = can_merge_doctests;
72+
self
73+
}
74+
75+
#[inline]
76+
pub(crate) fn test_id(mut self, test_id: String) -> Self {
77+
self.test_id = Some(test_id);
78+
self
79+
}
80+
81+
#[inline]
82+
pub(crate) fn lang_str(mut self, lang_str: &'a LangString) -> Self {
83+
self.lang_str = Some(lang_str);
84+
self
85+
}
86+
87+
#[inline]
88+
pub(crate) fn span(mut self, span: Span) -> Self {
89+
self.span = span;
90+
self
91+
}
92+
93+
#[inline]
94+
pub(crate) fn edition(mut self, edition: Edition) -> Self {
95+
self.edition = edition;
96+
self
97+
}
98+
99+
pub(crate) fn build(self, dcx: Option<DiagCtxtHandle<'_>>) -> DocTestBuilder {
100+
let BuildDocTestBuilder {
101+
source,
102+
crate_name,
103+
edition,
104+
can_merge_doctests,
105+
// If `test_id` is `None`, it means we're generating code for a code example "run" link.
106+
test_id,
107+
lang_str,
108+
span,
109+
} = self;
67110
let can_merge_doctests = can_merge_doctests
68111
&& lang_str.is_some_and(|lang_str| {
69112
!lang_str.compile_fail && !lang_str.test_harness && !lang_str.standalone_crate
@@ -89,7 +132,7 @@ impl DocTestBuilder {
89132
else {
90133
// If the AST returned an error, we don't want this doctest to be merged with the
91134
// others.
92-
return Self::invalid(
135+
return DocTestBuilder::invalid(
93136
String::new(),
94137
String::new(),
95138
String::new(),
@@ -109,7 +152,7 @@ impl DocTestBuilder {
109152
// If this is a merged doctest and a defined macro uses `$crate`, then the path will
110153
// not work, so better not put it into merged doctests.
111154
&& !(has_macro_def && everything_else.contains("$crate"));
112-
Self {
155+
DocTestBuilder {
113156
supports_color,
114157
has_main_fn,
115158
crate_attrs,
@@ -122,7 +165,26 @@ impl DocTestBuilder {
122165
can_be_merged,
123166
}
124167
}
168+
}
169+
170+
/// This struct contains information about the doctest itself which is then used to generate
171+
/// doctest source code appropriately.
172+
pub(crate) struct DocTestBuilder {
173+
pub(crate) supports_color: bool,
174+
pub(crate) already_has_extern_crate: bool,
175+
pub(crate) has_main_fn: bool,
176+
pub(crate) crate_attrs: String,
177+
/// If this is a merged doctest, it will be put into `everything_else`, otherwise it will
178+
/// put into `crate_attrs`.
179+
pub(crate) maybe_crate_attrs: String,
180+
pub(crate) crates: String,
181+
pub(crate) everything_else: String,
182+
pub(crate) test_id: Option<String>,
183+
pub(crate) invalid_ast: bool,
184+
pub(crate) can_be_merged: bool,
185+
}
125186

187+
impl DocTestBuilder {
126188
fn invalid(
127189
crate_attrs: String,
128190
maybe_crate_attrs: String,

src/librustdoc/doctest/tests.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use std::path::PathBuf;
22

3-
use rustc_span::DUMMY_SP;
4-
use rustc_span::edition::DEFAULT_EDITION;
5-
6-
use super::{DocTestBuilder, GlobalTestOptions};
3+
use super::{BuildDocTestBuilder, GlobalTestOptions};
74

85
fn make_test(
96
test_code: &str,
@@ -12,16 +9,14 @@ fn make_test(
129
opts: &GlobalTestOptions,
1310
test_id: Option<&str>,
1411
) -> (String, usize) {
15-
let doctest = DocTestBuilder::new(
16-
test_code,
17-
crate_name,
18-
DEFAULT_EDITION,
19-
false,
20-
test_id.map(|s| s.to_string()),
21-
None,
22-
None,
23-
DUMMY_SP,
24-
);
12+
let mut builder = BuildDocTestBuilder::new(test_code);
13+
if let Some(crate_name) = crate_name {
14+
builder = builder.crate_name(crate_name);
15+
}
16+
if let Some(test_id) = test_id {
17+
builder = builder.test_id(test_id.to_string());
18+
}
19+
let doctest = builder.build(None);
2520
let (code, line_offset) =
2621
doctest.generate_unique_doctest(test_code, dont_insert_main, opts, crate_name);
2722
(code, line_offset)

src/librustdoc/html/markdown.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use rustc_middle::ty::TyCtxt;
4545
pub(crate) use rustc_resolve::rustdoc::main_body_opts;
4646
use rustc_resolve::rustdoc::may_be_doc_link;
4747
use rustc_span::edition::Edition;
48-
use rustc_span::{DUMMY_SP, Span, Symbol};
48+
use rustc_span::{Span, Symbol};
4949
use tracing::{debug, trace};
5050

5151
use crate::clean::RenderedLink;
@@ -303,9 +303,11 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
303303
attrs: vec![],
304304
args_file: PathBuf::new(),
305305
};
306-
let doctest = doctest::DocTestBuilder::new(
307-
&test, krate, edition, false, None, None, None, DUMMY_SP,
308-
);
306+
let mut builder = doctest::BuildDocTestBuilder::new(&test).edition(edition);
307+
if let Some(krate) = krate {
308+
builder = builder.crate_name(krate);
309+
}
310+
let doctest = builder.build(None);
309311
let (test, _) = doctest.generate_unique_doctest(&test, false, &opts, krate);
310312
let channel = if test.contains("#![feature(") { "&amp;version=nightly" } else { "" };
311313

0 commit comments

Comments
 (0)