Skip to content

Commit e12a721

Browse files
committed
no need for an exhaustive enum of subcommands
1 parent ebda1de commit e12a721

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

cargo-miri/bin.rs

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(let_else)]
12
#![allow(clippy::useless_format, clippy::derive_partial_eq_without_eq)]
23

34
mod version;
@@ -34,11 +35,12 @@ Examples:
3435
cargo miri test -- test-suite-filter
3536
"#;
3637

37-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
38+
#[derive(Clone, Debug)]
3839
enum MiriCommand {
39-
Run,
40-
Test,
40+
/// Our own special 'setup' command.
4141
Setup,
42+
/// A command to be forwarded to cargo.
43+
Forward(String),
4244
}
4345

4446
/// The information to run a crate with the given environment.
@@ -339,17 +341,18 @@ fn write_to_file(filename: &Path, content: &str) {
339341
/// Performs the setup required to make `cargo miri` work: Getting a custom-built libstd. Then sets
340342
/// `MIRI_SYSROOT`. Skipped if `MIRI_SYSROOT` is already set, in which case we expect the user has
341343
/// done all this already.
342-
fn setup(subcommand: MiriCommand) {
344+
fn setup(subcommand: &MiriCommand) {
345+
let only_setup = matches!(subcommand, MiriCommand::Setup);
343346
if std::env::var_os("MIRI_SYSROOT").is_some() {
344-
if subcommand == MiriCommand::Setup {
347+
if only_setup {
345348
println!("WARNING: MIRI_SYSROOT already set, not doing anything.")
346349
}
347350
return;
348351
}
349352

350353
// Subcommands other than `setup` will do a setup if necessary, but
351354
// interactively confirm first.
352-
let ask_user = subcommand != MiriCommand::Setup;
355+
let ask_user = !only_setup;
353356

354357
// First, we need xargo.
355358
if xargo_version().map_or(true, |v| v < XARGO_MIN_VERSION) {
@@ -495,11 +498,11 @@ path = "lib.rs"
495498
let sysroot = if target == &host { dir.join("HOST") } else { PathBuf::from(dir) };
496499
std::env::set_var("MIRI_SYSROOT", &sysroot); // pass the env var to the processes we spawn, which will turn it into "--sysroot" flags
497500
// Figure out what to print.
498-
let print_sysroot = subcommand == MiriCommand::Setup && has_arg_flag("--print-sysroot"); // whether we just print the sysroot path
501+
let print_sysroot = only_setup && has_arg_flag("--print-sysroot"); // whether we just print the sysroot path
499502
if print_sysroot {
500503
// Print just the sysroot and nothing else; this way we do not need any escaping.
501504
println!("{}", sysroot.display());
502-
} else if subcommand == MiriCommand::Setup {
505+
} else if only_setup {
503506
println!("A libstd for Miri is now available in `{}`.", sysroot.display());
504507
}
505508
}
@@ -573,10 +576,12 @@ fn phase_cargo_miri(mut args: env::Args) {
573576
// Require a subcommand before any flags.
574577
// We cannot know which of those flags take arguments and which do not,
575578
// so we cannot detect subcommands later.
576-
let subcommand = match args.next().as_deref() {
577-
Some("test" | "t") => MiriCommand::Test,
578-
Some("run" | "r") => MiriCommand::Run,
579-
Some("setup") => MiriCommand::Setup,
579+
let Some(subcommand) = args.next() else {
580+
show_error(format!("`cargo miri` needs to be called with a subcommand (`run`, `test`)"));
581+
};
582+
let subcommand = match &*subcommand {
583+
"setup" => MiriCommand::Setup,
584+
"test" | "t" | "run" | "r" => MiriCommand::Forward(subcommand),
580585
// Invalid command.
581586
_ =>
582587
show_error(format!(
@@ -586,7 +591,7 @@ fn phase_cargo_miri(mut args: env::Args) {
586591
let verbose = has_arg_flag("-v");
587592

588593
// We always setup.
589-
setup(subcommand);
594+
setup(&subcommand);
590595

591596
// Invoke actual cargo for the job, but with different flags.
592597
// We re-use `cargo test` and `cargo run`, which makes target and binary handling very easy but
@@ -596,8 +601,7 @@ fn phase_cargo_miri(mut args: env::Args) {
596601
// harder.
597602
let cargo_miri_path = std::env::current_exe().expect("current executable path invalid");
598603
let cargo_cmd = match subcommand {
599-
MiriCommand::Test => "test",
600-
MiriCommand::Run => "run",
604+
MiriCommand::Forward(s) => s,
601605
MiriCommand::Setup => return, // `cargo miri setup` stops here.
602606
};
603607
let mut cmd = cargo();

0 commit comments

Comments
 (0)