Skip to content

Commit c027844

Browse files
committed
Fill in things needed to stabilize int_error_matching
1 parent fadf025 commit c027844

File tree

9 files changed

+66
-64
lines changed

9 files changed

+66
-64
lines changed

compiler/rustc_middle/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
#![feature(crate_visibility_modifier)]
4747
#![feature(associated_type_bounds)]
4848
#![feature(rustc_attrs)]
49-
#![feature(int_error_matching)]
5049
#![recursion_limit = "512"]
5150

5251
#[macro_use]

compiler/rustc_middle/src/middle/limits.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ fn update_limit(
4848
.unwrap_or(attr.span);
4949

5050
let error_str = match e.kind() {
51-
IntErrorKind::Overflow => "`limit` is too large",
52-
IntErrorKind::Empty => "`limit` must be a non-negative integer",
51+
IntErrorKind::PosOverflow => "`limit` is too large",
52+
IntErrorKind::Empty | IntErrorKind::OnlySign => {
53+
"`limit` must be a non-negative integer"
54+
}
5355
IntErrorKind::InvalidDigit => "not a valid integer",
54-
IntErrorKind::Underflow => bug!("`limit` should never underflow"),
56+
IntErrorKind::NegOverflow => bug!("`limit` should never underflow"),
5557
IntErrorKind::Zero => bug!("zero is a valid `limit`"),
5658
kind => bug!("unimplemented IntErrorKind variant: {:?}", kind),
5759
};

library/core/src/num/error.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,51 +77,47 @@ pub struct ParseIntError {
7777
/// # Example
7878
///
7979
/// ```
80-
/// #![feature(int_error_matching)]
81-
///
8280
/// # fn main() {
8381
/// if let Err(e) = i32::from_str_radix("a12", 10) {
8482
/// println!("Failed conversion to i32: {:?}", e.kind());
8583
/// }
8684
/// # }
8785
/// ```
88-
#[unstable(
89-
feature = "int_error_matching",
90-
reason = "it can be useful to match errors when making error messages \
91-
for integer parsing",
92-
issue = "22639"
93-
)]
86+
#[stable(feature = "int_error_matching", since = "1.47.0")]
9487
#[derive(Debug, Clone, PartialEq, Eq)]
9588
#[non_exhaustive]
9689
pub enum IntErrorKind {
9790
/// Value being parsed is empty.
9891
///
9992
/// Among other causes, this variant will be constructed when parsing an empty string.
93+
#[stable(feature = "int_error_matching", since = "1.47.0")]
10094
Empty,
10195
/// Contains an invalid digit.
10296
///
10397
/// Among other causes, this variant will be constructed when parsing a string that
10498
/// contains a letter.
99+
#[stable(feature = "int_error_matching", since = "1.47.0")]
105100
InvalidDigit,
106101
/// Integer is too large to store in target integer type.
107-
Overflow,
102+
#[stable(feature = "int_error_matching", since = "1.47.0")]
103+
PosOverflow,
108104
/// Integer is too small to store in target integer type.
109-
Underflow,
105+
#[stable(feature = "int_error_matching", since = "1.47.0")]
106+
NegOverflow,
110107
/// Value was Zero
111108
///
112109
/// This variant will be emitted when the parsing string has a value of zero, which
113110
/// would be illegal for non-zero types.
111+
#[stable(feature = "int_error_matching", since = "1.47.0")]
114112
Zero,
113+
/// The value contains nothing other than signs `+` or `-`.
114+
#[stable(feature = "int_error_matching", since = "1.47.0")]
115+
OnlySign,
115116
}
116117

117118
impl ParseIntError {
118119
/// Outputs the detailed cause of parsing an integer failing.
119-
#[unstable(
120-
feature = "int_error_matching",
121-
reason = "it can be useful to match errors when making error messages \
122-
for integer parsing",
123-
issue = "22639"
124-
)]
120+
#[stable(feature = "int_error_matching", since = "1.47.0")]
125121
pub fn kind(&self) -> &IntErrorKind {
126122
&self.kind
127123
}
@@ -136,9 +132,10 @@ impl ParseIntError {
136132
match self.kind {
137133
IntErrorKind::Empty => "cannot parse integer from empty string",
138134
IntErrorKind::InvalidDigit => "invalid digit found in string",
139-
IntErrorKind::Overflow => "number too large to fit in target type",
140-
IntErrorKind::Underflow => "number too small to fit in target type",
135+
IntErrorKind::PosOverflow => "number too large to fit in target type",
136+
IntErrorKind::NegOverflow => "number too small to fit in target type",
141137
IntErrorKind::Zero => "number would be zero for non-zero type",
138+
IntErrorKind::OnlySign => "only signs without digits found in string",
142139
}
143140
}
144141
}

