Skip to content

Commit 1e7e2e4

Browse files
committed
remove OnlySign in favour of InvalidDigit
1 parent 8eaf0de commit 1e7e2e4

File tree

4 files changed

+11
-14
lines changed

4 files changed

+11
-14
lines changed

compiler/rustc_middle/src/middle/limits.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ fn update_limit(
4949

5050
let error_str = match e.kind() {
5151
IntErrorKind::PosOverflow => "`limit` is too large",
52-
IntErrorKind::Empty | IntErrorKind::OnlySign => {
53-
"`limit` must be a non-negative integer"
54-
}
52+
IntErrorKind::Empty => "`limit` must be a non-negative integer",
5553
IntErrorKind::InvalidDigit(_) => "not a valid integer",
5654
IntErrorKind::NegOverflow => bug!("`limit` should never underflow"),
5755
IntErrorKind::Zero => bug!("zero is a valid `limit`"),

library/core/src/num/error.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ pub enum IntErrorKind {
9595
/// Contains an digit invalid in its context.
9696
///
9797
/// Among other causes, this variant will be constructed when parsing a string that
98-
/// contains a letter.
98+
/// contains a non-asci char.
99+
///
100+
/// This variant is also constructed when a `+` or `-` is misplaced within a sting
101+
/// either on its own or in the middle of a number.
99102
#[stable(feature = "int_error_matching", since = "1.47.0")]
100103
InvalidDigit(#[stable(feature = "int_error_matching", since = "1.47.0")] char),
101104
/// Integer is too large to store in target integer type.
@@ -110,9 +113,6 @@ pub enum IntErrorKind {
110113
/// would be illegal for non-zero types.
111114
#[stable(feature = "int_error_matching", since = "1.47.0")]
112115
Zero,
113-
/// The value contains nothing other than sign `+` or `-`.
114-
#[stable(feature = "int_error_matching", since = "1.47.0")]
115-
OnlySign,
116116
}
117117

118118
impl ParseIntError {
@@ -135,7 +135,6 @@ impl ParseIntError {
135135
IntErrorKind::PosOverflow => "number too large to fit in target type",
136136
IntErrorKind::NegOverflow => "number too small to fit in target type",
137137
IntErrorKind::Zero => "number would be zero for non-zero type",
138-
IntErrorKind::OnlySign => "only sign without digits found in string",
139138
}
140139
}
141140
}

library/core/src/num/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -830,15 +830,14 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
830830
let src = src.as_bytes();
831831

832832
let (is_positive, digits) = match src[0] {
833+
b'+' | b'-' if src[1..].is_empty() => {
834+
return Err(PIE { kind: InvalidDigit(src[0] as char) });
835+
}
833836
b'+' => (true, &src[1..]),
834837
b'-' if is_signed_ty => (false, &src[1..]),
835838
_ => (true, src),
836839
};
837840

838-
if digits.is_empty() {
839-
return Err(PIE { kind: OnlySign });
840-
}
841-
842841
let mut result = T::from_u32(0);
843842
if is_positive {
844843
// The number is positive

library/core/tests/num/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,13 @@ fn test_invalid() {
122122
test_parse::<u8>("Съешь", Err(IntErrorKind::InvalidDigit('Ð')));
123123
test_parse::<u8>("123Hello", Err(IntErrorKind::InvalidDigit('H')));
124124
test_parse::<i8>("--", Err(IntErrorKind::InvalidDigit('-')));
125+
test_parse::<i8>("-", Err(IntErrorKind::InvalidDigit('-')));
126+
test_parse::<i8>("+", Err(IntErrorKind::InvalidDigit('+')));
127+
test_parse::<u8>("-1", Err(IntErrorKind::InvalidDigit('-')));
125128
}
126129

127130
#[test]
128131
fn test_empty() {
129-
test_parse::<i8>("-", Err(IntErrorKind::OnlySign));
130-
test_parse::<i8>("+", Err(IntErrorKind::OnlySign));
131132
test_parse::<u8>("", Err(IntErrorKind::Empty));
132133
}
133134

0 commit comments

Comments
 (0)