Skip to content

Commit e7089a9

Browse files
committed
rustbuild: refactor how the wrapper deals with exit codes
1 parent 3dcab29 commit e7089a9

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

src/bootstrap/bin/rustc.rs

+24-26
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
//! never get replaced.
1717
1818
use std::env;
19-
use std::io;
2019
use std::path::PathBuf;
2120
use std::process::Command;
2221
use std::str::FromStr;
@@ -147,22 +146,15 @@ fn main() {
147146
eprintln!("libdir: {:?}", libdir);
148147
}
149148

150-
if let Some(mut on_fail) = on_fail {
151-
let e = match cmd.status() {
152-
Ok(s) => if s.success() { std::process::exit(0) } else { format!("Ok({})", s) },
153-
Err(e) => format!("Err({})", e),
154-
};
155-
println!("\nDid not run successfully: {}\n{:?}\n-------------", e, cmd);
156-
status_code(&mut on_fail).expect("could not run the backup command");
157-
std::process::exit(1);
158-
}
149+
let start = Instant::now();
150+
let status = {
151+
let errmsg = format!("\nFailed to run:\n{:?}\n-------------", cmd);
152+
cmd.status().expect(&errmsg)
153+
};
159154

160155
if env::var_os("RUSTC_PRINT_STEP_TIMINGS").is_some() {
161156
if let Some(crate_name) = crate_name {
162-
let start = Instant::now();
163-
let status = cmd.status().unwrap_or_else(|_| panic!("\n\n failed to run {:?}", cmd));
164157
let dur = start.elapsed();
165-
166158
let is_test = args.iter().any(|a| a == "--test");
167159
eprintln!(
168160
"[RUSTC-TIMING] {} test:{} {}.{:03}",
@@ -171,21 +163,27 @@ fn main() {
171163
dur.as_secs(),
172164
dur.subsec_millis()
173165
);
174-
175-
match status.code() {
176-
Some(i) => std::process::exit(i),
177-
None => {
178-
eprintln!("rustc exited with {}", status);
179-
std::process::exit(0xfe);
180-
}
181-
}
182166
}
183167
}
184168

185-
let code = status_code(&mut cmd).unwrap_or_else(|_| panic!("\n\n failed to run {:?}", cmd));
186-
std::process::exit(code);
187-
}
169+
if status.success() {
170+
std::process::exit(0);
171+
// note: everything below here is unreachable. do not put code that
172+
// should run on success, after this block.
173+
}
174+
println!("\nDid not run successfully: {}\n{:?}\n-------------", status, cmd);
175+
176+
if let Some(mut on_fail) = on_fail {
177+
on_fail.status().expect("Could not run the on_fail command");
178+
}
188179

189-
fn status_code(cmd: &mut Command) -> io::Result<i32> {
190-
cmd.status().map(|status| status.code().unwrap())
180+
// Preserve the exit code. In case of signal, exit with 0xfe since it's
181+
// awkward to preserve this status in a cross-platform way.
182+
match status.code() {
183+
Some(i) => std::process::exit(i),
184+
None => {
185+
eprintln!("rustc exited with {}", status);
186+
std::process::exit(0xfe);
187+
}
188+
}
191189
}

0 commit comments

Comments
 (0)