From 26eb6da4e7f19fd1408eac2c67891f3dbfc47e5a Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 30 Jan 2024 14:46:51 +1100 Subject: [PATCH] Fit more values into `DiagnosticArgValue::Number`. It contains an `i128`, but when creating them we convert any number outside the range -100..100 to a string, because Fluent uses an `f64`. It's all a bit strange. This commit changes the `i128` to an `i32`, which fits safely in Fluent's `f64`, and removes the -100..100 range check. This means that only integers outside the range of `i32` will be converted to strings. --- compiler/rustc_errors/src/diagnostic.rs | 5 ++++- compiler/rustc_errors/src/diagnostic_impls.rs | 8 ++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index b87eef07fd5..249155ba7c6 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -33,7 +33,10 @@ pub type DiagnosticArgName = Cow<'static, str>; #[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)] pub enum DiagnosticArgValue { Str(Cow<'static, str>), - Number(i128), + // This gets converted to a `FluentNumber`, which is an `f64`. An `i32` + // safely fits in an `f64`. Any integers bigger than that will be converted + // to strings in `into_diagnostic_arg` and stored using the `Str` variant. + Number(i32), StrListSepByAnd(Vec>), } diff --git a/compiler/rustc_errors/src/diagnostic_impls.rs b/compiler/rustc_errors/src/diagnostic_impls.rs index faab3fc663a..15effd3cbec 100644 --- a/compiler/rustc_errors/src/diagnostic_impls.rs +++ b/compiler/rustc_errors/src/diagnostic_impls.rs @@ -63,12 +63,8 @@ macro_rules! into_diagnostic_arg_for_number { $( impl IntoDiagnosticArg for $ty { fn into_diagnostic_arg(self) -> DiagnosticArgValue { - // 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::::try_into(self) && n >= -100 && n <= 100 { + // Convert to a string if it won't fit into `Number`. + if let Ok(n) = TryInto::::try_into(self) { DiagnosticArgValue::Number(n) } else { self.to_string().into_diagnostic_arg()