From 02291502e5c21de05c0aca5cc3862d2ca6144500 Mon Sep 17 00:00:00 2001 From: Shun Sakai Date: Wed, 6 May 2026 16:38:21 +0900 Subject: [PATCH] feat(num): improve error messages for `TryFromIntError` Change to display more detailed error messages based on `IntErrorKind`. --- library/core/src/num/error.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/library/core/src/num/error.rs b/library/core/src/num/error.rs index 8cd065630cd88..3565d9a51fa34 100644 --- a/library/core/src/num/error.rs +++ b/library/core/src/num/error.rs @@ -21,7 +21,14 @@ impl TryFromIntError { #[stable(feature = "try_from", since = "1.34.0")] impl fmt::Display for TryFromIntError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - "out of range integral type conversion attempted".fmt(f) + match self.0 { + IntErrorKind::Empty | IntErrorKind::InvalidDigit => unreachable!(), + IntErrorKind::PosOverflow => "number too large to fit in target type", + IntErrorKind::NegOverflow => "number too small to fit in target type", + IntErrorKind::Zero => "number would be zero for non-zero type", + IntErrorKind::NotAPowerOfTwo => "number is not a power of two", + } + .fmt(f) } }