|
1 |
| -use rustc_mir::interpret::InterpErrorInfo; |
2 | 1 | use std::cell::RefCell;
|
3 | 2 |
|
| 3 | +use rustc_span::DUMMY_SP; |
| 4 | + |
4 | 5 | use crate::*;
|
5 | 6 |
|
| 7 | +/// Details of premature program termination. |
| 8 | +pub enum TerminationInfo { |
| 9 | + Exit(i64), |
| 10 | + Abort(Option<String>), |
| 11 | + UnsupportedInIsolation(String), |
| 12 | + ExperimentalUb { msg: String, url: String } |
| 13 | +} |
| 14 | + |
6 | 15 | /// Miri specific diagnostics
|
7 | 16 | pub enum NonHaltingDiagnostic {
|
8 | 17 | PoppedTrackedPointerTag(Item),
|
9 | 18 | CreatedAlloc(AllocId),
|
10 | 19 | }
|
11 | 20 |
|
12 | 21 | /// Emit a custom diagnostic without going through the miri-engine machinery
|
13 |
| -pub fn report_diagnostic<'tcx, 'mir>( |
| 22 | +pub fn report_error<'tcx, 'mir>( |
14 | 23 | ecx: &InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
|
15 | 24 | mut e: InterpErrorInfo<'tcx>,
|
16 | 25 | ) -> Option<i64> {
|
17 |
| - // Special treatment for some error kinds |
18 |
| - let msg = match e.kind { |
19 |
| - InterpError::MachineStop(ref info) => { |
| 26 | + use InterpError::*; |
| 27 | + |
| 28 | + e.print_backtrace(); |
| 29 | + let (title, msg, helps) = match e.kind { |
| 30 | + MachineStop(info) => { |
20 | 31 | let info = info.downcast_ref::<TerminationInfo>().expect("invalid MachineStop payload");
|
21 |
| - match info { |
22 |
| - TerminationInfo::Exit(code) => return Some(*code), |
23 |
| - TerminationInfo::Abort(None) => format!("the evaluated program aborted execution"), |
24 |
| - TerminationInfo::Abort(Some(msg)) => format!("the evaluated program aborted execution: {}", msg), |
25 |
| - } |
| 32 | + use TerminationInfo::*; |
| 33 | + let (title, msg) = match info { |
| 34 | + Exit(code) => return Some(*code), |
| 35 | + Abort(None) => |
| 36 | + ("abnormal termination", format!("the evaluated program aborted execution")), |
| 37 | + Abort(Some(msg)) => |
| 38 | + ("abnormal termination", format!("the evaluated program aborted execution: {}", msg)), |
| 39 | + UnsupportedInIsolation(msg) => |
| 40 | + ("unsupported operation", format!("{}", msg)), |
| 41 | + ExperimentalUb { msg, .. } => |
| 42 | + ("Undefined Behavior", format!("{}", msg)), |
| 43 | + }; |
| 44 | + let helps = match info { |
| 45 | + UnsupportedInIsolation(_) => |
| 46 | + vec![format!("pass the flag `-Zmiri-disable-isolation` to disable isolation")], |
| 47 | + ExperimentalUb { url, .. } => |
| 48 | + vec![ |
| 49 | + format!("this indicates a potential bug in the program: it performed an invalid operation, but the rules it violated are still experimental"), |
| 50 | + format!("see {} for further information", url), |
| 51 | + ], |
| 52 | + _ => vec![], |
| 53 | + }; |
| 54 | + (title, msg, helps) |
| 55 | + } |
| 56 | + _ => { |
| 57 | + let (title, msg) = match e.kind { |
| 58 | + Unsupported(_) => |
| 59 | + ("unsupported operation", e.to_string()), |
| 60 | + UndefinedBehavior(_) => |
| 61 | + ("Undefined Behavior", e.to_string()), |
| 62 | + ResourceExhaustion(_) => |
| 63 | + ("resource exhaustion", e.to_string()), |
| 64 | + _ => |
| 65 | + bug!("This error should be impossible in Miri: {}", e), |
| 66 | + }; |
| 67 | + let helps = match e.kind { |
| 68 | + Unsupported(UnsupportedOpInfo::NoMirFor(..)) => |
| 69 | + vec![format!("make sure to use a Miri sysroot, which you can prepare with `cargo miri setup`")], |
| 70 | + Unsupported(_) => |
| 71 | + vec![format!("this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support")], |
| 72 | + UndefinedBehavior(_) => |
| 73 | + vec![ |
| 74 | + format!("this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior"), |
| 75 | + format!("see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information"), |
| 76 | + ], |
| 77 | + _ => vec![], |
| 78 | + }; |
| 79 | + (title, msg, helps) |
26 | 80 | }
|
27 |
| - err_unsup!(NoMirFor(..)) => format!( |
28 |
| - "{}. Did you set `MIRI_SYSROOT` to a Miri-enabled sysroot? You can prepare one with `cargo miri setup`.", |
29 |
| - e |
30 |
| - ), |
31 |
| - InterpError::InvalidProgram(_) => bug!("This error should be impossible in Miri: {}", e), |
32 |
| - _ => e.to_string(), |
33 | 81 | };
|
34 |
| - e.print_backtrace(); |
35 |
| - report_msg(ecx, msg, true) |
| 82 | + report_msg(ecx, &format!("{}: {}", title, msg), msg, &helps, true) |
36 | 83 | }
|
37 | 84 |
|
38 | 85 | /// Report an error or note (depending on the `error` argument) at the current frame's current statement.
|
39 | 86 | /// Also emits a full stacktrace of the interpreter stack.
|
40 |
| -pub fn report_msg<'tcx, 'mir>( |
| 87 | +fn report_msg<'tcx, 'mir>( |
41 | 88 | ecx: &InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
|
42 |
| - msg: String, |
| 89 | + title: &str, |
| 90 | + span_msg: String, |
| 91 | + helps: &[String], |
43 | 92 | error: bool,
|
44 | 93 | ) -> Option<i64> {
|
45 |
| - if let Some(frame) = ecx.stack().last() { |
46 |
| - let span = frame.current_source_info().unwrap().span; |
47 |
| - |
48 |
| - let mut err = if error { |
49 |
| - let msg = format!("Miri evaluation error: {}", msg); |
50 |
| - ecx.tcx.sess.struct_span_err(span, msg.as_str()) |
| 94 | + let span = if let Some(frame) = ecx.stack().last() { |
| 95 | + frame.current_source_info().unwrap().span |
| 96 | + } else { |
| 97 | + DUMMY_SP |
| 98 | + }; |
| 99 | + let mut err = if error { |
| 100 | + ecx.tcx.sess.struct_span_err(span, title) |
| 101 | + } else { |
| 102 | + ecx.tcx.sess.diagnostic().span_note_diag(span, title) |
| 103 | + }; |
| 104 | + err.span_label(span, span_msg); |
| 105 | + for help in helps { |
| 106 | + err.help(help); |
| 107 | + } |
| 108 | + // Add backtrace |
| 109 | + let frames = ecx.generate_stacktrace(None); |
| 110 | + // We iterate with indices because we need to look at the next frame (the caller). |
| 111 | + for idx in 0..frames.len() { |
| 112 | + let frame_info = &frames[idx]; |
| 113 | + let call_site_is_local = frames |
| 114 | + .get(idx + 1) |
| 115 | + .map_or(false, |caller_info| caller_info.instance.def_id().is_local()); |
| 116 | + if call_site_is_local { |
| 117 | + err.span_note(frame_info.call_site, &frame_info.to_string()); |
51 | 118 | } else {
|
52 |
| - ecx.tcx.sess.diagnostic().span_note_diag(span, msg.as_str()) |
53 |
| - }; |
54 |
| - let frames = ecx.generate_stacktrace(None); |
55 |
| - err.span_label(span, msg); |
56 |
| - // We iterate with indices because we need to look at the next frame (the caller). |
57 |
| - for idx in 0..frames.len() { |
58 |
| - let frame_info = &frames[idx]; |
59 |
| - let call_site_is_local = frames |
60 |
| - .get(idx + 1) |
61 |
| - .map_or(false, |caller_info| caller_info.instance.def_id().is_local()); |
62 |
| - if call_site_is_local { |
63 |
| - err.span_note(frame_info.call_site, &frame_info.to_string()); |
64 |
| - } else { |
65 |
| - err.note(&frame_info.to_string()); |
66 |
| - } |
| 119 | + err.note(&frame_info.to_string()); |
67 | 120 | }
|
68 |
| - err.emit(); |
69 |
| - } else { |
70 |
| - ecx.tcx.sess.err(&msg); |
71 | 121 | }
|
72 | 122 |
|
| 123 | + err.emit(); |
| 124 | + |
73 | 125 | for (i, frame) in ecx.stack().iter().enumerate() {
|
74 | 126 | trace!("-------------------");
|
75 | 127 | trace!("Frame {}", i);
|
@@ -106,7 +158,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
|
106 | 158 | CreatedAlloc(AllocId(id)) =>
|
107 | 159 | format!("created allocation with id {}", id),
|
108 | 160 | };
|
109 |
| - report_msg(this, msg, false); |
| 161 | + report_msg(this, "tracking was triggered", msg, &[], false); |
110 | 162 | }
|
111 | 163 | });
|
112 | 164 | }
|
|
0 commit comments