Skip to content

Commit 14ab4d0

Browse files
Add --doctest-compilation-args option to allow passing arguments to doctest compilation
1 parent 8a1f803 commit 14ab4d0

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/librustdoc/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ pub(crate) struct Options {
172172
/// This is mainly useful for other tools that reads that debuginfo to figure out
173173
/// how to call the compiler with the same arguments.
174174
pub(crate) expanded_args: Vec<String>,
175+
176+
/// Arguments to be used when compiling doctests.
177+
pub(crate) doctest_compilation_args: Vec<String>,
175178
}
176179

177180
impl fmt::Debug for Options {
@@ -774,6 +777,7 @@ impl Options {
774777
let scrape_examples_options = ScrapeExamplesOptions::new(matches, dcx);
775778
let with_examples = matches.opt_strs("with-examples");
776779
let call_locations = crate::scrape_examples::load_call_locations(with_examples, dcx);
780+
let doctest_compilation_args = matches.opt_strs("doctest-compilation-args");
777781

778782
let unstable_features =
779783
rustc_feature::UnstableFeatures::from_environment(crate_name.as_deref());
@@ -819,6 +823,7 @@ impl Options {
819823
scrape_examples_options,
820824
unstable_features,
821825
expanded_args: args,
826+
doctest_compilation_args,
822827
};
823828
let render_options = RenderOptions {
824829
output,

src/librustdoc/doctest.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ pub(crate) fn generate_args_file(file_path: &Path, options: &RustdocOptions) ->
7878
content.push(format!("-Z{unstable_option_str}"));
7979
}
8080

81+
for compilation_args in &options.doctest_compilation_args {
82+
for flag in compilation_args
83+
.split_whitespace()
84+
.map(|flag| flag.trim())
85+
.filter(|flag| !flag.is_empty())
86+
{
87+
// Very simple parsing implementation. Might be a good idea to correctly handle strings.
88+
content.push(flag.to_string());
89+
}
90+
}
91+
8192
let content = content.join("\n");
8293

8394
file.write_all(content.as_bytes())

src/librustdoc/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,14 @@ fn opts() -> Vec<RustcOptGroup> {
685685
"[rust]",
686686
),
687687
opt(Unstable, Flag, "", "html-no-source", "Disable HTML source code pages generation", ""),
688+
unstable("doctest-compilation-args", |o| {
689+
o.optmulti(
690+
"",
691+
"doctest-compilation-args",
692+
"",
693+
"add arguments to be used when compiling doctests",
694+
)
695+
}),
688696
]
689697
}
690698

0 commit comments

Comments
 (0)