Skip to content

Commit 0694435

Browse files
committed
implement exit
implement exit code via new error kind
1 parent 788616d commit 0694435

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
130dc3e7dac132cf30272ccf4541b512828e2108
1+
9224be5fa39f6170f6e046342976efee5453a1ff

src/fn_call.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,15 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
6969
let link_name = link_name.get().trim_end_matches("$UNIX2003");
7070
let tcx = &{this.tcx.tcx};
7171

72-
// First: functions that could diverge.
72+
// First: functions that diverge.
7373
match link_name {
7474
"__rust_start_panic" | "panic_impl" => {
7575
return err!(MachineError("the evaluated program panicked".to_string()));
7676
}
77+
"exit" => {
78+
let code = this.read_scalar(args[0])?.to_i32()?;
79+
return err!(Exit(code));
80+
}
7781
_ => if dest.is_none() {
7882
return err!(Unimplemented(
7983
format!("can't call diverging foreign function: {}", link_name),

src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,13 @@ pub fn eval_main<'a, 'tcx: 'a>(
242242
}
243243
}
244244
Err(mut e) => {
245+
// Special treatment for some error kinds
246+
let msg = match e.kind {
247+
InterpError::Exit(code) => std::process::exit(code),
248+
InterpError::NoMirFor(..) =>
249+
format!("{}. Did you set `MIRI_SYSROOT` to a Miri-enabled sysroot? You can prepare one with `cargo miri setup`.", e),
250+
_ => e.to_string()
251+
};
245252
e.print_backtrace();
246253
if let Some(frame) = ecx.stack().last() {
247254
let block = &frame.mir.basic_blocks()[frame.block];
@@ -251,11 +258,10 @@ pub fn eval_main<'a, 'tcx: 'a>(
251258
block.terminator().source_info.span
252259
};
253260

254-
let e = e.to_string();
255-
let msg = format!("constant evaluation error: {}", e);
261+
let msg = format!("Miri evaluation error: {}", msg);
256262
let mut err = struct_error(ecx.tcx.tcx.at(span), msg.as_str());
257263
let frames = ecx.generate_stacktrace(None);
258-
err.span_label(span, e);
264+
err.span_label(span, msg);
259265
// We iterate with indices because we need to look at the next frame (the caller).
260266
for idx in 0..frames.len() {
261267
let frame_info = &frames[idx];
@@ -269,7 +275,7 @@ pub fn eval_main<'a, 'tcx: 'a>(
269275
}
270276
err.emit();
271277
} else {
272-
ecx.tcx.sess.err(&e.to_string());
278+
ecx.tcx.sess.err(&msg);
273279
}
274280

275281
for (i, frame) in ecx.stack().iter().enumerate() {

tests/run-pass/exit.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
std::process::exit(0)
3+
}

0 commit comments

Comments
 (0)