Skip to content

Commit 26eb6da

Browse files
committed
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.
1 parent 3db37fb commit 26eb6da

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ pub type DiagnosticArgName = Cow<'static, str>;
3333
#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
3434
pub enum DiagnosticArgValue {
3535
Str(Cow<'static, str>),
36-
Number(i128),
36+
// This gets converted to a `FluentNumber`, which is an `f64`. An `i32`
37+
// safely fits in an `f64`. Any integers bigger than that will be converted
38+
// to strings in `into_diagnostic_arg` and stored using the `Str` variant.
39+
Number(i32),
3740
StrListSepByAnd(Vec<Cow<'static, str>>),
3841
}
3942

compiler/rustc_errors/src/diagnostic_impls.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,8 @@ macro_rules! into_diagnostic_arg_for_number {
6363
$(
6464
impl IntoDiagnosticArg for $ty {
6565
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
66-
// HACK: `FluentNumber` the underline backing struct represent
67-
// numbers using a f64 which can't represent all the i128 numbers
68-
// So in order to be able to use fluent selectors and still
69-
// have all the numbers representable we only convert numbers
70-
// below a certain threshold.
71-
if let Ok(n) = TryInto::<i128>::try_into(self) && n >= -100 && n <= 100 {
66+
// Convert to a string if it won't fit into `Number`.
67+
if let Ok(n) = TryInto::<i32>::try_into(self) {
7268
DiagnosticArgValue::Number(n)
7369
} else {
7470
self.to_string().into_diagnostic_arg()

0 commit comments

Comments
 (0)