Skip to content

Commit 307a67a

Browse files
committed
Replace flag --doctest-compilation-args with a simpler one: --doctest-build-arg
More notably, the value of the new flag does *not* get lexed shell-like and ad hoc.
1 parent 58c2dd9 commit 307a67a

File tree

8 files changed

+15
-79
lines changed

8 files changed

+15
-79
lines changed

Diff for: src/librustdoc/config.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ pub(crate) struct Options {
178178
pub(crate) expanded_args: Vec<String>,
179179

180180
/// Arguments to be used when compiling doctests.
181-
pub(crate) doctest_compilation_args: Vec<String>,
181+
pub(crate) doctest_build_args: Vec<String>,
182182
}
183183

184184
impl fmt::Debug for Options {
@@ -808,7 +808,7 @@ impl Options {
808808
let scrape_examples_options = ScrapeExamplesOptions::new(matches, dcx);
809809
let with_examples = matches.opt_strs("with-examples");
810810
let call_locations = crate::scrape_examples::load_call_locations(with_examples, dcx);
811-
let doctest_compilation_args = matches.opt_strs("doctest-compilation-args");
811+
let doctest_build_args = matches.opt_strs("doctest-build-arg");
812812

813813
let unstable_features =
814814
rustc_feature::UnstableFeatures::from_environment(crate_name.as_deref());
@@ -858,7 +858,7 @@ impl Options {
858858
scrape_examples_options,
859859
unstable_features,
860860
expanded_args: args,
861-
doctest_compilation_args,
861+
doctest_build_args,
862862
};
863863
let render_options = RenderOptions {
864864
output,

Diff for: src/librustdoc/doctest.rs

+1-43
Original file line numberDiff line numberDiff line change
@@ -51,46 +51,6 @@ pub(crate) struct GlobalTestOptions {
5151
pub(crate) args_file: PathBuf,
5252
}
5353

54-
/// Function used to split command line arguments just like a shell would.
55-
fn split_args(args: &str) -> Vec<String> {
56-
let mut out = Vec::new();
57-
let mut iter = args.chars();
58-
let mut current = String::new();
59-
60-
while let Some(c) = iter.next() {
61-
if c == '\\' {
62-
if let Some(c) = iter.next() {
63-
// If it's escaped, even a quote or a whitespace will be ignored.
64-
current.push(c);
65-
}
66-
} else if c == '"' || c == '\'' {
67-
while let Some(new_c) = iter.next() {
68-
if new_c == c {
69-
break;
70-
} else if new_c == '\\' {
71-
if let Some(c) = iter.next() {
72-
// If it's escaped, even a quote will be ignored.
73-
current.push(c);
74-
}
75-
} else {
76-
current.push(new_c);
77-
}
78-
}
79-
} else if " \n\t\r".contains(c) {
80-
if !current.is_empty() {
81-
out.push(current.clone());
82-
current.clear();
83-
}
84-
} else {
85-
current.push(c);
86-
}
87-
}
88-
if !current.is_empty() {
89-
out.push(current);
90-
}
91-
out
92-
}
93-
9454
pub(crate) fn generate_args_file(file_path: &Path, options: &RustdocOptions) -> Result<(), String> {
9555
let mut file = File::create(file_path)
9656
.map_err(|error| format!("failed to create args file: {error:?}"))?;
@@ -119,9 +79,7 @@ pub(crate) fn generate_args_file(file_path: &Path, options: &RustdocOptions) ->
11979
content.push(format!("-Z{unstable_option_str}"));
12080
}
12181

122-
for compilation_args in &options.doctest_compilation_args {
123-
content.extend(split_args(compilation_args));
124-
}
82+
content.extend(options.doctest_build_args.clone());
12583

12684
let content = content.join("\n");
12785

Diff for: src/librustdoc/doctest/tests.rs

-22
Original file line numberDiff line numberDiff line change
@@ -381,28 +381,6 @@ fn main() {
381381
assert_eq!((output, len), (expected, 1));
382382
}
383383

384-
#[test]
385-
fn check_split_args() {
386-
fn compare(input: &str, expected: &[&str]) {
387-
let output = super::split_args(input);
388-
let expected = expected.iter().map(|s| s.to_string()).collect::<Vec<_>>();
389-
assert_eq!(expected, output, "test failed for {input:?}");
390-
}
391-
392-
compare("'a' \"b\"c", &["a", "bc"]);
393-
compare("'a' \"b \"c d", &["a", "b c", "d"]);
394-
compare("'a' \"b\\\"c\"", &["a", "b\"c"]);
395-
compare("'a\"'", &["a\""]);
396-
compare("\"a'\"", &["a'"]);
397-
compare("\\ a", &[" a"]);
398-
compare("\\\\", &["\\"]);
399-
compare("a'", &["a"]);
400-
compare("a ", &["a"]);
401-
compare("a b", &["a", "b"]);
402-
compare("a\n\t \rb", &["a", "b"]);
403-
compare("a\n\t1 \rb", &["a", "1", "b"]);
404-
}
405-
406384
#[test]
407385
fn comment_in_attrs() {
408386
// If there is an inline code comment after attributes, we need to ensure that

Diff for: src/librustdoc/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -646,9 +646,9 @@ fn opts() -> Vec<RustcOptGroup> {
646646
Unstable,
647647
Multi,
648648
"",
649-
"doctest-compilation-args",
650-
"",
651-
"add arguments to be used when compiling doctests",
649+
"doctest-build-arg",
650+
"One argument (of possibly many) to be used when compiling doctests",
651+
"ARG",
652652
),
653653
opt(
654654
Unstable,

Diff for: tests/run-make/rustdoc-default-output/output-default.stdout

+3-2
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,9 @@ Options:
191191
from provided path. Only use with --merge=finalize
192192
--html-no-source
193193
Disable HTML source code pages generation
194-
--doctest-compilation-args add arguments to be used when compiling doctests
195-
194+
--doctest-build-arg ARG
195+
One argument (of possibly many) to be used when
196+
compiling doctests
196197
--disable-minification
197198
disable the minification of CSS/JS files
198199
(perma-unstable, do not use with cached files)

Diff for: tests/rustdoc-ui/doctest/rustflags-multiple-args.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
// This test checks that the test behave when `--doctest-compilation-args` is passed
2-
// multiple times.
1+
// This test checks that the test behave when `--doctest-build-arg` is passed multiple times.
32

43
//@ check-pass
5-
//@ compile-flags: --test -Zunstable-options --doctest-compilation-args=--cfg=testcase_must_be_present
6-
//@ compile-flags: --doctest-compilation-args=--cfg=another
4+
//@ compile-flags: --test -Zunstable-options --doctest-build-arg=--cfg=testcase_must_be_present
5+
//@ compile-flags: --doctest-build-arg=--cfg=another
76
//@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR"
87
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
98

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
running 1 test
3-
test $DIR/rustflags-multiple-args.rs - Bar (line 10) ... ok
3+
test $DIR/rustflags-multiple-args.rs - Bar (line 9) ... ok
44

55
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
66

Diff for: tests/rustdoc-ui/doctest/rustflags.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ check-pass
2-
//@ compile-flags: --test -Zunstable-options --doctest-compilation-args=--cfg=testcase_must_be_present
2+
//@ compile-flags: --test -Zunstable-options --doctest-build-arg=--cfg=testcase_must_be_present
33
//@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR"
44
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
55

0 commit comments

Comments
 (0)