Skip to content

Commit 7a5bbe0

Browse files
authored
Rollup merge of #139863 - fmease:simp-doctest-build-arg-passing, r=GuillaumeGomez
rustdoc: Replace unstable flag `--doctest-compilation-args` with a simpler one: `--doctest-build-arg` Tracking issue: #134172. Context: #137096 (comment) Yeets the ad hoc shell-like lexer for 'nested' program arguments. No FCP necessary since the flag is unstable. I've chosen to replace `compilation` with `build` because it's shorter (you now need to pass it multiple times in order to pass many arguments to the doctest compiler, so it matters a bit) and since I prefer it esthetically. **Issue**: Even though we don't process the argument passed to `--doctest-build-arg`, we end up passing it via an argument file (`rustc `@argfile`)` which delimits arguments by line break (LF or CRLF, [via](https://doc.rust-lang.org/rustc/command-line-arguments.html#path-load-command-line-flags-from-a-path)) meaning ultimately the arguments still get split which is unfortunate. Still, I think this change is an improvement over the status quo. I'll update the tracking issue if/once this PR merges. I'll also add the (CR)LF issue to 'unresolved question'. r? GuillaumeGomez r? notriddle
2 parents dcecb99 + 307a67a commit 7a5bbe0

File tree

8 files changed

+15
-79
lines changed

8 files changed

+15
-79
lines changed

src/librustdoc/config.rs

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

176176
/// Arguments to be used when compiling doctests.
177-
pub(crate) doctest_compilation_args: Vec<String>,
177+
pub(crate) doctest_build_args: Vec<String>,
178178
}
179179

180180
impl fmt::Debug for Options {
@@ -802,7 +802,7 @@ impl Options {
802802
let scrape_examples_options = ScrapeExamplesOptions::new(matches, dcx);
803803
let with_examples = matches.opt_strs("with-examples");
804804
let call_locations = crate::scrape_examples::load_call_locations(with_examples, dcx);
805-
let doctest_compilation_args = matches.opt_strs("doctest-compilation-args");
805+
let doctest_build_args = matches.opt_strs("doctest-build-arg");
806806

807807
let unstable_features =
808808
rustc_feature::UnstableFeatures::from_environment(crate_name.as_deref());
@@ -851,7 +851,7 @@ impl Options {
851851
scrape_examples_options,
852852
unstable_features,
853853
expanded_args: args,
854-
doctest_compilation_args,
854+
doctest_build_args,
855855
};
856856
let render_options = RenderOptions {
857857
output,

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

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

src/librustdoc/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -654,9 +654,9 @@ fn opts() -> Vec<RustcOptGroup> {
654654
Unstable,
655655
Multi,
656656
"",
657-
"doctest-compilation-args",
658-
"",
659-
"add arguments to be used when compiling doctests",
657+
"doctest-build-arg",
658+
"One argument (of possibly many) to be used when compiling doctests",
659+
"ARG",
660660
),
661661
opt(
662662
Unstable,

tests/run-make/rustdoc-default-output/output-default.stdout

+3-2
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,9 @@ Options:
188188
from provided path. Only use with --merge=finalize
189189
--html-no-source
190190
Disable HTML source code pages generation
191-
--doctest-compilation-args add arguments to be used when compiling doctests
192-
191+
--doctest-build-arg ARG
192+
One argument (of possibly many) to be used when
193+
compiling doctests
193194
--disable-minification
194195
disable the minification of CSS/JS files
195196
(perma-unstable, do not use with cached files)

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

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

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)