Skip to content

Commit c3c3e48

Browse files
committed
Auto merge of #115370 - matthiaskrgr:rollup-l0e1zuj, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #113565 (Make SIGSEGV handler emit nicer backtraces) - #114704 (parser: not insert dummy field in struct) - #115272 (miri/diagnostics: don't forget to print_backtrace when ICEing on unexpected errors) - #115313 (Make `get_return_block()` return `Some` only for HIR nodes in body) - #115347 (suggest removing `impl` in generic trait bound position) - #115355 (new solver: handle edge case of a recursion limit of 0) - #115363 (Don't suggest adding parentheses to call an inaccessible method.) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 388718b + 9ee73ec commit c3c3e48

File tree

1 file changed

+27
-38
lines changed

1 file changed

+27
-38
lines changed

src/diagnostics.rs

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use std::fmt;
1+
use std::fmt::{self, Write};
22
use std::num::NonZeroU64;
33

44
use log::trace;
55

6-
use rustc_const_eval::ReportErrorExt;
76
use rustc_errors::DiagnosticMessage;
87
use rustc_span::{source_map::DUMMY_SP, SpanData, Symbol};
98
use rustc_target::abi::{Align, Size};
@@ -271,10 +270,13 @@ pub fn report_error<'tcx, 'mir>(
271270
};
272271
(title, helps)
273272
} else {
274-
#[rustfmt::skip]
275273
let title = match e.kind() {
276-
UndefinedBehavior(UndefinedBehaviorInfo::ValidationError(e)) if matches!(e.kind, ValidationErrorKind::PointerAsInt { .. } | ValidationErrorKind::PartialPointer) =>
277-
bug!("This validation error should be impossible in Miri: {:?}", e.kind),
274+
UndefinedBehavior(UndefinedBehaviorInfo::ValidationError(validation_err))
275+
if matches!(validation_err.kind, ValidationErrorKind::PointerAsInt { .. } | ValidationErrorKind::PartialPointer) =>
276+
{
277+
ecx.handle_ice(); // print interpreter backtrace
278+
bug!("This validation error should be impossible in Miri: {}", ecx.format_error(e));
279+
}
278280
UndefinedBehavior(_) =>
279281
"Undefined Behavior",
280282
ResourceExhaustion(_) =>
@@ -290,8 +292,10 @@ pub fn report_error<'tcx, 'mir>(
290292
InvalidProgramInfo::Layout(..)
291293
) =>
292294
"post-monomorphization error",
293-
kind =>
294-
bug!("This error should be impossible in Miri: {kind:?}"),
295+
_ => {
296+
ecx.handle_ice(); // print interpreter backtrace
297+
bug!("This error should be impossible in Miri: {}", ecx.format_error(e));
298+
}
295299
};
296300
#[rustfmt::skip]
297301
let helps = match e.kind() {
@@ -333,30 +337,22 @@ pub fn report_error<'tcx, 'mir>(
333337

334338
let stacktrace = ecx.generate_stacktrace();
335339
let (stacktrace, was_pruned) = prune_stacktrace(stacktrace, &ecx.machine);
336-
let (e, backtrace) = e.into_parts();
337-
backtrace.print_backtrace();
338-
339-
// We want to dump the allocation if this is `InvalidUninitBytes`. Since `add_args` consumes
340-
// the `InterpError`, we extract the variables it before that.
341-
let extra = match e {
342-
UndefinedBehavior(UndefinedBehaviorInfo::InvalidUninitBytes(Some((alloc_id, access)))) =>
343-
Some((alloc_id, access)),
344-
_ => None,
345-
};
346340

347-
// FIXME(fee1-dead), HACK: we want to use the error as title therefore we can just extract the
348-
// label and arguments from the InterpError.
349-
let e = {
350-
let handler = &ecx.tcx.sess.parse_sess.span_diagnostic;
351-
let mut diag = ecx.tcx.sess.struct_allow("");
352-
let msg = e.diagnostic_message();
353-
e.add_args(handler, &mut diag);
354-
let s = handler.eagerly_translate_to_string(msg, diag.args());
355-
diag.cancel();
356-
s
357-
};
341+
// We want to dump the allocation if this is `InvalidUninitBytes`. Since `format_error` consumes `e`, we compute the outut early.
342+
let mut extra = String::new();
343+
match e.kind() {
344+
UndefinedBehavior(UndefinedBehaviorInfo::InvalidUninitBytes(Some((alloc_id, access)))) => {
345+
writeln!(
346+
extra,
347+
"Uninitialized memory occurred at {alloc_id:?}{range:?}, in this allocation:",
348+
range = access.bad,
349+
).unwrap();
350+
writeln!(extra, "{:?}", ecx.dump_alloc(*alloc_id)).unwrap();
351+
}
352+
_ => {}
353+
}
358354

359-
msg.insert(0, e);
355+
msg.insert(0, ecx.format_error(e));
360356

361357
report_msg(
362358
DiagLevel::Error,
@@ -375,6 +371,8 @@ pub fn report_error<'tcx, 'mir>(
375371
);
376372
}
377373

374+
eprint!("{extra}"); // newlines are already in the string
375+
378376
// Debug-dump all locals.
379377
for (i, frame) in ecx.active_thread_stack().iter().enumerate() {
380378
trace!("-------------------");
@@ -385,15 +383,6 @@ pub fn report_error<'tcx, 'mir>(
385383
}
386384
}
387385

388-
// Extra output to help debug specific issues.
389-
if let Some((alloc_id, access)) = extra {
390-
eprintln!(
391-
"Uninitialized memory occurred at {alloc_id:?}{range:?}, in this allocation:",
392-
range = access.bad,
393-
);
394-
eprintln!("{:?}", ecx.dump_alloc(alloc_id));
395-
}
396-
397386
None
398387
}
399388

0 commit comments

Comments
 (0)