Skip to content

Commit

Permalink
fix diagnostic message
Browse files Browse the repository at this point in the history
  • Loading branch information
fee1-dead committed May 29, 2023
1 parent 74a6803 commit 5d2f4a1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
32 changes: 10 additions & 22 deletions compiler/rustc_const_eval/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::fmt;

use rustc_errors::{
DiagnosticArgValue, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Handler,
IntoDiagnostic,
Expand Down Expand Up @@ -409,24 +407,6 @@ 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 @@ -436,11 +416,19 @@ pub trait ReportErrorExt {
builder: &mut DiagnosticBuilder<'_, G>,
);

fn debug(self) -> DebugExt<Self>
fn debug(self) -> String
where
Self: Sized,
{
DebugExt(self)
ty::tls::with(move |tcx| {
let mut builder = tcx.sess.struct_allow(DiagnosticMessage::Str(String::new()));
let handler = &tcx.sess.parse_sess.span_diagnostic;
let message = self.diagnostic_message();
self.add_args(handler, &mut builder);
let s = handler.eagerly_translate_to_string(message, builder.args());
builder.cancel();
s
})
}
}

Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_mir_transform/src/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use either::Right;

use rustc_const_eval::const_eval::CheckAlignment;
use rustc_const_eval::ReportErrorExt;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::def::DefKind;
use rustc_index::bit_set::BitSet;
Expand Down Expand Up @@ -378,7 +379,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
op
}
Err(e) => {
trace!("get_const failed: {:?}", e.debug());
trace!("get_const failed: {:?}", e.into_kind().debug());
return None;
}
};
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_mir_transform/src/const_prop_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rustc_const_eval::interpret::Immediate;
use rustc_const_eval::interpret::{
self, InterpCx, InterpResult, LocalValue, MemoryKind, OpTy, Scalar, StackPopCleanup,
};
use rustc_const_eval::ReportErrorExt;
use rustc_hir::def::DefKind;
use rustc_hir::HirId;
use rustc_index::bit_set::BitSet;
Expand Down Expand Up @@ -232,7 +233,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
op
}
Err(e) => {
trace!("get_const failed: {:?}", e.debug());
trace!("get_const failed: {:?}", e.into_kind().debug());
return None;
}
};
Expand Down

0 comments on commit 5d2f4a1

Please sign in to comment.