1
Fork 0

Apply suggested changes

This commit is contained in:
Ethan Brierley 2020-10-26 18:14:12 +00:00
parent 75e6deefee
commit ad2d93da1f
10 changed files with 48 additions and 25 deletions

View file

@ -46,6 +46,7 @@
#![feature(crate_visibility_modifier)] #![feature(crate_visibility_modifier)]
#![feature(associated_type_bounds)] #![feature(associated_type_bounds)]
#![feature(rustc_attrs)] #![feature(rustc_attrs)]
#![feature(int_error_matching)]
#![recursion_limit = "512"] #![recursion_limit = "512"]
#[macro_use] #[macro_use]

View file

@ -50,8 +50,10 @@ fn update_limit(
let error_str = match e.kind() { let error_str = match e.kind() {
IntErrorKind::PosOverflow => "`limit` is too large", IntErrorKind::PosOverflow => "`limit` is too large",
IntErrorKind::Empty => "`limit` must be a non-negative integer", IntErrorKind::Empty => "`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::NegOverflow => {
bug!("`limit` should never negatively underflow")
}
IntErrorKind::Zero => bug!("zero is a valid `limit`"), IntErrorKind::Zero => bug!("zero is a valid `limit`"),
kind => bug!("unimplemented IntErrorKind variant: {:?}", kind), kind => bug!("unimplemented IntErrorKind variant: {:?}", kind),
}; };

View file

@ -151,6 +151,7 @@
#![feature(slice_ptr_get)] #![feature(slice_ptr_get)]
#![feature(no_niche)] // rust-lang/rust#68303 #![feature(no_niche)] // rust-lang/rust#68303
#![feature(unsafe_block_in_unsafe_fn)] #![feature(unsafe_block_in_unsafe_fn)]
#![feature(int_error_matching)]
#![deny(unsafe_op_in_unsafe_fn)] #![deny(unsafe_op_in_unsafe_fn)]
#[prelude_import] #[prelude_import]

View file

