Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 4655382

Browse files
committed
reuse arg flag parse logic in rustdoc handling
1 parent e12df0f commit 4655382

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

cargo-miri/src/phases.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,17 +310,17 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
310310
let mut cmd = miri();
311311

312312
// Ensure --emit argument for a check-only build is present.
313-
// We cannot use the usual helpers since we need to check specifically in `env.args`.
314-
if let Some(i) = env.args.iter().position(|arg| arg.starts_with("--emit=")) {
313+
if let Some(val) = ArgFlagValueIter::new(env.args.clone().into_iter(), "--emit").next()
314+
{
315315
// For `no_run` tests, rustdoc passes a `--emit` flag; make sure it has the right shape.
316-
assert_eq!(env.args[i], "--emit=metadata");
316+
assert_eq!(val, "metadata");
317317
} else {
318318
// For all other kinds of tests, we can just add our flag.
319319
cmd.arg("--emit=metadata");
320320
}
321321

322322
// Alter the `-o` parameter so that it does not overwrite the JSON file we stored above.
323-
let mut args = env.args.clone();
323+
let mut args = env.args;
324324
for i in 0..args.len() {
325325
if args[i] == "-o" {
326326
args[i + 1].push_str(".miri");
@@ -344,7 +344,7 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
344344
return;
345345
}
346346

347-
if runnable_crate && ArgFlagValueIter::new("--extern").any(|krate| krate == "proc_macro") {
347+
if runnable_crate && get_arg_flag_values("--extern").any(|krate| krate == "proc_macro") {
348348
// This is a "runnable" `proc-macro` crate (unit tests). We do not support
349349
// interpreting that under Miri now, so we write a JSON file to (display a
350350
// helpful message and) skip it in the runner phase.
@@ -568,7 +568,7 @@ pub fn phase_rustdoc(mut args: impl Iterator<Item = String>) {
568568

569569
// Doctests of `proc-macro` crates (and their dependencies) are always built for the host,
570570
// so we are not able to run them in Miri.
571-
if ArgFlagValueIter::new("--crate-type").any(|crate_type| crate_type == "proc-macro") {
571+
if get_arg_flag_values("--crate-type").any(|crate_type| crate_type == "proc-macro") {
572572
eprintln!("Running doctests of `proc-macro` crates is not currently supported by Miri.");
573573
return;
574574
}

cargo-miri/src/util.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn has_arg_flag(name: &str) -> bool {
8181

8282
/// Determines how many times a `--flag` is present.
8383
pub fn num_arg_flag(name: &str) -> usize {
84-
std::env::args().take_while(|val| val != "--").filter(|val| val == name).count()
84+
env::args().take_while(|val| val != "--").filter(|val| val == name).count()
8585
}
8686

8787
/// Yields all values of command line flag `name` as `Ok(arg)`, and all other arguments except
@@ -121,15 +121,15 @@ impl<I: Iterator<Item = String>> Iterator for ArgSplitFlagValue<'_, I> {
121121
}
122122

123123
/// Yields all values of command line flag `name`.
124-
pub struct ArgFlagValueIter<'a>(ArgSplitFlagValue<'a, env::Args>);
124+
pub struct ArgFlagValueIter<'a, I>(ArgSplitFlagValue<'a, I>);
125125

126-
impl<'a> ArgFlagValueIter<'a> {
127-
pub fn new(name: &'a str) -> Self {
128-
Self(ArgSplitFlagValue::new(env::args(), name))
126+
impl<'a, I: Iterator<Item = String>> ArgFlagValueIter<'a, I> {
127+
pub fn new(args: I, name: &'a str) -> Self {
128+
Self(ArgSplitFlagValue::new(args, name))
129129
}
130130
}
131131

132-
impl Iterator for ArgFlagValueIter<'_> {
132+
impl<I: Iterator<Item = String>> Iterator for ArgFlagValueIter<'_, I> {
133133
type Item = String;
134134

135135
fn next(&mut self) -> Option<Self::Item> {
@@ -141,9 +141,14 @@ impl Iterator for ArgFlagValueIter<'_> {
141141
}
142142
}
143143

144+
/// Gets the values of a `--flag`.
145+
pub fn get_arg_flag_values<'a>(name: &'a str) -> impl Iterator<Item = String> + 'a {
146+
ArgFlagValueIter::new(env::args(), name)
147+
}
148+
144149
/// Gets the value of a `--flag`.
145150
pub fn get_arg_flag_value(name: &str) -> Option<String> {
146-
ArgFlagValueIter::new(name).next()
151+
get_arg_flag_values(name).next()
147152
}
148153

149154
/// Escapes `s` in a way that is suitable for using it as a string literal in TOML syntax.
@@ -296,7 +301,7 @@ fn cargo_extra_flags() -> Vec<String> {
296301

297302
// Forward `--config` flags.
298303
let config_flag = "--config";
299-
for arg in ArgFlagValueIter::new(config_flag) {
304+
for arg in get_arg_flag_values(config_flag) {
300305
flags.push(config_flag.to_string());
301306
flags.push(arg);
302307
}

0 commit comments

Comments
 (0)