Skip to content

Commit 0972d44

Browse files
authored
Rollup merge of #120398 - Urgau:into_diag_arg-numbers, r=compiler-errors
Improve handling of numbers in `IntoDiagnosticArg` While working on #120393, I realize that my fluent selectors were not working. So here is an improvement (not a fix unfortunately).
2 parents 58db961 + 93ff4a4 commit 0972d44

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

compiler/rustc_errors/src/diagnostic_impls.rs

+22-25
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,29 @@ macro_rules! into_diagnostic_arg_using_display {
5858
}
5959
}
6060

61+
macro_rules! into_diagnostic_arg_for_number {
62+
($( $ty:ty ),+ $(,)?) => {
63+
$(
64+
impl IntoDiagnosticArg for $ty {
65+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
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 {
72+
DiagnosticArgValue::Number(n)
73+
} else {
74+
self.to_string().into_diagnostic_arg()
75+
}
76+
}
77+
}
78+
)+
79+
}
80+
}
81+
6182
into_diagnostic_arg_using_display!(
6283
ast::ParamKindOrd,
63-
i8,
64-
u8,
65-
i16,
66-
u16,
67-
u32,
68-
i64,
69-
i128,
70-
u128,
7184
std::io::Error,
7285
Box<dyn std::error::Error>,
7386
std::num::NonZeroU32,
@@ -82,17 +95,7 @@ into_diagnostic_arg_using_display!(
8295
ExitStatus,
8396
);
8497

85-
impl IntoDiagnosticArg for i32 {
86-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
87-
DiagnosticArgValue::Number(self.into())
88-
}
89-
}
90-
91-
impl IntoDiagnosticArg for u64 {
92-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
93-
DiagnosticArgValue::Number(self.into())
94-
}
95-
}
98+
into_diagnostic_arg_for_number!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize);
9699

97100
impl IntoDiagnosticArg for bool {
98101
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
@@ -154,12 +157,6 @@ impl IntoDiagnosticArg for PathBuf {
154157
}
155158
}
156159

157-
impl IntoDiagnosticArg for usize {
158-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
159-
DiagnosticArgValue::Number(self as i128)
160-
}
161-
}
162-
163160
impl IntoDiagnosticArg for PanicStrategy {
164161
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
165162
DiagnosticArgValue::Str(Cow::Owned(self.desc().to_string()))

0 commit comments

Comments
 (0)