Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use translatable diagnostics in rustc_const_eval #111677

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
improve debug message by eagerly translating
  • Loading branch information
fee1-dead committed Jun 1, 2023
commit f964b46451f3a323f312a7035e25180060044335
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 @@ -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;
Expand All @@ -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 {
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 @@ -469,6 +469,7 @@ impl dyn MachineStopType {
}
}

#[derive(Debug)]
pub enum InterpError<'tcx> {
/// The program caused undefined behavior.
UndefinedBehavior(UndefinedBehaviorInfo<'tcx>),
Expand All @@ -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,
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