1
Fork 0

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 {