Skip to content

Commit f964b46

Browse files
committed
improve debug message by eagerly translating
1 parent 4f83717 commit f964b46

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

compiler/rustc_const_eval/src/errors.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt;
2+
13
use rustc_errors::{
24
DiagnosticArgValue, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Handler,
35
IntoDiagnostic,
@@ -8,7 +10,7 @@ use rustc_middle::mir::interpret::{
810
CheckInAllocMsg, ExpectedKind, InterpError, InvalidMetaKind, InvalidProgramInfo, PointerKind,
911
ResourceExhaustionInfo, UndefinedBehaviorInfo, UnsupportedOpInfo, ValidationErrorInfo,
1012
};
11-
use rustc_middle::ty::Ty;
13+
use rustc_middle::ty::{self, Ty};
1214
use rustc_span::Span;
1315
use rustc_target::abi::call::AdjustForForeignAbiError;
1416
use rustc_target::abi::{Size, WrappingRange};
@@ -425,6 +427,24 @@ pub struct UndefinedBehavior {
425427
pub raw_bytes: RawBytesNote,
426428
}
427429

430+
pub struct DebugExt<T>(T);
431+
432+
impl<T: ReportErrorExt> fmt::Debug for DebugExt<T> {
433+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
434+
let s = ty::tls::with(|tcx| {
435+
let mut builder = tcx.sess.struct_allow("");
436+
let handler = &tcx.sess.parse_sess.span_diagnostic;
437+
let message = self.0.diagnostic_message();
438+
self.0.add_args(handler, &mut builder);
439+
let s = handler.eagerly_translate_to_string(message, builder.args());
440+
builder.cancel();
441+
s
442+
});
443+
444+
f.write_str(&s)
445+
}
446+
}
447+
428448
pub trait ReportErrorExt {
429449
/// Returns the diagnostic message for this error.
430450
fn diagnostic_message(&self) -> DiagnosticMessage;
@@ -433,6 +453,13 @@ pub trait ReportErrorExt {
433453
handler: &Handler,
434454
builder: &mut DiagnosticBuilder<'_, G>,
435455
);
456+
457+
fn debug(self) -> DebugExt<Self>
458+
where
459+
Self: Sized,
460+
{
461+
DebugExt(self)
462+
}
436463
}
437464

438465
fn bad_pointer_message(msg: CheckInAllocMsg, handler: &Handler) -> String {

compiler/rustc_middle/src/mir/interpret/error.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ impl dyn MachineStopType {
469469
}
470470
}
471471

472+
#[derive(Debug)]
472473
pub enum InterpError<'tcx> {
473474
/// The program caused undefined behavior.
474475
UndefinedBehavior(UndefinedBehaviorInfo<'tcx>),
@@ -487,19 +488,6 @@ pub enum InterpError<'tcx> {
487488

488489
pub type InterpResult<'tcx, T = ()> = Result<T, InterpErrorInfo<'tcx>>;
489490

490-
impl fmt::Debug for InterpError<'_> {
491-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
492-
use InterpError::*;
493-
match self {
494-
Unsupported(msg) => msg.fmt(f),
495-
InvalidProgram(msg) => msg.fmt(f),
496-
UndefinedBehavior(msg) => msg.fmt(f),
497-
ResourceExhaustion(msg) => msg.fmt(f),
498-
MachineStop(msg) => msg.fmt(f),
499-
}
500-
}
501-
}
502-
503491
impl InterpError<'_> {
504492
/// Some errors do string formatting even if the error is never printed.
505493
/// To avoid performance issues, there are places where we want to be sure to never raise these formatting errors,

compiler/rustc_mir_transform/src/const_prop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
378378
op
379379
}
380380
Err(e) => {
381-
trace!("get_const failed: {e:?}");
381+
trace!("get_const failed: {:?}", e.debug());
382382
return None;
383383
}
384384
};

compiler/rustc_mir_transform/src/const_prop_lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
232232
op
233233
}
234234
Err(e) => {
235-
trace!("get_const failed: {e:?}");
235+
trace!("get_const failed: {:?}", e.debug());
236236
return None;
237237
}
238238
};

0 commit comments

Comments
 (0)