Skip to content

Commit f6c2bc5

Browse files
committed
fix diagnostic message
1 parent f964b46 commit f6c2bc5

File tree

3 files changed

+15
-25
lines changed

3 files changed

+15
-25
lines changed

compiler/rustc_const_eval/src/errors.rs

+11-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::fmt;
2-
31
use rustc_errors::{
42
DiagnosticArgValue, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Handler,
53
IntoDiagnostic,
@@ -427,24 +425,6 @@ pub struct UndefinedBehavior {
427425
pub raw_bytes: RawBytesNote,
428426
}
429427

430-
pub struct DebugExt<T>(T);
431-
432-
impl<T: ReportErrorExt> fmt::Debug for DebugExt<T> {
433-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
434-
let s = ty::tls::with(|tcx| {
435-
let mut builder = tcx.sess.struct_allow("");
436-
let handler = &tcx.sess.parse_sess.span_diagnostic;
437-
let message = self.0.diagnostic_message();
438-
self.0.add_args(handler, &mut builder);
439-
let s = handler.eagerly_translate_to_string(message, builder.args());
440-
builder.cancel();
441-
s
442-
});
443-
444-
f.write_str(&s)
445-
}
446-
}
447-
448428
pub trait ReportErrorExt {
449429
/// Returns the diagnostic message for this error.
450430
fn diagnostic_message(&self) -> DiagnosticMessage;
@@ -454,11 +434,19 @@ pub trait ReportErrorExt {
454434
builder: &mut DiagnosticBuilder<'_, G>,
455435
);
456436

457-
fn debug(self) -> DebugExt<Self>
437+
fn debug(self) -> String
458438
where
459439
Self: Sized,
460440
{
461-
DebugExt(self)
441+
ty::tls::with(move |tcx| {
442+
let mut builder = tcx.sess.struct_allow(DiagnosticMessage::Str(String::new().into()));
443+
let handler = &tcx.sess.parse_sess.span_diagnostic;
444+
let message = self.diagnostic_message();
445+
self.add_args(handler, &mut builder);
446+
let s = handler.eagerly_translate_to_string(message, builder.args());
447+
builder.cancel();
448+
s
449+
})
462450
}
463451
}
464452

@@ -481,7 +469,7 @@ impl<'a> ReportErrorExt for UndefinedBehaviorInfo<'a> {
481469
use crate::fluent_generated::*;
482470
use UndefinedBehaviorInfo::*;
483471
match self {
484-
Ub(msg) => (&**msg).into(),
472+
Ub(msg) => msg.clone().into(),
485473
Unreachable => const_eval_unreachable,
486474
BoundsCheckFailed { .. } => const_eval_bounds_check_failed,
487475
DivisionByZero => const_eval_division_by_zero,

compiler/rustc_mir_transform/src/const_prop.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use either::Right;
55

66
use rustc_const_eval::const_eval::CheckAlignment;
7+
use rustc_const_eval::ReportErrorExt;
78
use rustc_data_structures::fx::FxHashSet;
89
use rustc_hir::def::DefKind;
910
use rustc_index::bit_set::BitSet;
@@ -378,7 +379,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
378379
op
379380
}
380381
Err(e) => {
381-
trace!("get_const failed: {:?}", e.debug());
382+
trace!("get_const failed: {:?}", e.into_kind().debug());
382383
return None;
383384
}
384385
};

compiler/rustc_mir_transform/src/const_prop_lint.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_const_eval::interpret::Immediate;
99
use rustc_const_eval::interpret::{
1010
self, InterpCx, InterpResult, LocalValue, MemoryKind, OpTy, Scalar, StackPopCleanup,
1111
};
12+
use rustc_const_eval::ReportErrorExt;
1213
use rustc_hir::def::DefKind;
1314
use rustc_hir::HirId;
1415
use rustc_index::bit_set::BitSet;
@@ -232,7 +233,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
232233
op
233234
}
234235
Err(e) => {
235-
trace!("get_const failed: {:?}", e.debug());
236+
trace!("get_const failed: {:?}", e.into_kind().debug());
236237
return None;
237238
}
238239
};

0 commit comments

Comments
 (0)