Skip to content

Commit

Permalink
improve debug message by eagerly translating
Browse files Browse the repository at this point in the history
  • Loading branch information
fee1-dead committed May 29, 2023
1 parent 043989d commit 74a6803
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
29 changes: 28 additions & 1 deletion compiler/rustc_const_eval/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt;

use rustc_errors::{
DiagnosticArgValue, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Handler,
IntoDiagnostic,
Expand All @@ -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};
Expand Down Expand Up @@ -407,6 +409,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;
Expand All @@ -415,6 +435,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 {
Expand Down
14 changes: 1 addition & 13 deletions compiler/rustc_middle/src/mir/interpret/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ impl dyn MachineStopType {
}
}

#[derive(Debug)]
pub enum InterpError<'tcx> {
/// The program caused undefined behavior.
UndefinedBehavior(UndefinedBehaviorInfo<'tcx>),
Expand All @@ -490,19 +491,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,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/const_prop_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
Expand Down

0 comments on commit 74a6803

Please sign in to comment.