improve debug message by eagerly translating
This commit is contained in:
parent
4f83717cf7
commit
f964b46451
@ -1,3 +1,5 @@
|
|||||||
|
use std::fmt;
|
||||||
|
|
||||||
use rustc_errors::{
|
use rustc_errors::{
|
||||||
DiagnosticArgValue, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Handler,
|
DiagnosticArgValue, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Handler,
|
||||||
IntoDiagnostic,
|
IntoDiagnostic,
|
||||||
@ -8,7 +10,7 @@ use rustc_middle::mir::interpret::{
|
|||||||
CheckInAllocMsg, ExpectedKind, InterpError, InvalidMetaKind, InvalidProgramInfo, PointerKind,
|
CheckInAllocMsg, ExpectedKind, InterpError, InvalidMetaKind, InvalidProgramInfo, PointerKind,
|
||||||
ResourceExhaustionInfo, UndefinedBehaviorInfo, UnsupportedOpInfo, ValidationErrorInfo,
|
ResourceExhaustionInfo, UndefinedBehaviorInfo, UnsupportedOpInfo, ValidationErrorInfo,
|
||||||
};
|
};
|
||||||
use rustc_middle::ty::Ty;
|
use rustc_middle::ty::{self, Ty};
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
use rustc_target::abi::call::AdjustForForeignAbiError;
|
use rustc_target::abi::call::AdjustForForeignAbiError;
|
||||||
use rustc_target::abi::{Size, WrappingRange};
|
use rustc_target::abi::{Size, WrappingRange};
|
||||||
@ -425,6 +427,24 @@ pub struct UndefinedBehavior {
|
|||||||
pub raw_bytes: RawBytesNote,
|
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 {
|
pub trait ReportErrorExt {
|
||||||
/// Returns the diagnostic message for this error.
|
/// Returns the diagnostic message for this error.
|
||||||
fn diagnostic_message(&self) -> DiagnosticMessage;
|
fn diagnostic_message(&self) -> DiagnosticMessage;
|
||||||
@ -433,6 +453,13 @@ pub trait ReportErrorExt {
|
|||||||
handler: &Handler,
|
handler: &Handler,
|
||||||
builder: &mut DiagnosticBuilder<'_, G>,
|
builder: &mut DiagnosticBuilder<'_, G>,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
fn debug(self) -> DebugExt<Self>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
DebugExt(self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bad_pointer_message(msg: CheckInAllocMsg, handler: &Handler) -> String {
|
fn bad_pointer_message(msg: CheckInAllocMsg, handler: &Handler) -> String {
|
||||||
|
@ -469,6 +469,7 @@ impl dyn MachineStopType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum InterpError<'tcx> {
|
pub enum InterpError<'tcx> {
|
||||||
/// The program caused undefined behavior.
|
/// The program caused undefined behavior.
|
||||||
UndefinedBehavior(UndefinedBehaviorInfo<'tcx>),
|
UndefinedBehavior(UndefinedBehaviorInfo<'tcx>),
|
||||||
@ -487,19 +488,6 @@ pub enum InterpError<'tcx> {
|
|||||||
|
|
||||||
pub type InterpResult<'tcx, T = ()> = Result<T, InterpErrorInfo<'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<'_> {
|
impl InterpError<'_> {
|
||||||
/// Some errors do string formatting even if the error is never printed.
|
/// 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,
|
/// To avoid performance issues, there are places where we want to be sure to never raise these formatting errors,
|
||||||
|
@ -378,7 +378,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
|||||||
op
|
op
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
trace!("get_const failed: {e:?}");
|
trace!("get_const failed: {:?}", e.debug());
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -232,7 +232,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
|||||||
op
|
op
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
trace!("get_const failed: {e:?}");
|
trace!("get_const failed: {:?}", e.debug());
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user