Skip to content

Commit 68f1f5b

Browse files
committed
detect when the user passes Miri's flags the old way, and support this for now
1 parent 8fd1674 commit 68f1f5b

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

cargo-miri/bin.rs

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,21 @@ use rustc_version::VersionMeta;
1212

1313
const XARGO_MIN_VERSION: (u32, u32, u32) = (0, 3, 22);
1414

15-
const CARGO_MIRI_HELP: &str = r#"Interprets bin crates and tests in Miri
15+
const CARGO_MIRI_HELP: &str = r#"Runs binary crates and tests in Miri
1616
1717
Usage:
18-
cargo miri [subcommand] [<cargo options>...] [--] [<miri options>...] [--] [<program/test suite options>...]
18+
cargo miri [subcommand] [<cargo options>...] [--] [<program/test suite options>...]
1919
2020
Subcommands:
21-
run Run binaries (default)
21+
run Run binaries
2222
test Run tests
2323
setup Only perform automatic setup, but without asking questions (for getting a proper libstd)
2424
25-
Common options:
26-
-h, --help Print this message
27-
--features Features to compile for the package
28-
-V, --version Print version info and exit
29-
30-
Other [options] are the same as `cargo check`. Everything after the first "--" is
31-
passed verbatim to Miri, which will pass everything after the second "--" verbatim
32-
to the interpreted program.
25+
The cargo options are exactly the same as for `cargo run` and `cargo test`, respectively.
3326
3427
Examples:
35-
cargo miri run -- -Zmiri-disable-stacked-borrows
36-
cargo miri test -- -- test-suite-filter
28+
cargo miri run
29+
cargo miri test -- test-suite-filter
3730
"#;
3831

3932
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -390,7 +383,7 @@ fn phase_cargo_miri(mut args: env::Args) {
390383
Some("run") => MiriCommand::Run,
391384
Some("setup") => MiriCommand::Setup,
392385
// Invalid command.
393-
_ => show_error(format!("`cargo miri` must be immediately followed by `test`, `run`, or `setup`.")),
386+
_ => show_error(format!("`cargo miri` supports the following subcommands: `run`, `test`, and `setup`.")),
394387
};
395388
let verbose = has_arg_flag("-v");
396389

@@ -421,8 +414,44 @@ fn phase_cargo_miri(mut args: env::Args) {
421414
host
422415
};
423416

424-
// Forward all further arguments.
425-
cmd.args(args);
417+
// Forward all further arguments. We do some processing here because we want to
418+
// detect people still using the old way of passing flags to Miri
419+
// (`cargo miri -- -Zmiri-foo`).
420+
while let Some(arg) = args.next() {
421+
cmd.arg(&arg);
422+
if arg == "--" {
423+
// Check if the next argument starts with `-Zmiri`. If yes, we assume
424+
// this is an old-style invocation.
425+
if let Some(next_arg) = args.next() {
426+
if next_arg.starts_with("-Zmiri") {
427+
eprintln!(
428+
"WARNING: it seems like you are setting Miri's flags in `cargo miri` the old way,\n\
429+
i.e., by passing them after the first `--`. This style is deprecated; please set\n\
430+
the MIRIFLAGS environment variable instead. `cargo miri run/test` now interprets\n\
431+
arguments the exact same way as `cargo run/test`."
432+
);
433+
// Old-style invocation. Turn these into MIRIFLAGS.
434+
let mut miriflags = env::var("MIRIFLAGS").unwrap_or_default();
435+
miriflags.push(' ');
436+
miriflags.push_str(&next_arg);
437+
while let Some(further_arg) = args.next() {
438+
if further_arg == "--" {
439+
// End of the Miri flags!
440+
break;
441+
}
442+
miriflags.push(' ');
443+
miriflags.push_str(&further_arg);
444+
}
445+
env::set_var("MIRIFLAGS", miriflags);
446+
// Pass the remaining flags to cargo.
447+
cmd.args(args);
448+
break;
449+
}
450+
// Not a Miri argument after all, make sure we pass it to cargo.
451+
cmd.arg(next_arg);
452+
}
453+
}
454+
}
426455

427456
// Set `RUSTC_WRAPPER` to ourselves. Cargo will prepend that binary to its usual invocation,
428457
// i.e., the first argument is `rustc` -- which is what we use in `main` to distinguish

0 commit comments

Comments
 (0)