improve debug message by eagerly translating

This commit is contained in:
Deadbeef 2023-05-29 03:32:38 +00:00
parent 4f83717cf7
commit f964b46451
4 changed files with 31 additions and 16 deletions

View File

@ -1,3 +1,5 @@
use std::fmt;
use rustc_errors::{
DiagnosticArgValue, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Handler,
IntoDiagnostic,
@ -8,7 +10,7 @@ use rustc_middle::mir::interpret::{
CheckInAllocMsg, ExpectedKind, InterpError, InvalidMetaKind, InvalidProgramInfo, PointerKind,
ResourceExhaustionInfo, UndefinedBehaviorInfo, UnsupportedOpInfo, ValidationErrorInfo,
};
use rustc_middle::ty::Ty;
use rustc_middle::ty::{self, Ty};
use rustc_span::Span;
use rustc_target::abi::call::AdjustForForeignAbiError;
use rustc_target::abi::{Size, WrappingRange};
@ -425,6 +427,24 @@ pub struct UndefinedBehavior {
pub raw_bytes: RawBytesNote,
}
pub struct DebugExt<T>(T);
impl<T: ReportErrorExt> fmt::Debug for DebugExt<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = ty::tls::with(|tcx| {
let mut builder = tcx.sess.struct_allow("");
let handler = &tcx.sess.parse_sess.span_diagnostic;
let message = self.0.diagnostic_message();
self.0.add_args(handler, &mut builder);
let s = handler.eagerly_translate_to_string(message, builder.args());
builder.cancel();
s
});
f.write_str(&s)
}
}
pub trait ReportErrorExt {
/// Returns the diagnostic message for this error.
fn diagnostic_message(&self) -> DiagnosticMessage;
@ -433,6 +453,13 @@ pub trait ReportErrorExt {
handler: &Handler,
builder: &mut DiagnosticBuilder<'_, G>,
);
fn debug(self) -> DebugExt<Self>
where
Self: Sized,
{
DebugExt(self)
}
}
fn bad_pointer_message(msg: CheckInAllocMsg, handler: &Handler) -> String {

View File

@ -469,6 +469,7 @@ impl dyn MachineStopType {
}
}
#[derive(Debug)]
pub enum InterpError<'tcx> {
/// The program caused undefined behavior.
UndefinedBehavior(UndefinedBehaviorInfo<'tcx>),
@ -487,19 +488,6 @@ pub enum InterpError<'tcx> {
pub type InterpResult<'tcx, T = ()> = Result<T, InterpErrorInfo<'tcx>>;
impl fmt::Debug for InterpError<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use InterpError::*;
match self {
Unsupported(msg) => msg.fmt(f),
InvalidProgram(msg) => msg.fmt(f),
UndefinedBehavior(msg) => msg.fmt(f),
ResourceExhaustion(msg) => msg.fmt(f),
MachineStop(msg) => msg.fmt(f),
}
}
}
impl InterpError<'_> {
/// Some errors do string formatting even if the error is never printed.
/// To avoid performance issues, there are places where we want to be sure to never raise these formatting errors,

View File

@ -378,7 +378,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
op
}
Err(e) => {
trace!("get_const failed: {e:?}");
trace!("get_const failed: {:?}", e.debug());
return None;
}
};

View File

@ -232,7 +232,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
op
}
Err(e) => {
trace!("get_const failed: {e:?}");
trace!("get_const failed: {:?}", e.debug());
return None;
}
};