Skip to content

Commit b233a0f

Browse files
committed
rearrange wording in errors, pass lines as one argument to error display
1 parent d0b60f0 commit b233a0f

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

src/diagnostics.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ pub fn report_error<'tcx, 'mir>(
134134
) -> Option<i64> {
135135
use InterpError::*;
136136

137-
let (title, labels, helps) = match &e.kind() {
137+
let mut msg = vec![];
138+
139+
let (title, helps) = match &e.kind() {
138140
MachineStop(info) => {
139141
let info = info.downcast_ref::<TerminationInfo>().expect("invalid MachineStop payload");
140142
use TerminationInfo::*;
@@ -146,7 +148,6 @@ pub fn report_error<'tcx, 'mir>(
146148
Deadlock => Some("deadlock"),
147149
MultipleSymbolDefinitions { .. } | SymbolShimClashing { .. } => None,
148150
};
149-
let mut labels = vec![];
150151
#[rustfmt::skip]
151152
let helps = match info {
152153
UnsupportedInIsolation(_) =>
@@ -155,9 +156,7 @@ pub fn report_error<'tcx, 'mir>(
155156
(None, format!("or pass `-Zmiri-isolation-error=warn to configure Miri to return an error code from isolated operations (if supported for that operation) and continue with a warning")),
156157
],
157158
ExperimentalUb { url, help, .. } => {
158-
if let Some(help) = help {
159-
labels.push(help.clone());
160-
}
159+
msg.extend(help.clone());
161160
vec![
162161
(None, format!("this indicates a potential bug in the program: it performed an invalid operation, but the rules it violated are still experimental")),
163162
(None, format!("see {} for further information", url))
@@ -172,7 +171,7 @@ pub fn report_error<'tcx, 'mir>(
172171
vec![(Some(*span), format!("the `{}` symbol is defined here", link_name))],
173172
_ => vec![],
174173
};
175-
(title, labels, helps)
174+
(title, helps)
176175
}
177176
_ => {
178177
#[rustfmt::skip]
@@ -210,20 +209,19 @@ pub fn report_error<'tcx, 'mir>(
210209
],
211210
_ => vec![],
212211
};
213-
(Some(title), vec![], helps)
212+
(Some(title), helps)
214213
}
215214
};
216215

217216
let stacktrace = ecx.generate_stacktrace();
218217
let (stacktrace, was_pruned) = prune_stacktrace(ecx, stacktrace);
219218
e.print_backtrace();
220-
let msg = e.to_string();
219+
msg.insert(0, e.to_string());
221220
report_msg(
222221
*ecx.tcx,
223222
DiagLevel::Error,
224-
&if let Some(title) = title { format!("{}: {}", title, msg) } else { msg.clone() },
223+
&if let Some(title) = title { format!("{}: {}", title, msg[0]) } else { msg[0].clone() },
225224
msg,
226-
labels,
227225
helps,
228226
&stacktrace,
229227
);
@@ -263,12 +261,14 @@ pub fn report_error<'tcx, 'mir>(
263261

264262
/// Report an error or note (depending on the `error` argument) with the given stacktrace.
265263
/// Also emits a full stacktrace of the interpreter stack.
264+
/// We want to present a multi-line span message for some errors. Diagnostics do not support this
265+
/// directly, so we pass the lines as a `Vec<String>` and display each line after the first with an
266+
/// additional `span_label` or `note` call.
266267
fn report_msg<'tcx>(
267268
tcx: TyCtxt<'tcx>,
268269
diag_level: DiagLevel,
269270
title: &str,
270-
span_msg: String,
271-
labels: Vec<String>,
271+
span_msg: Vec<String>,
272272
mut helps: Vec<(Option<SpanData>, String)>,
273273
stacktrace: &[FrameInfo<'tcx>],
274274
) {
@@ -279,18 +279,19 @@ fn report_msg<'tcx>(
279279
DiagLevel::Note => tcx.sess.diagnostic().span_note_diag(span, title),
280280
};
281281

282+
let first_line = span_msg.get(0).cloned().unwrap_or_default();
282283
// Show main message.
283284
if span != DUMMY_SP {
284-
err.span_label(span, span_msg);
285-
for label in labels {
286-
err.span_label(span, label);
285+
err.span_label(span, first_line);
286+
for line in span_msg.iter().skip(1) {
287+
err.span_label(span, line);
287288
}
288289
} else {
289290
// Make sure we show the message even when it is a dummy span.
290-
err.note(&span_msg);
291+
err.note(&first_line);
291292
err.note("(no span available)");
292-
for label in labels {
293-
err.note(&label);
293+
for line in span_msg.iter().skip(1) {
294+
err.note(&line);
294295
}
295296
}
296297

@@ -428,7 +429,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
428429
_ => ("tracking was triggered", DiagLevel::Note),
429430
};
430431

431-
report_msg(*this.tcx, diag_level, title, msg, vec![], vec![], &stacktrace);
432+
report_msg(*this.tcx, diag_level, title, vec![msg], vec![], &stacktrace);
432433
}
433434
});
434435
}

src/stacked_borrows.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ impl<'tcx> Stack {
341341

342342
/// Test if a memory `access` using pointer tagged `tag` is granted.
343343
/// If yes, return the index of the item that granted it.
344-
/// `range` refers the entire operation, and `offset` refers to the specific location in
345-
/// `range` that we are currently checking.
344+
/// `range` refers the entire operation, and `offset` refers to the specific offset into the
345+
/// allocation that we are currently checking.
346346
fn access(
347347
&mut self,
348348
access: AccessKind,
@@ -484,11 +484,11 @@ impl<'tcx> Stack {
484484
error_offset: Size,
485485
) -> InterpError<'static> {
486486
let action = format!(
487-
"trying to reborrow {:?}[{:#x}] with {:?} permission via tag {:?}",
487+
"trying to reborrow {:?} for {:?} permission at {}[{:#x}]",
488+
derived_from,
489+
new.perm,
488490
alloc_id,
489491
error_offset.bytes(),
490-
new.perm,
491-
derived_from,
492492
);
493493
err_sb_ub(
494494
format!("{}{}", action, self.error_cause(derived_from)),
@@ -506,11 +506,11 @@ impl<'tcx> Stack {
506506
error_offset: Size,
507507
) -> InterpError<'static> {
508508
let action = format!(
509-
"attempting a {} at {}[{:#x}] via tag {:?}",
509+
"attempting a {} using {:?} at {}[{:#x}]",
510510
access,
511+
tag,
511512
alloc_id,
512513
error_offset.bytes(),
513-
tag,
514514
);
515515
err_sb_ub(
516516
format!("{}{}", action, self.error_cause(tag)),
@@ -534,9 +534,9 @@ impl<'tcx> Stack {
534534

535535
fn error_cause(&self, tag: SbTag) -> &'static str {
536536
if self.borrows.iter().any(|item| item.tag == tag && item.perm != Permission::Disabled) {
537-
", but that tag only grants SharedReadOnly permission for this memory"
537+
", but that tag only grants SharedReadOnly permission for this location"
538538
} else {
539-
", but that tag does not exist in the borrow stack for this memory"
539+
", but that tag does not exist in the borrow stack for this location"
540540
}
541541
}
542542
}

0 commit comments

Comments
 (0)