Skip to content

Commit 74a6803

Browse files
committed
improve debug message by eagerly translating
1 parent 043989d commit 74a6803

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};
@@ -407,6 +409,24 @@ pub struct UndefinedBehavior {
407409
pub raw_bytes: RawBytesNote,
408410
}
409411

412+
pub struct DebugExt<T>(T);
413+
414+
impl<T: ReportErrorExt> fmt::Debug for DebugExt<T> {
415+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
416+
let s = ty::tls::with(|tcx| {
417+
let mut builder = tcx.sess.struct_allow("");
418+
let handler = &tcx.sess.parse_sess.span_diagnostic;
419+
let message = self.0.diagnostic_message();
420+
self.0.add_args(handler, &mut builder);
421+
let s = handler.eagerly_translate_to_string(message, builder.args());
422+
builder.cancel();
423+
s
424+
});
425+
426+
f.write_str(&s)
427+
}
428+
}
429+
410430
pub trait ReportErrorExt {
411431
/// Returns the diagnostic message for this error.
412432
fn diagnostic_message(&self) -> DiagnosticMessage;
@@ -415,6 +435,13 @@ pub trait ReportErrorExt {
415435
handler: &Handler,
416436
builder: &mut DiagnosticBuilder<'_, G>,
417437
);
438+
439+
fn debug(self) -> DebugExt<Self>
440+
where
441+
Self: Sized,
442+
{
443+
DebugExt(self)
444+
}
418445
}
419446

420447
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
@@ -472,6 +472,7 @@ impl dyn MachineStopType {
472472
}
473473
}
474474

475+
#[derive(Debug)]
475476
pub enum InterpError<'tcx> {
476477
/// The program caused undefined behavior.
477478
UndefinedBehavior(UndefinedBehaviorInfo<'tcx>),
@@ -490,19 +491,6 @@ pub enum InterpError<'tcx> {
490491

491492
pub type InterpResult<'tcx, T = ()> = Result<T, InterpErrorInfo<'tcx>>;
492493

493-
impl fmt::Debug for InterpError<'_> {
494-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
495-
use InterpError::*;
496-
match self {
497-
Unsupported(msg) => msg.fmt(f),
498-
InvalidProgram(msg) => msg.fmt(f),
499-
UndefinedBehavior(msg) => msg.fmt(f),
500-
ResourceExhaustion(msg) => msg.fmt(f),
501-
MachineStop(msg) => msg.fmt(f),
502-
}
503-
}
504-
}
505-
506494
impl InterpError<'_> {
507495
/// Some errors do string formatting even if the error is never printed.
508496
/// 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)