From 4f83717cf768adb0b0dfe23b8eecf2b259eec354 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Wed, 17 May 2023 10:30:14 +0000 Subject: Use translatable diagnostics in `rustc_const_eval` --- compiler/rustc_mir_transform/src/const_prop.rs | 15 +++++++++++++-- compiler/rustc_mir_transform/src/const_prop_lint.rs | 5 ++--- compiler/rustc_mir_transform/src/errors.rs | 11 +++++++++-- 3 files changed, 24 insertions(+), 7 deletions(-) (limited to 'compiler/rustc_mir_transform/src') diff --git a/compiler/rustc_mir_transform/src/const_prop.rs b/compiler/rustc_mir_transform/src/const_prop.rs index 1ba1951afde..2639bbee86f 100644 --- a/compiler/rustc_mir_transform/src/const_prop.rs +++ b/compiler/rustc_mir_transform/src/const_prop.rs @@ -37,6 +37,7 @@ macro_rules! throw_machine_stop_str { ($($tt:tt)*) => {{ // We make a new local type for it. The type itself does not carry any information, // but its vtable (for the `MachineStopType` trait) does. + #[derive(Debug)] struct Zst; // Printing this type shows the desired string. impl std::fmt::Display for Zst { @@ -44,7 +45,17 @@ macro_rules! throw_machine_stop_str { write!(f, $($tt)*) } } - impl rustc_middle::mir::interpret::MachineStopType for Zst {} + + impl rustc_middle::mir::interpret::MachineStopType for Zst { + fn diagnostic_message(&self) -> rustc_errors::DiagnosticMessage { + self.to_string().into() + } + + fn add_args( + self: Box, + _: &mut dyn FnMut(std::borrow::Cow<'static, str>, rustc_errors::DiagnosticArgValue<'static>), + ) {} + } throw_machine_stop!(Zst) }}; } @@ -367,7 +378,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { op } Err(e) => { - trace!("get_const failed: {}", e); + trace!("get_const failed: {e:?}"); return None; } }; diff --git a/compiler/rustc_mir_transform/src/const_prop_lint.rs b/compiler/rustc_mir_transform/src/const_prop_lint.rs index 0fe49b8a1bb..61cc76e6646 100644 --- a/compiler/rustc_mir_transform/src/const_prop_lint.rs +++ b/compiler/rustc_mir_transform/src/const_prop_lint.rs @@ -232,7 +232,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { op } Err(e) => { - trace!("get_const failed: {}", e); + trace!("get_const failed: {e:?}"); return None; } }; @@ -272,8 +272,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { // dedicated error variants should be introduced instead. assert!( !error.kind().formatted_string(), - "const-prop encountered formatting error: {}", - error + "const-prop encountered formatting error: {error:?}", ); None } diff --git a/compiler/rustc_mir_transform/src/errors.rs b/compiler/rustc_mir_transform/src/errors.rs index 602e40d5131..22f71bb0851 100644 --- a/compiler/rustc_mir_transform/src/errors.rs +++ b/compiler/rustc_mir_transform/src/errors.rs @@ -163,7 +163,14 @@ impl<'a, P: std::fmt::Debug> DecorateLint<'a, ()> for AssertLint

{ self, diag: &'b mut DiagnosticBuilder<'a, ()>, ) -> &'b mut DiagnosticBuilder<'a, ()> { - diag.span_label(self.span(), format!("{:?}", self.panic())); + let span = self.span(); + let assert_kind = self.panic(); + let message = assert_kind.diagnostic_message(); + assert_kind.add_args(&mut |name, value| { + diag.set_arg(name, value); + }); + diag.span_label(span, message); + diag } @@ -191,7 +198,7 @@ impl

AssertLint

{ AssertLint::ArithmeticOverflow(sp, _) | AssertLint::UnconditionalPanic(sp, _) => *sp, } } - pub fn panic(&self) -> &AssertKind

{ + pub fn panic(self) -> AssertKind

{ match self { AssertLint::ArithmeticOverflow(_, p) | AssertLint::UnconditionalPanic(_, p) => p, } -- cgit 1.4.1-3-g733a5 From f964b46451f3a323f312a7035e25180060044335 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Mon, 29 May 2023 03:32:38 +0000 Subject: improve debug message by eagerly translating --- compiler/rustc_const_eval/src/errors.rs | 29 +++++++++++++++++++++- compiler/rustc_middle/src/mir/interpret/error.rs | 14 +---------- compiler/rustc_mir_transform/src/const_prop.rs | 2 +- .../rustc_mir_transform/src/const_prop_lint.rs | 2 +- 4 files changed, 31 insertions(+), 16 deletions(-) (limited to 'compiler/rustc_mir_transform/src') diff --git a/compiler/rustc_const_eval/src/errors.rs b/compiler/rustc_const_eval/src/errors.rs index 8a71e3b3401..c83f6cf13e0 100644 --- a/compiler/rustc_const_eval/src/errors.rs +++ b/compiler/rustc_const_eval/src/errors.rs @@ -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); + +impl fmt::Debug for DebugExt { + 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 + where + Self: Sized, + { + DebugExt(self) + } } fn bad_pointer_message(msg: CheckInAllocMsg, handler: &Handler) -> String { diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs index ca6f58adbb1..2435bc59ec0 100644 --- a/compiler/rustc_middle/src/mir/interpret/error.rs +++ b/compiler/rustc_middle/src/mir/interpret/error.rs @@ -469,6 +469,7 @@ impl dyn MachineStopType { } } +#[derive(Debug)] pub enum InterpError<'tcx> { /// The program caused undefined behavior. UndefinedBehavior(UndefinedBehaviorInfo<'tcx>), @@ -487,19 +488,6 @@ pub enum InterpError<'tcx> { pub type InterpResult<'tcx, T = ()> = Result>; -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, diff --git a/compiler/rustc_mir_transform/src/const_prop.rs b/compiler/rustc_mir_transform/src/const_prop.rs index 2639bbee86f..c6d60039eb9 100644 --- a/compiler/rustc_mir_transform/src/const_prop.rs +++ b/compiler/rustc_mir_transform/src/const_prop.rs @@ -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; } }; diff --git a/compiler/rustc_mir_transform/src/const_prop_lint.rs b/compiler/rustc_mir_transform/src/const_prop_lint.rs index 61cc76e6646..16826582372 100644 --- a/compiler/rustc_mir_transform/src/const_prop_lint.rs +++ b/compiler/rustc_mir_transform/src/const_prop_lint.rs @@ -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; } }; -- cgit 1.4.1-3-g733a5 From f6c2bc5c24fd08200c0e4438d981ca58dc71f488 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Mon, 29 May 2023 06:40:15 +0000 Subject: fix diagnostic message --- compiler/rustc_const_eval/src/errors.rs | 34 +++++++--------------- compiler/rustc_mir_transform/src/const_prop.rs | 3 +- .../rustc_mir_transform/src/const_prop_lint.rs | 3 +- 3 files changed, 15 insertions(+), 25 deletions(-) (limited to 'compiler/rustc_mir_transform/src') diff --git a/compiler/rustc_const_eval/src/errors.rs b/compiler/rustc_const_eval/src/errors.rs index c83f6cf13e0..eed3091d481 100644 --- a/compiler/rustc_const_eval/src/errors.rs +++ b/compiler/rustc_const_eval/src/errors.rs @@ -1,5 +1,3 @@ -use std::fmt; - use rustc_errors::{ DiagnosticArgValue, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Handler, IntoDiagnostic, @@ -427,24 +425,6 @@ pub struct UndefinedBehavior { pub raw_bytes: RawBytesNote, } -pub struct DebugExt(T); - -impl fmt::Debug for DebugExt { - 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; @@ -454,11 +434,19 @@ pub trait ReportErrorExt { builder: &mut DiagnosticBuilder<'_, G>, ); - fn debug(self) -> DebugExt + 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().into())); + 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 + }) } } @@ -481,7 +469,7 @@ impl<'a> ReportErrorExt for UndefinedBehaviorInfo<'a> { use crate::fluent_generated::*; use UndefinedBehaviorInfo::*; match self { - Ub(msg) => (&**msg).into(), + Ub(msg) => msg.clone().into(), Unreachable => const_eval_unreachable, BoundsCheckFailed { .. } => const_eval_bounds_check_failed, DivisionByZero => const_eval_division_by_zero, diff --git a/compiler/rustc_mir_transform/src/const_prop.rs b/compiler/rustc_mir_transform/src/const_prop.rs index c6d60039eb9..26d1201266f 100644 --- a/compiler/rustc_mir_transform/src/const_prop.rs +++ b/compiler/rustc_mir_transform/src/const_prop.rs @@ -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; @@ -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; } }; diff --git a/compiler/rustc_mir_transform/src/const_prop_lint.rs b/compiler/rustc_mir_transform/src/const_prop_lint.rs index 16826582372..759650fe4db 100644 --- a/compiler/rustc_mir_transform/src/const_prop_lint.rs +++ b/compiler/rustc_mir_transform/src/const_prop_lint.rs @@ -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; @@ -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; } }; -- cgit 1.4.1-3-g733a5