]> git.lizzy.rs Git - rust.git/commitdiff
remove OnlySign in favour of InvalidDigit
authorEthan Brierley <ethanboxx@gmail.com>
Tue, 6 Oct 2020 21:42:33 +0000 (22:42 +0100)
committerEthan Brierley <ethanboxx@gmail.com>
Tue, 6 Oct 2020 21:42:33 +0000 (22:42 +0100)
compiler/rustc_middle/src/middle/limits.rs
library/core/src/num/error.rs
library/core/src/num/mod.rs
library/core/tests/num/mod.rs

index f03f439f73b5a7b702a260f262a80d3cd494b74e..e0d171fa77125cf0bf52e1137f17566751b185e3 100644 (file)
@@ -49,9 +49,7 @@ fn update_limit(
 
                     let error_str = match e.kind() {
                         IntErrorKind::PosOverflow => "`limit` is too large",
-                        IntErrorKind::Empty | IntErrorKind::OnlySign => {
-                            "`limit` must be a non-negative integer"
-                        }
+                        IntErrorKind::Empty => "`limit` must be a non-negative integer",
                         IntErrorKind::InvalidDigit(_) => "not a valid integer",
                         IntErrorKind::NegOverflow => bug!("`limit` should never underflow"),
                         IntErrorKind::Zero => bug!("zero is a valid `limit`"),
index a8f8a7804fd782e273518312728db1c009ac10ea..401d52eb0848118ee0005dc10d9de6749d59a605 100644 (file)
@@ -95,7 +95,10 @@ pub enum IntErrorKind {
     /// Contains an digit invalid in its context.
     ///
     /// Among other causes, this variant will be constructed when parsing a string that
-    /// contains a letter.
+    /// contains a non-asci char.
+    ///
+    /// This variant is also constructed when a `+` or `-` is misplaced within a sting
+    /// either on its own or in the middle of a number.
     #[stable(feature = "int_error_matching", since = "1.47.0")]
     InvalidDigit(#[stable(feature = "int_error_matching", since = "1.47.0")] char),
     /// Integer is too large to store in target integer type.
@@ -110,9 +113,6 @@ pub enum IntErrorKind {
     /// would be illegal for non-zero types.
     #[stable(feature = "int_error_matching", since = "1.47.0")]
     Zero,
-    /// The value contains nothing other than sign `+` or `-`.
-    #[stable(feature = "int_error_matching", since = "1.47.0")]
-    OnlySign,
 }
 
 impl ParseIntError {
@@ -135,7 +135,6 @@ pub fn __description(&self) -> &str {
             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::OnlySign => "only sign without digits found in string",
         }
     }
 }
index a438f3161a3af3154fec0e1f131bdbec47d04823..fd00a072d896c903b537a9ee4d1e139e00247ce5 100644 (file)
@@ -830,15 +830,14 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
     let src = src.as_bytes();
 
     let (is_positive, digits) = match src[0] {
+        b'+' | b'-' if src[1..].is_empty() => {
+            return Err(PIE { kind: InvalidDigit(src[0] as char) });
+        }
         b'+' => (true, &src[1..]),
         b'-' if is_signed_ty => (false, &src[1..]),
         _ => (true, src),
     };
 
-    if digits.is_empty() {
-        return Err(PIE { kind: OnlySign });
-    }
-
     let mut result = T::from_u32(0);
     if is_positive {
         // The number is positive
index a93cd38160b5896db6e6c859ebfe8c5120f0cfbd..4fd9f721b823be057973b0c00448176c5338bc54 100644 (file)
@@ -122,12 +122,13 @@ fn test_invalid() {
     test_parse::<u8>("Съешь", Err(IntErrorKind::InvalidDigit('Ð')));
     test_parse::<u8>("123Hello", Err(IntErrorKind::InvalidDigit('H')));
     test_parse::<i8>("--", Err(IntErrorKind::InvalidDigit('-')));
+    test_parse::<i8>("-", Err(IntErrorKind::InvalidDigit('-')));
+    test_parse::<i8>("+", Err(IntErrorKind::InvalidDigit('+')));
+    test_parse::<u8>("-1", Err(IntErrorKind::InvalidDigit('-')));
 }
 
 #[test]
 fn test_empty() {
-    test_parse::<i8>("-", Err(IntErrorKind::OnlySign));
-    test_parse::<i8>("+", Err(IntErrorKind::OnlySign));
     test_parse::<u8>("", Err(IntErrorKind::Empty));
 }