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

Commit 7c99f90

Browse files
committed
cargo-miri: clean up phase dispatching a bit
1 parent dac1676 commit 7c99f90

File tree

1 file changed

+38
-57
lines changed

1 file changed

+38
-57
lines changed

cargo-miri/bin.rs

Lines changed: 38 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ struct CrateRunEnv {
6060

6161
impl CrateRunEnv {
6262
/// Gather all the information we need.
63-
fn collect(args: env::Args, capture_stdin: bool) -> Self {
63+
fn collect(args: impl Iterator<Item = String>, capture_stdin: bool) -> Self {
6464
let args = args.collect();
6565
let env = env::vars_os().collect();
6666
let current_dir = env::current_dir().unwrap().into_os_string();
@@ -757,7 +757,7 @@ enum RustcPhase {
757757
Rustdoc,
758758
}
759759

760-
fn phase_rustc(mut args: env::Args, phase: RustcPhase) {
760+
fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
761761
/// Determines if we are being invoked (as rustc) to build a crate for
762762
/// the "target" architecture, in contrast to the "host" architecture.
763763
/// Host crates are for build scripts and proc macros and still need to
@@ -978,10 +978,11 @@ enum RunnerPhase {
978978
Rustdoc,
979979
}
980980

981-
fn phase_runner(binary: &Path, binary_args: impl Iterator<Item = String>, phase: RunnerPhase) {
981+
fn phase_runner(mut binary_args: impl Iterator<Item = String>, phase: RunnerPhase) {
982982
let verbose = std::env::var("MIRI_VERBOSE")
983983
.map_or(0, |verbose| verbose.parse().expect("verbosity flag must be an integer"));
984984

985+
let binary = binary_args.next().unwrap();
985986
let file = File::open(&binary)
986987
.unwrap_or_else(|_| show_error(format!("file {:?} not found or `cargo-miri` invoked incorrectly; please only invoke this binary through `cargo miri`", binary)));
987988
let file = BufReader::new(file);
@@ -1071,25 +1072,16 @@ fn phase_runner(binary: &Path, binary_args: impl Iterator<Item = String>, phase:
10711072
}
10721073
}
10731074

1074-
fn phase_rustdoc(fst_arg: &str, mut args: env::Args) {
1075+
fn phase_rustdoc(mut args: impl Iterator<Item = String>) {
10751076
let verbose = std::env::var("MIRI_VERBOSE")
10761077
.map_or(0, |verbose| verbose.parse().expect("verbosity flag must be an integer"));
10771078

10781079
// phase_cargo_miri sets the RUSTDOC env var to ourselves, so we can't use that here;
10791080
// just default to a straight-forward invocation for now:
10801081
let mut cmd = Command::new("rustdoc");
10811082

1082-
// Because of the way the main function is structured, we have to take the first argument spearately
1083-
// from the rest; to simplify the following argument patching loop, we'll just skip that one.
1084-
// This is fine for now, because cargo will never pass --extern arguments in the first position,
1085-
// but we should defensively assert that this will work.
10861083
let extern_flag = "--extern";
1087-
assert!(fst_arg != extern_flag);
1088-
cmd.arg(fst_arg);
1089-
10901084
let runtool_flag = "--runtool";
1091-
// `crossmode` records if *any* argument matches `runtool_flag`; here we check the first one.
1092-
let mut crossmode = fst_arg == runtool_flag;
10931085
while let Some(arg) = args.next() {
10941086
if arg == extern_flag {
10951087
// Patch --extern arguments to use *.rmeta files, since phase_cargo_rustc only creates stub *.rlib files.
@@ -1098,17 +1090,12 @@ fn phase_rustdoc(fst_arg: &str, mut args: env::Args) {
10981090
// An existing --runtool flag indicates cargo is running in cross-target mode, which we don't support.
10991091
// Note that this is only passed when cargo is run with the unstable -Zdoctest-xcompile flag;
11001092
// otherwise, we won't be called as rustdoc at all.
1101-
crossmode = true;
1102-
break;
1093+
show_error(format!("cross-interpreting doctests is not currently supported by Miri."));
11031094
} else {
11041095
cmd.arg(arg);
11051096
}
11061097
}
11071098

1108-
if crossmode {
1109-
show_error(format!("cross-interpreting doctests is not currently supported by Miri."));
1110-
}
1111-
11121099
// Doctests of `proc-macro` crates (and their dependencies) are always built for the host,
11131100
// so we are not able to run them in Miri.
11141101
if ArgFlagValueIter::new("--crate-type").any(|crate_type| crate_type == "proc-macro") {
@@ -1178,52 +1165,46 @@ fn main() {
11781165
// since we don't specify any runtool-args, and rustdoc supplies multiple arguments to
11791166
// the test-builder unconditionally, we can just check the number of remaining arguments:
11801167
if args.len() == 1 {
1181-
let arg = args.next().unwrap();
1182-
let binary = Path::new(&arg);
1183-
if binary.exists() {
1184-
phase_runner(binary, args, RunnerPhase::Rustdoc);
1185-
} else {
1186-
show_error(format!(
1187-
"`cargo-miri` called with non-existing path argument `{}` in rustdoc mode; please invoke this binary through `cargo miri`",
1188-
arg
1189-
));
1190-
}
1168+
phase_runner(args, RunnerPhase::Rustdoc);
11911169
} else {
11921170
phase_rustc(args, RustcPhase::Rustdoc);
11931171
}
11941172

11951173
return;
11961174
}
11971175

1198-
match args.next().as_deref() {
1199-
Some("miri") => phase_cargo_miri(args),
1200-
Some(arg) => {
1201-
// If the first arg is equal to the RUSTC variable (which should be set at this point),
1202-
// then we need to behave as rustc. This is the somewhat counter-intuitive behavior of
1203-
// having both RUSTC and RUSTC_WRAPPER set (see
1204-
// https://github.com/rust-lang/cargo/issues/10886).
1205-
if arg == env::var_os("RUSTC").unwrap() {
1206-
return phase_rustc(args, RustcPhase::Build);
1207-
}
1208-
// We have to distinguish the "runner" and "rustdoc" cases.
1209-
// As runner, the first argument is the binary (a file that should exist, with an absolute path);
1210-
// as rustdoc, the first argument is a flag (`--something`).
1211-
let binary = Path::new(arg);
1212-
if binary.exists() {
1213-
assert!(!arg.starts_with("--")); // not a flag
1214-
phase_runner(binary, args, RunnerPhase::Cargo);
1215-
} else if arg.starts_with("--") {
1216-
phase_rustdoc(arg, args);
1217-
} else {
1218-
show_error(format!(
1219-
"`cargo-miri` called with unexpected first argument `{}`; please only invoke this binary through `cargo miri`",
1220-
arg
1221-
));
1222-
}
1176+
let mut args = args.peekable();
1177+
if args.next_if(|a| a == "miri").is_some() {
1178+
phase_cargo_miri(args);
1179+
} else if let Some(arg) = args.peek().cloned() {
1180+
// Cargo calls us for everything it does. We could be invoked as rustc, rustdoc, or the runner.
1181+
1182+
// If the first arg is equal to the RUSTC variable (which should be set at this point),
1183+
// then we need to behave as rustc. This is the somewhat counter-intuitive behavior of
1184+
// having both RUSTC and RUSTC_WRAPPER set (see
1185+
// https://github.com/rust-lang/cargo/issues/10886).
1186+
if arg == env::var("RUSTC").unwrap() {
1187+
args.next().unwrap(); // consume wrapped RUSTC command.
1188+
return phase_rustc(args, RustcPhase::Build);
12231189
}
1224-
_ =>
1190+
// We have to distinguish the "runner" and "rustdoc" cases.
1191+
// As runner, the first argument is the binary (a file that should exist, with an absolute path);
1192+
// as rustdoc, the first argument is a flag (`--something`).
1193+
let binary = Path::new(&arg);
1194+
if binary.exists() {
1195+
assert!(!arg.starts_with("--")); // not a flag
1196+
phase_runner(args, RunnerPhase::Cargo);
1197+
} else if arg.starts_with("--") {
1198+
phase_rustdoc(args);
1199+
} else {
12251200
show_error(format!(
1226-
"`cargo-miri` called without first argument; please only invoke this binary through `cargo miri`"
1227-
)),
1201+
"`cargo-miri` called with unexpected first argument `{}`; please only invoke this binary through `cargo miri`",
1202+
arg
1203+
));
1204+
}
1205+
} else {
1206+
show_error(format!(
1207+
"`cargo-miri` called without first argument; please only invoke this binary through `cargo miri`"
1208+
));
12281209
}
12291210
}

0 commit comments

Comments
 (0)