library/core/src/num/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub use nonzero::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, No
6363
#[stable(feature = "try_from", since = "1.34.0")]
6464
pub use error::TryFromIntError;
6565

66-
#[unstable(feature = "int_error_matching", issue = "22639")]
66+
#[stable(feature = "int_error_matching", since = "1.47.0")]
6767
pub use error::IntErrorKind;
6868

6969
macro_rules! usize_isize_to_xe_bytes_doc {
@@ -836,7 +836,7 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
836836
};
837837

838838
if digits.is_empty() {
839-
return Err(PIE { kind: Empty });
839+
return Err(PIE { kind: OnlySign });
840840
}
841841

842842
let mut result = T::from_u32(0);
@@ -849,11 +849,11 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
849849
};
850850
result = match result.checked_mul(radix) {
851851
Some(result) => result,
852-
None => return Err(PIE { kind: Overflow }),
852+
None => return Err(PIE { kind: PosOverflow }),
853853
};
854854
result = match result.checked_add(x) {
855855
Some(result) => result,
856-
None => return Err(PIE { kind: Overflow }),
856+
None => return Err(PIE { kind: PosOverflow }),
857857
};
858858
}
859859
} else {
@@ -865,11 +865,11 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
865865
};
866866
result = match result.checked_mul(radix) {
867867
Some(result) => result,
868-
None => return Err(PIE { kind: Underflow }),
868+
None => return Err(PIE { kind: NegOverflow }),
869869
};
870870
result = match result.checked_sub(x) {
871871
Some(result) => result,
872-
None => return Err(PIE { kind: Underflow }),
872+
None => return Err(PIE { kind: NegOverflow }),
873873
};
874874
}
875875
}

library/core/tests/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#![feature(try_trait)]
3838
#![feature(slice_internals)]
3939
#![feature(slice_partition_dedup)]
40-
#![feature(int_error_matching)]
4140
#![feature(array_value_iter)]
4241
#![feature(iter_partition_in_place)]
4342
#![feature(iter_is_partitioned)]

library/core/tests/nonzero.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ fn test_from_str() {
135135
);
136136
assert_eq!(
137137
"-129".parse::<NonZeroI8>().err().map(|e| e.kind().clone()),
138-
Some(IntErrorKind::Underflow)
138+
Some(IntErrorKind::NegOverflow)
139139
);
140140
assert_eq!(
141141
"257".parse::<NonZeroU8>().err().map(|e| e.kind().clone()),
142-
Some(IntErrorKind::Overflow)
142+
Some(IntErrorKind::PosOverflow)
143143
);
144144
}
145145

library/core/tests/num/mod.rs

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use core::cmp::PartialEq;
22
use core::convert::{TryFrom, TryInto};
33
use core::fmt::Debug;
44
use core::marker::Copy;
5-
use core::num::TryFromIntError;
5+
use core::num::{IntErrorKind, ParseIntError, TryFromIntError};
66
use core::ops::{Add, Div, Mul, Rem, Sub};
77
use core::option::Option;
8-
use core::option::Option::{None, Some};
8+
use core::option::Option::None;
9+
use core::str::FromStr;
910

1011
#[macro_use]
1112
mod int_macros;
@@ -65,6 +66,14 @@ where
6566
assert_eq!(ten.rem(two), ten % two);
6667
}
6768

