about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEthan Brierley <ethanboxx@gmail.com>2020-10-06 22:42:33 +0100
committerEthan Brierley <ethanboxx@gmail.com>2020-10-06 22:42:33 +0100
commit1e7e2e40e4992a82b9e5bc7120bd33964bcef355 (patch)
treeeb52105c4acbf8c116e51b48dcf5f13ea47f22da
parent8eaf0de1f45924a0fdbde00d4c7fe0333b377993 (diff)
downloadrust-1e7e2e40e4992a82b9e5bc7120bd33964bcef355.tar.gz
rust-1e7e2e40e4992a82b9e5bc7120bd33964bcef355.zip
remove OnlySign in favour of InvalidDigit
-rw-r--r--compiler/rustc_middle/src/middle/limits.rs4
-rw-r--r--library/core/src/num/error.rs9
-rw-r--r--library/core/src/num/mod.rs7
-rw-r--r--library/core/tests/num/mod.rs5
4 files changed, 11 insertions, 14 deletions
diff --git a/compiler/rustc_middle/src/middle/limits.rs b/compiler/rustc_middle/src/middle/limits.rs
index f03f439f73b..e0d171fa771 100644
--- a/compiler/rustc_middle/src/middle/limits.rs
+++ b/compiler/rustc_middle/src/middle/limits.rs
@@ -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`"),
diff --git a/library/core/src/num/error.rs b/library/core/src/num/error.rs
index a8f8a7804fd..401d52eb084 100644
--- a/library/core/src/num/error.rs
+++ b/library/core/src/num/error.rs
@@ -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 @@ impl ParseIntError {
             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",
         }
     }
 }
diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs
index a438f3161a3..fd00a072d89 100644
--- a/library/core/src/num/mod.rs
+++ b/library/core/src/num/mod.rs
@@ -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
diff --git a/library/core/tests/num/mod.rs b/library/core/tests/num/mod.rs
index a93cd38160b..4fd9f721b82 100644
--- a/library/core/tests/num/mod.rs
+++ b/library/core/tests/num/mod.rs
@@ -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));
 }