Skip to content

Commit c228271

Browse files
Add --doctest-compilation-args option to allow passing arguments to doctest compilation
1 parent 45fbf41 commit c228271

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/librustdoc/config.rs

+5
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ pub(crate) struct Options {
166166
/// This is mainly useful for other tools that reads that debuginfo to figure out
167167
/// how to call the compiler with the same arguments.
168168
pub(crate) expanded_args: Vec<String>,
169+
170+
/// Arguments to be used when compiling doctests.
171+
pub(crate) doctest_compilation_args: Vec<String>,
169172
}
170173

171174
impl fmt::Debug for Options {
@@ -747,6 +750,7 @@ impl Options {
747750
let scrape_examples_options = ScrapeExamplesOptions::new(matches, dcx);
748751
let with_examples = matches.opt_strs("with-examples");
749752
let call_locations = crate::scrape_examples::load_call_locations(with_examples, dcx);
753+
let doctest_compilation_args = matches.opt_strs("doctest-compilation-args");
750754

751755
let unstable_features =
752756
rustc_feature::UnstableFeatures::from_environment(crate_name.as_deref());
@@ -793,6 +797,7 @@ impl Options {
793797
scrape_examples_options,
794798
unstable_features,
795799
expanded_args: args,
800+
doctest_compilation_args,
796801
};
797802
let render_options = RenderOptions {
798803
output,

src/librustdoc/doctest.rs

+11
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ pub(crate) fn generate_args_file(file_path: &Path, options: &RustdocOptions) ->
7676
content.push(format!("-Z{unstable_option_str}"));
7777
}
7878

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

8192
file.write_all(content.as_bytes())

src/librustdoc/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,14 @@ fn opts() -> Vec<RustcOptGroup> {
653653
unstable("html-no-source", |o| {
654654
o.optflag("", "html-no-source", "Disable HTML source code pages generation")
655655
}),
656+
unstable("doctest-compilation-args", |o| {
657+
o.optmulti(
658+
"",
659+
"doctest-compilation-args",
660+
"",
661+
"add arguments to be used when compiling doctests",
662+
)
663+
}),
656664
]
657665
}
658666

0 commit comments

Comments
 (0)