@ -77,20 +77,26 @@ pub struct ParseIntError {
/// # Example /// # Example
/// ///
/// ``` /// ```
/// #![feature(int_error_matching)]
///
/// # fn main() { /// # fn main() {
/// if let Err(e) = i32::from_str_radix("a12", 10) { /// if let Err(e) = i32::from_str_radix("a12", 10) {
/// println!("Failed conversion to i32: {:?}", e.kind()); /// println!("Failed conversion to i32: {:?}", e.kind());
/// } /// }
/// # } /// # }
/// ``` /// ```
#[stable(feature = "int_error_matching", since = "1.47.0")] #[unstable(
feature = "int_error_matching",
reason = "it can be useful to match errors when making error messages \
for integer parsing",
issue = "22639"
)]
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive] #[non_exhaustive]
pub enum IntErrorKind { pub enum IntErrorKind {
/// Value being parsed is empty. /// Value being parsed is empty.
/// ///
/// Among other causes, this variant will be constructed when parsing an empty string. /// Among other causes, this variant will be constructed when parsing an empty string.
#[stable(feature = "int_error_matching", since = "1.47.0")]
Empty, Empty,
/// Contains an invalid digit in its context. /// Contains an invalid digit in its context.
/// ///
@ -99,25 +105,26 @@ pub enum IntErrorKind {
/// ///
/// This variant is also constructed when a `+` or `-` is misplaced within a string /// This variant is also constructed when a `+` or `-` is misplaced within a string
/// either on its own or in the middle of a number. /// either on its own or in the middle of a number.
#[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. /// Integer is too large to store in target integer type.
#[stable(feature = "int_error_matching", since = "1.47.0")]
PosOverflow, PosOverflow,
/// Integer is too small to store in target integer type. /// Integer is too small to store in target integer type.
#[stable(feature = "int_error_matching", since = "1.47.0")]
NegOverflow, NegOverflow,
/// Value was Zero /// Value was Zero
/// ///
/// This variant will be emitted when the parsing string has a value of zero, which /// This variant will be emitted when the parsing string has a value of zero, which
/// would be illegal for non-zero types. /// would be illegal for non-zero types.
#[stable(feature = "int_error_matching", since = "1.47.0")]
Zero, Zero,
} }
impl ParseIntError { impl ParseIntError {
/// Outputs the detailed cause of parsing an integer failing. /// Outputs the detailed cause of parsing an integer failing.
#[stable(feature = "int_error_matching", since = "1.47.0")] #[unstable(
feature = "int_error_matching",
reason = "it can be useful to match errors when making error messages \
for integer parsing",
issue = "22639"
)]
pub fn kind(&self) -> &IntErrorKind { pub fn kind(&self) -> &IntErrorKind {
&self.kind &self.kind
} }
@ -131,7 +138,7 @@ impl ParseIntError {
pub fn __description(&self) -> &str { pub fn __description(&self) -> &str {
match self.kind { match self.kind {
IntErrorKind::Empty => "cannot parse integer from empty string", 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::PosOverflow => "number too large to fit in target type",
IntErrorKind::NegOverflow => "number too small 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::Zero => "number would be zero for non-zero type",

View file

@ -63,7 +63,12 @@ pub use nonzero::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, No
#[stable(feature = "try_from", since = "1.34.0")] #[stable(feature = "try_from", since = "1.34.0")]
pub use error::TryFromIntError; pub use error::TryFromIntError;
#[stable(feature = "int_error_matching", since = "1.47.0")] #[unstable(
feature = "int_error_matching",
reason = "it can be useful to match errors when making error messages \
for integer parsing",
issue = "22639"
)]
pub use error::IntErrorKind; pub use error::IntErrorKind;
macro_rules! usize_isize_to_xe_bytes_doc { macro_rules! usize_isize_to_xe_bytes_doc {
@ -831,7 +836,7 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
let (is_positive, digits) = match src[0] { let (is_positive, digits) = match src[0] {
b'+' | b'-' if src[1..].is_empty() => { b'+' | b'-' if src[1..].is_empty() => {
return Err(PIE { kind: InvalidDigit(src[0] as char) }); return Err(PIE { kind: InvalidDigit });
} }
b'+' => (true, &src[1..]), b'+' => (true, &src[1..]),
b'-' if is_signed_ty => (false, &src[1..]), b'-' if is_signed_ty => (false, &src[1..]),
@ -844,7 +849,7 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
for &c in digits { for &c in digits {
let x = match (c as char).to_digit(radix) { let x = match (c as char).to_digit(radix) {
Some(x) => x, Some(x) => x,
None => return Err(PIE { kind: InvalidDigit(c as char) }), None => return Err(PIE { kind: InvalidDigit }),
}; };
result = match result.checked_mul(radix) { result = match result.checked_mul(radix) {
Some(result) => result, Some(result) => result,
@ -860,7 +865,7 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
for &c in digits { for &c in digits {
let x = match (c as char).to_digit(radix) { let x = match (c as char).to_digit(radix) {
Some(x) => x, Some(x) => x,
None => return Err(PIE { kind: InvalidDigit(c as char) }), None => return Err(PIE { kind: InvalidDigit }),
}; };
result = match result.checked_mul(radix) { result = match result.checked_mul(radix) {
Some(result) => result, Some(result) => result,

View file

@ -37,6 +37,7 @@
#![feature(try_trait)] #![feature(try_trait)]
#![feature(slice_internals)] #![feature(slice_internals)]
#![feature(slice_partition_dedup)] #![feature(slice_partition_dedup)]
#![feature(int_error_matching)]
#![feature(array_value_iter)] #![feature(array_value_iter)]
#![feature(iter_partition_in_place)] #![feature(iter_partition_in_place)]
#![feature(iter_is_partitioned)] #![feature(iter_is_partitioned)]

View 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!("0".parse::<NonZeroU8>().err().map(|e| e.kind().clone()), Some(IntErrorKind::Zero));
assert_eq!( assert_eq!(
"-1".parse::<NonZeroU8>().err().map(|e| e.kind().clone()), "-1".parse::<NonZeroU8>().err().map(|e| e.kind().clone()),
Some(IntErrorKind::InvalidDigit('-')) Some(IntErrorKind::InvalidDigit)
); );
assert_eq!( assert_eq!(
"-129".parse::<NonZeroI8>().err().map(|e| e.kind().clone()), "-129".parse::<NonZeroI8>().err().map(|e| e.kind().clone()),

View file

@ -118,14 +118,14 @@ fn test_leading_plus() {
#[test] #[test]
fn test_invalid() { fn test_invalid() {
test_parse::<i8>("--129", Err(IntErrorKind::InvalidDigit('-'))); test_parse::<i8>("--129", 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>("Съешь", Err(IntErrorKind::InvalidDigit));
test_parse::<u8>("123Hello", Err(IntErrorKind::InvalidDigit('H'))); test_parse::<u8>("123Hello", Err(IntErrorKind::InvalidDigit));
test_parse::<i8>("--", Err(IntErrorKind::InvalidDigit('-'))); test_parse::<i8>("--", Err(IntErrorKind::InvalidDigit));
test_parse::<i8>("-", Err(IntErrorKind::InvalidDigit('-'))); 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_parse::<u8>("-1", Err(IntErrorKind::InvalidDigit));
} }
#[test] #[test]

View file

@ -264,6 +264,7 @@
#![feature(global_asm)] #![feature(global_asm)]
#![feature(hashmap_internals)] #![feature(hashmap_internals)]
#![feature(int_error_internals)] #![feature(int_error_internals)]
#![feature(int_error_matching)]
#![feature(integer_atomics)] #![feature(integer_atomics)]
#![feature(into_future)] #![feature(into_future)]
#![feature(lang_items)] #![feature(lang_items)]

View file

@ -22,7 +22,12 @@ pub use core::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8,
#[stable(feature = "nonzero", since = "1.28.0")] #[stable(feature = "nonzero", since = "1.28.0")]
pub use core::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize}; pub use core::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
#[stable(feature = "int_error_matching", since = "1.47.0")] #[unstable(
feature = "int_error_matching",
reason = "it can be useful to match errors when making error messages \
for integer parsing",
issue = "22639"
)]
pub use core::num::IntErrorKind; pub use core::num::IntErrorKind;
#[cfg(test)] #[cfg(test)]