]> git.lizzy.rs Git - rust.git/commitdiff
Bring char along with InvalidDigit
authorEthan Brierley <ethanboxx@gmail.com>
Tue, 6 Oct 2020 18:05:25 +0000 (19:05 +0100)
committerEthan Brierley <ethanboxx@gmail.com>
Tue, 6 Oct 2020 18:05:25 +0000 (19:05 +0100)
compiler/rustc_middle/src/middle/limits.rs
library/core/src/num/error.rs
library/core/src/num/mod.rs
library/core/tests/nonzero.rs
library/core/tests/num/mod.rs

index 6b6df3a303c2220a579e18026b294cb52011df7e..f03f439f73b5a7b702a260f262a80d3cd494b74e 100644 (file)
@@ -52,7 +52,7 @@ fn update_limit(
                         IntErrorKind::Empty | IntErrorKind::OnlySign => {
                             "`limit` must be a non-negative integer"
                         }
-                        IntErrorKind::InvalidDigit => "not a valid integer",
+                        IntErrorKind::InvalidDigit(_) => "not a valid integer",
                         IntErrorKind::NegOverflow => bug!("`limit` should never underflow"),
                         IntErrorKind::Zero => bug!("zero is a valid `limit`"),
                         kind => bug!("unimplemented IntErrorKind variant: {:?}", kind),
index 9705226ba243a33e414bc74ed93b6549d3108069..ba7c94656ce3ffbec01739dd4ceb87e53a0cc5b6 100644 (file)
@@ -92,12 +92,12 @@ pub enum IntErrorKind {
     /// Among other causes, this variant will be constructed when parsing an empty string.
     #[stable(feature = "int_error_matching", since = "1.47.0")]
     Empty,
-    /// Contains an invalid digit.
+    /// Contains an digit invalid in its context.
     ///
     /// Among other causes, this variant will be constructed when parsing a string that
     /// contains a letter.
     #[stable(feature = "int_error_matching", since = "1.47.0")]
-    InvalidDigit,
+    InvalidDigit(#[stable(feature = "int_error_matching", since = "1.47.0")] char),
     /// Integer is too large to store in target integer type.
     #[stable(feature = "int_error_matching", since = "1.47.0")]
     PosOverflow,
@@ -131,7 +131,7 @@ pub fn kind(&self) -> &IntErrorKind {
     pub fn __description(&self) -> &str {
         match self.kind {
             IntErrorKind::Empty => "cannot parse integer from empty string",
-            IntErrorKind::InvalidDigit => "invalid digit found in string",
+            IntErrorKind::InvalidDigit(_) => "invalid digit found in string",
             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",
index 67b4b885dd2ecf597744ad84042a49bd737c942a..a438f3161a3af3154fec0e1f131bdbec47d04823 100644 (file)
@@ -845,7 +845,7 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
         for &c in digits {
             let x = match (c as char).to_digit(radix) {
                 Some(x) => x,
-                None => return Err(PIE { kind: InvalidDigit }),
+                None => return Err(PIE { kind: InvalidDigit(c as char) }),
             };
             result = match result.checked_mul(radix) {
                 Some(result) => result,
@@ -861,7 +861,7 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
         for &c in digits {
             let x = match (c as char).to_digit(radix) {
                 Some(x) => x,
-                None => return Err(PIE { kind: InvalidDigit }),
+                None => return Err(PIE { kind: InvalidDigit(c as char) }),
             };
             result = match result.checked_mul(radix) {
                 Some(result) => result,
index fb1293c99bba934748dd13362c0a994eea41d833..949d4ea32f06463f7d6ca1b339b5bb1b0967dccb 100644 (file)
@@ -131,7 +131,7 @@ fn test_from_str() {
     assert_eq!("0".parse::<NonZeroU8>().err().map(|e| e.kind().clone()), Some(IntErrorKind::Zero));
     assert_eq!(
         "-1".parse::<NonZeroU8>().err().map(|e| e.kind().clone()),
-        Some(IntErrorKind::InvalidDigit)
+        Some(IntErrorKind::InvalidDigit('-'))
     );
     assert_eq!(
         "-129".parse::<NonZeroI8>().err().map(|e| e.kind().clone()),
index d6f92f25e78461ecf4912a37d92222f66c9df7ee..a93cd38160b5896db6e6c859ebfe8c5120f0cfbd 100644 (file)
@@ -117,11 +117,11 @@ fn test_leading_plus() {
 
 #[test]
 fn test_invalid() {
-    test_parse::<i8>("--129", Err(IntErrorKind::InvalidDigit));
-    test_parse::<i8>("++129", Err(IntErrorKind::InvalidDigit));
-    test_parse::<u8>("Съешь", Err(IntErrorKind::InvalidDigit));
-    // is this the correct error here. Maybe need a reapeat sign error here
-    test_parse::<i8>("--", Err(IntErrorKind::InvalidDigit));
+    test_parse::<i8>("--129", Err(IntErrorKind::InvalidDigit('-')));
+    test_parse::<i8>("++129", Err(IntErrorKind::InvalidDigit('+')));
+    test_parse::<u8>("Съешь", Err(IntErrorKind::InvalidDigit('Ð')));
+    test_parse::<u8>("123Hello", Err(IntErrorKind::InvalidDigit('H')));
+    test_parse::<i8>("--", Err(IntErrorKind::InvalidDigit('-')));
 }
 
 #[test]