Skip to content

Commit 4de031b

Browse files
committed
Move error reporting to its own module
1 parent 2673ba9 commit 4de031b

File tree

3 files changed

+64
-60
lines changed

3 files changed

+64
-60
lines changed

src/diagnostics.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use rustc_mir::interpret::InterpErrorInfo;
2+
3+
use crate::*;
4+
5+
pub fn report_err<'tcx, 'mir>(
6+
ecx: &InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
7+
mut e: InterpErrorInfo<'tcx>,
8+
) -> Option<i64> {
9+
// Special treatment for some error kinds
10+
let msg = match e.kind {
11+
InterpError::MachineStop(ref info) => {
12+
let info = info.downcast_ref::<TerminationInfo>().expect("invalid MachineStop payload");
13+
match info {
14+
TerminationInfo::Exit(code) => return Some(*code),
15+
TerminationInfo::PoppedTrackedPointerTag(item) =>
16+
format!("popped tracked tag for item {:?}", item),
17+
TerminationInfo::Abort => format!("the evaluated program aborted execution"),
18+
}
19+
}
20+
err_unsup!(NoMirFor(..)) => format!(
21+
"{}. Did you set `MIRI_SYSROOT` to a Miri-enabled sysroot? You can prepare one with `cargo miri setup`.",
22+
e
23+
),
24+
InterpError::InvalidProgram(_) => bug!("This error should be impossible in Miri: {}", e),
25+
_ => e.to_string(),
26+
};
27+
e.print_backtrace();
28+
if let Some(frame) = ecx.stack().last() {
29+
let span = frame.current_source_info().unwrap().span;
30+
31+
let msg = format!("Miri evaluation error: {}", msg);
32+
let mut err = ecx.tcx.sess.struct_span_err(span, msg.as_str());
33+
let frames = ecx.generate_stacktrace(None);
34+
err.span_label(span, msg);
35+
// We iterate with indices because we need to look at the next frame (the caller).
36+
for idx in 0..frames.len() {
37+
let frame_info = &frames[idx];
38+
let call_site_is_local = frames
39+
.get(idx + 1)
40+
.map_or(false, |caller_info| caller_info.instance.def_id().is_local());
41+
if call_site_is_local {
42+
err.span_note(frame_info.call_site, &frame_info.to_string());
43+
} else {
44+
err.note(&frame_info.to_string());
45+
}
46+
}
47+
err.emit();
48+
} else {
49+
ecx.tcx.sess.err(&msg);
50+
}
51+
52+
for (i, frame) in ecx.stack().iter().enumerate() {
53+
trace!("-------------------");
54+
trace!("Frame {}", i);
55+
trace!(" return: {:?}", frame.return_place.map(|p| *p));
56+
for (i, local) in frame.locals.iter().enumerate() {
57+
trace!(" local {}: {:?}", i, local.value);
58+
}
59+
}
60+
// Let the reported error determine the return code.
61+
return None;
62+
}

src/eval.rs

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rand::SeedableRng;
88
use rustc_hir::def_id::DefId;
99
use rustc::ty::layout::{LayoutOf, Size};
1010
use rustc::ty::{self, TyCtxt};
11-
use rustc_mir::interpret::InterpErrorInfo;
1211

1312
use crate::*;
1413

@@ -209,62 +208,3 @@ pub fn eval_main<'tcx>(tcx: TyCtxt<'tcx>, main_id: DefId, config: MiriConfig) ->
209208
Err(e) => report_err(&ecx, e),
210209
}
211210
}
212-
213-
fn report_err<'tcx, 'mir>(
214-
ecx: &InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
215-
mut e: InterpErrorInfo<'tcx>,
216-
) -> Option<i64> {
217-
// Special treatment for some error kinds
218-
let msg = match e.kind {
219-
InterpError::MachineStop(ref info) => {
220-
let info = info.downcast_ref::<TerminationInfo>().expect("invalid MachineStop payload");
221-
match info {
222-
TerminationInfo::Exit(code) => return Some(*code),
223-
TerminationInfo::PoppedTrackedPointerTag(item) =>
224-
format!("popped tracked tag for item {:?}", item),
225-
TerminationInfo::Abort => format!("the evaluated program aborted execution"),
226-
}
227-
}
228-
err_unsup!(NoMirFor(..)) => format!(
229-
"{}. Did you set `MIRI_SYSROOT` to a Miri-enabled sysroot? You can prepare one with `cargo miri setup`.",
230-
e
231-
),
232-
InterpError::InvalidProgram(_) => bug!("This error should be impossible in Miri: {}", e),
233-
_ => e.to_string(),
234-
};
235-
e.print_backtrace();
236-
if let Some(frame) = ecx.stack().last() {
237-
let span = frame.current_source_info().unwrap().span;
238-
239-
let msg = format!("Miri evaluation error: {}", msg);
240-
let mut err = ecx.tcx.sess.struct_span_err(span, msg.as_str());
241-
let frames = ecx.generate_stacktrace(None);
242-
err.span_label(span, msg);
243-
// We iterate with indices because we need to look at the next frame (the caller).
244-
for idx in 0..frames.len() {
245-
let frame_info = &frames[idx];
246-
let call_site_is_local = frames
247-
.get(idx + 1)
248-
.map_or(false, |caller_info| caller_info.instance.def_id().is_local());
249-
if call_site_is_local {
250-
err.span_note(frame_info.call_site, &frame_info.to_string());
251-
} else {
252-
err.note(&frame_info.to_string());
253-
}
254-
}
255-
err.emit();
256-
} else {
257-
ecx.tcx.sess.err(&msg);
258-
}
259-
260-
for (i, frame) in ecx.stack().iter().enumerate() {
261-
trace!("-------------------");
262-
trace!("Frame {}", i);
263-
trace!(" return: {:?}", frame.return_place.map(|p| *p));
264-
for (i, local) in frame.locals.iter().enumerate() {
265-
trace!(" local {}: {:?}", i, local.value);
266-
}
267-
}
268-
// Let the reported error determine the return code.
269-
return None;
270-
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extern crate rustc_data_structures;
1616
extern crate rustc_mir;
1717
extern crate rustc_target;
1818

19+
mod diagnostics;
1920
mod eval;
2021
mod helpers;
2122
mod intptrcast;
@@ -41,6 +42,7 @@ pub use crate::shims::time::EvalContextExt as TimeEvalContextExt;
4142
pub use crate::shims::tls::{EvalContextExt as TlsEvalContextExt, TlsData};
4243
pub use crate::shims::EvalContextExt as ShimsEvalContextExt;
4344

45+
pub use crate::diagnostics::report_err;
4446
pub use crate::eval::{create_ecx, eval_main, MiriConfig, TerminationInfo};
4547
pub use crate::helpers::EvalContextExt as HelpersEvalContextExt;
4648
pub use crate::machine::{

0 commit comments

Comments
 (0)