Skip to content

Commit

Permalink
Rollup merge of rust-lang#120398 - Urgau:into_diag_arg-numbers, r=com…
Browse files Browse the repository at this point in the history
…piler-errors

Improve handling of numbers in `IntoDiagnosticArg`

While working on rust-lang#120393, I realize that my fluent selectors were not working. So here is an improvement (not a fix unfortunately).
  • Loading branch information
Nadrieril authored Jan 27, 2024
2 parents c6618c0 + 93ff4a4 commit d9e06c7
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions compiler/rustc_errors/src/diagnostic_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,29 @@ macro_rules! into_diagnostic_arg_using_display {
}
}

macro_rules! into_diagnostic_arg_for_number {
($( $ty:ty ),+ $(,)?) => {
$(
impl IntoDiagnosticArg for $ty {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
// HACK: `FluentNumber` the underline backing struct represent
// numbers using a f64 which can't represent all the i128 numbers
// So in order to be able to use fluent selectors and still
// have all the numbers representable we only convert numbers
// below a certain threshold.
if let Ok(n) = TryInto::<i128>::try_into(self) && n >= -100 && n <= 100 {
DiagnosticArgValue::Number(n)
} else {
self.to_string().into_diagnostic_arg()
}
}
}
)+
}
}

into_diagnostic_arg_using_display!(
ast::ParamKindOrd,
i8,
u8,
i16,
u16,
u32,
i64,
i128,
u128,
std::io::Error,
Box<dyn std::error::Error>,
std::num::NonZeroU32,
Expand All @@ -82,17 +95,7 @@ into_diagnostic_arg_using_display!(
ExitStatus,
);

impl IntoDiagnosticArg for i32 {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Number(self.into())
}
}

impl IntoDiagnosticArg for u64 {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Number(self.into())
}
}
into_diagnostic_arg_for_number!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize);

impl IntoDiagnosticArg for bool {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
Expand Down Expand Up @@ -154,12 +157,6 @@ impl IntoDiagnosticArg for PathBuf {
}
}

impl IntoDiagnosticArg for usize {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Number(self as i128)
}
}

impl IntoDiagnosticArg for PanicStrategy {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(self.desc().to_string()))
Expand Down

0 comments on commit d9e06c7

Please sign in to comment.