Skip to content

Commit 174a92c

Browse files
committed
detect when the user passes Miri's flags the old way, and support this for now
1 parent 93bedd0 commit 174a92c

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)]
@@ -393,7 +386,7 @@ fn phase_cargo_miri(mut args: env::Args) {
393386
Some("run") => MiriCommand::Run,
394387
Some("setup") => MiriCommand::Setup,
395388
// Invalid command.
396-
_ => show_error(format!("`cargo miri` must be immediately followed by `test`, `run`, or `setup`.")),
389+
_ => show_error(format!("`cargo miri` supports the following subcommands: `run`, `test`, and `setup`.")),
397390
};
398391
let verbose = has_arg_flag("-v");
399392

@@ -424,8 +417,44 @@ fn phase_cargo_miri(mut args: env::Args) {
424417
host
425418
};
426419

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

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

0 commit comments

Comments
 (0)