Skip to content

Commit 1164815

Browse files
committed
make cargo-miri show_error a bit nicer to use
1 parent 12e3f75 commit 1164815

File tree

4 files changed

+35
-34
lines changed

4 files changed

+35
-34
lines changed

cargo-miri/src/main.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#![feature(let_else)]
22
#![allow(clippy::useless_format, clippy::derive_partial_eq_without_eq, rustc::internal)]
33

4+
#[macro_use]
5+
mod util;
6+
47
mod arg;
58
mod phases;
69
mod setup;
7-
mod util;
810
mod version;
911

1012
use std::{env, iter};
@@ -73,9 +75,9 @@ fn main() {
7375
}
7476

7577
let Some(first) = args.next() else {
76-
show_error(format!(
78+
show_error!(
7779
"`cargo-miri` called without first argument; please only invoke this binary through `cargo miri`"
78-
))
80+
)
7981
};
8082
match first.as_str() {
8183
"miri" => phase_cargo_miri(args),

cargo-miri/src/phases.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
7777
// We cannot know which of those flags take arguments and which do not,
7878
// so we cannot detect subcommands later.
7979
let Some(subcommand) = args.next() else {
80-
show_error(format!("`cargo miri` needs to be called with a subcommand (`run`, `test`)"));
80+
show_error!("`cargo miri` needs to be called with a subcommand (`run`, `test`)");
8181
};
8282
let subcommand = match &*subcommand {
8383
"setup" => MiriCommand::Setup,
8484
"test" | "t" | "run" | "r" | "nextest" => MiriCommand::Forward(subcommand),
8585
_ =>
86-
show_error(format!(
86+
show_error!(
8787
"`cargo miri` supports the following subcommands: `run`, `test`, `nextest`, and `setup`."
88-
)),
88+
),
8989
};
9090
let verbose = num_arg_flag("-v");
9191

@@ -123,7 +123,7 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
123123
match arg {
124124
Ok(value) => {
125125
if target_dir.is_some() {
126-
show_error(format!("`--target-dir` is provided more than once"));
126+
show_error!("`--target-dir` is provided more than once");
127127
}
128128
target_dir = Some(value.into());
129129
}
@@ -456,16 +456,13 @@ pub fn phase_runner(mut binary_args: impl Iterator<Item = String>, phase: Runner
456456

457457
let binary = binary_args.next().unwrap();
458458
let file = File::open(&binary)
459-
.unwrap_or_else(|_| show_error(format!(
459+
.unwrap_or_else(|_| show_error!(
460460
"file {:?} not found or `cargo-miri` invoked incorrectly; please only invoke this binary through `cargo miri`", binary
461-
)));
461+
));
462462
let file = BufReader::new(file);
463463

464464
let info = serde_json::from_reader(file).unwrap_or_else(|_| {
465-
show_error(format!(
466-
"file {:?} contains outdated or invalid JSON; try `cargo clean`",
467-
binary
468-
))
465+
show_error!("file {:?} contains outdated or invalid JSON; try `cargo clean`", binary)
469466
});
470467
let info = match info {
471468
CrateRunInfo::RunWith(info) => info,
@@ -562,7 +559,7 @@ pub fn phase_rustdoc(mut args: impl Iterator<Item = String>) {
562559
// An existing --runtool flag indicates cargo is running in cross-target mode, which we don't support.
563560
// Note that this is only passed when cargo is run with the unstable -Zdoctest-xcompile flag;
564561
// otherwise, we won't be called as rustdoc at all.
565-
show_error(format!("cross-interpreting doctests is not currently supported by Miri."));
562+
show_error!("cross-interpreting doctests is not currently supported by Miri.");
566563
} else {
567564
cmd.arg(arg);
568565
}

cargo-miri/src/setup.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn setup(subcommand: &MiriCommand, host: &str, target: &str) {
7373
if xargo_version().map_or(true, |v| v < XARGO_MIN_VERSION) {
7474
if std::env::var_os("XARGO_CHECK").is_some() {
7575
// The user manually gave us a xargo binary; don't do anything automatically.
76-
show_error(format!("xargo is too old; please upgrade to the latest version"))
76+
show_error!("xargo is too old; please upgrade to the latest version")
7777
}
7878
let mut cmd = cargo();
7979
cmd.args(&["install", "xargo"]);
@@ -97,10 +97,10 @@ pub fn setup(subcommand: &MiriCommand, host: &str, target: &str) {
9797
.output()
9898
.expect("failed to determine sysroot");
9999
if !output.status.success() {
100-
show_error(format!(
100+
show_error!(
101101
"Failed to determine sysroot; Miri said:\n{}",
102102
String::from_utf8_lossy(&output.stderr).trim_end()
103-
));
103+
);
104104
}
105105
let sysroot = std::str::from_utf8(&output.stdout).unwrap();
106106
let sysroot = Path::new(sysroot.trim_end_matches('\n'));
@@ -121,14 +121,14 @@ pub fn setup(subcommand: &MiriCommand, host: &str, target: &str) {
121121
}
122122
};
123123
if !rust_src.exists() {
124-
show_error(format!("given Rust source directory `{}` does not exist.", rust_src.display()));
124+
show_error!("given Rust source directory `{}` does not exist.", rust_src.display());
125125
}
126126
if rust_src.file_name().and_then(OsStr::to_str) != Some("library") {
127-
show_error(format!(
127+
show_error!(
128128
"given Rust source directory `{}` does not seem to be the `library` subdirectory of \
129129
a Rust source checkout.",
130130
rust_src.display()
131-
));
131+
);
132132
}
133133

134134
// Next, we need our own libstd. Prepare a xargo project for that purpose.
@@ -226,11 +226,9 @@ path = "lib.rs"
226226
// Finally run it!
227227
if command.status().expect("failed to run xargo").success().not() {
228228
if only_setup {
229-
show_error(format!("failed to run xargo, see error details above"))
229+
show_error!("failed to run xargo, see error details above")
230230
} else {
231-
show_error(format!(
232-
"failed to run xargo; run `cargo miri setup` to see the error details"
233-
))
231+
show_error!("failed to run xargo; run `cargo miri setup` to see the error details")
234232
}
235233
}
236234

cargo-miri/src/util.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ use serde::{Deserialize, Serialize};
1414

1515
pub use crate::arg::*;
1616

17+
pub fn show_error(msg: &str) -> ! {
18+
eprintln!("fatal error: {msg}");
19+
std::process::exit(1)
20+
}
21+
22+
macro_rules! show_error {
23+
($($tt:tt)*) => { show_error(&format!($($tt)*)) };
24+
}
25+
1726
/// The information to run a crate with the given environment.
1827
#[derive(Clone, Serialize, Deserialize)]
1928
pub struct CrateRunEnv {
@@ -55,10 +64,10 @@ pub enum CrateRunInfo {
5564
impl CrateRunInfo {
5665
pub fn store(&self, filename: &Path) {
5766
let file = File::create(filename)
58-
.unwrap_or_else(|_| show_error(format!("cannot create `{}`", filename.display())));
67+
.unwrap_or_else(|_| show_error!("cannot create `{}`", filename.display()));
5968
let file = BufWriter::new(file);
6069
serde_json::ser::to_writer(file, self)
61-
.unwrap_or_else(|_| show_error(format!("cannot write to `{}`", filename.display())));
70+
.unwrap_or_else(|_| show_error!("cannot write to `{}`", filename.display()));
6271
}
6372
}
6473

@@ -70,11 +79,6 @@ pub enum MiriCommand {
7079
Forward(String),
7180
}
7281

73-
pub fn show_error(msg: String) -> ! {
74-
eprintln!("fatal error: {}", msg);
75-
std::process::exit(1)
76-
}
77-
7882
/// Escapes `s` in a way that is suitable for using it as a string literal in TOML syntax.
7983
pub fn escape_for_toml(s: &str) -> String {
8084
// We want to surround this string in quotes `"`. So we first escape all quotes,
@@ -187,15 +191,15 @@ pub fn ask_to_run(mut cmd: Command, ask: bool, text: &str) {
187191
match buf.trim().to_lowercase().as_ref() {
188192
// Proceed.
189193
"" | "y" | "yes" => {}
190-
"n" | "no" => show_error(format!("aborting as per your request")),
191-
a => show_error(format!("invalid answer `{}`", a)),
194+
"n" | "no" => show_error!("aborting as per your request"),
195+
a => show_error!("invalid answer `{}`", a),
192196
};
193197
} else {
194198
eprintln!("Running `{:?}` to {}.", cmd, text);
195199
}
196200

197201
if cmd.status().unwrap_or_else(|_| panic!("failed to execute {:?}", cmd)).success().not() {
198-
show_error(format!("failed to {}", text));
202+
show_error!("failed to {}", text);
199203
}
200204
}
201205

0 commit comments

Comments
 (0)