69+
fn test_parse<T>(num_str: &str, expected: Result<T, IntErrorKind>)
70+
where
71+
T: FromStr<Err = ParseIntError>,
72+
Result<T, IntErrorKind>: PartialEq + Debug,
73+
{
74+
assert_eq!(num_str.parse::<T>().map_err(|e| e.kind().clone()), expected)
75+
}
76+
6877
#[test]
6978
fn from_str_issue7588() {
7079
let u: Option<u8> = u8::from_str_radix("1000", 10).ok();
@@ -75,49 +84,51 @@ fn from_str_issue7588() {
7584

7685
#[test]
7786
fn test_int_from_str_overflow() {
78-
assert_eq!("127".parse::<i8>().ok(), Some(127i8));
79-
assert_eq!("128".parse::<i8>().ok(), None);
87+
test_parse::<i8>("127", Ok(127));
88+
test_parse::<i8>("128", Err(IntErrorKind::PosOverflow));
8089

81-
assert_eq!("-128".parse::<i8>().ok(), Some(-128i8));
82-
assert_eq!("-129".parse::<i8>().ok(), None);
90+
test_parse::<i8>("-128", Ok(-128));
91+
test_parse::<i8>("-129", Err(IntErrorKind::NegOverflow));
8392

84-
assert_eq!("32767".parse::<i16>().ok(), Some(32_767i16));
85-
assert_eq!("32768".parse::<i16>().ok(), None);
93+
test_parse::<i16>("32767", Ok(32_767));
94+
test_parse::<i16>("32768", Err(IntErrorKind::PosOverflow));
8695

87-
assert_eq!("-32768".parse::<i16>().ok(), Some(-32_768i16));
88-
assert_eq!("-32769".parse::<i16>().ok(), None);
96+
test_parse::<i16>("-32768", Ok(-32_768));
97+
test_parse::<i16>("-32769", Err(IntErrorKind::NegOverflow));
8998

90-
assert_eq!("2147483647".parse::<i32>().ok(), Some(2_147_483_647i32));
91-
assert_eq!("2147483648".parse::<i32>().ok(), None);
99+
test_parse::<i32>("2147483647", Ok(2_147_483_647));
100+
test_parse::<i32>("2147483648", Err(IntErrorKind::PosOverflow));
92101

93-
assert_eq!("-2147483648".parse::<i32>().ok(), Some(-2_147_483_648i32));
94-
assert_eq!("-2147483649".parse::<i32>().ok(), None);
102+
test_parse::<i32>("-2147483648", Ok(-2_147_483_648));
103+
test_parse::<i32>("-2147483649", Err(IntErrorKind::NegOverflow));
95104

96-
assert_eq!("9223372036854775807".parse::<i64>().ok(), Some(9_223_372_036_854_775_807i64));
97-
assert_eq!("9223372036854775808".parse::<i64>().ok(), None);
105+
test_parse::<i64>("9223372036854775807", Ok(9_223_372_036_854_775_807));
106+
test_parse::<i64>("9223372036854775808", Err(IntErrorKind::PosOverflow));
98107

99-
assert_eq!("-9223372036854775808".parse::<i64>().ok(), Some(-9_223_372_036_854_775_808i64));
100-
assert_eq!("-9223372036854775809".parse::<i64>().ok(), None);
108+
test_parse::<i64>("-9223372036854775808", Ok(-9_223_372_036_854_775_808));
109+
test_parse::<i64>("-9223372036854775809", Err(IntErrorKind::NegOverflow));
101110
}
102111

103112
#[test]
104113
fn test_leading_plus() {
105-
assert_eq!("+127".parse::<u8>().ok(), Some(127));
106-
assert_eq!("+9223372036854775807".parse::<i64>().ok(), Some(9223372036854775807));
114+
test_parse::<u8>("+127", Ok(127));
115+
test_parse::<i64>("+9223372036854775807", Ok(9223372036854775807));
107116
}
108117

109118
#[test]
110119
fn test_invalid() {
111-
assert_eq!("--129".parse::<i8>().ok(), None);
112-
assert_eq!("++129".parse::<i8>().ok(), None);
113-
assert_eq!("Съешь".parse::<u8>().ok(), None);
120+
test_parse::<i8>("--129", Err(IntErrorKind::InvalidDigit));
121+
test_parse::<i8>("++129", Err(IntErrorKind::InvalidDigit));
122+
test_parse::<u8>("Съешь", Err(IntErrorKind::InvalidDigit));
123+
// is this the correct error here. Maybe need a reapeat sign error here
124+
test_parse::<i8>("--", Err(IntErrorKind::InvalidDigit));
114125
}
115126

116127
#[test]
117128
fn test_empty() {
118-
assert_eq!("-".parse::<i8>().ok(), None);
119-
assert_eq!("+".parse::<i8>().ok(), None);
120-
assert_eq!("".parse::<u8>().ok(), None);
129+
test_parse::<i8>("-", Err(IntErrorKind::OnlySign));
130+
test_parse::<i8>("+", Err(IntErrorKind::OnlySign));
131+
test_parse::<u8>("", Err(IntErrorKind::Empty));
121132
}
122133

123134
#[test]

library/std/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@
264264
#![feature(global_asm)]
265265
#![feature(hashmap_internals)]
266266
#![feature(int_error_internals)]
267-
#![feature(int_error_matching)]
268267
#![feature(integer_atomics)]
269268
#![feature(into_future)]
270269
#![feature(lang_items)]

library/std/src/num.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ pub use core::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8,
2222
#[stable(feature = "nonzero", since = "1.28.0")]
2323
pub use core::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
2424

25-
#[unstable(
26-
feature = "int_error_matching",
27-
reason = "it can be useful to match errors when making error messages \
28-
for integer parsing",
29-
issue = "22639"
30-
)]
25+
#[stable(feature = "int_error_matching", since = "1.47.0")]
3126
pub use core::num::IntErrorKind;
3227

3328
#[cfg(test)]

0 commit comments

Comments
 (0)