Skip to content

Stabilize TryFrom / TryInto, and tweak impls for integers #49305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/libcore/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ unsafe impl<T, A: Unsize<[T]>> FixedSizeArray<T> for A {
}

/// The error type returned when a conversion from a slice to an array fails.
#[unstable(feature = "try_from", issue = "33417")]
#[stable(feature = "try_from", since = "1.26.0")]
#[derive(Debug, Copy, Clone)]
pub struct TryFromSliceError(());

Expand Down Expand Up @@ -148,7 +148,7 @@ macro_rules! array_impls {
}
}

#[unstable(feature = "try_from", issue = "33417")]
#[stable(feature = "try_from", since = "1.26.0")]
impl<'a, T> TryFrom<&'a [T]> for &'a [T; $N] {
type Error = TryFromSliceError;

Expand All @@ -162,7 +162,7 @@ macro_rules! array_impls {
}
}

#[unstable(feature = "try_from", issue = "33417")]
#[stable(feature = "try_from", since = "1.26.0")]
impl<'a, T> TryFrom<&'a mut [T]> for &'a mut [T; $N] {
type Error = TryFromSliceError;

Expand Down
6 changes: 3 additions & 3 deletions src/libcore/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl FromStr for char {
}


#[unstable(feature = "try_from", issue = "33417")]
#[stable(feature = "try_from", since = "1.26.0")]
impl TryFrom<u32> for char {
type Error = CharTryFromError;

Expand All @@ -280,11 +280,11 @@ impl TryFrom<u32> for char {
}

/// The error type returned when a conversion from u32 to char fails.
#[unstable(feature = "try_from", issue = "33417")]
#[stable(feature = "try_from", since = "1.26.0")]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct CharTryFromError(());

#[unstable(feature = "try_from", issue = "33417")]
#[stable(feature = "try_from", since = "1.26.0")]
impl fmt::Display for CharTryFromError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
"converted integer out of range for `char`".fmt(f)
Expand Down
12 changes: 8 additions & 4 deletions src/libcore/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,22 +322,26 @@ pub trait From<T>: Sized {
///
/// [`TryFrom`]: trait.TryFrom.html
/// [`Into`]: trait.Into.html
#[unstable(feature = "try_from", issue = "33417")]
#[stable(feature = "try_from", since = "1.26.0")]
pub trait TryInto<T>: Sized {
/// The type returned in the event of a conversion error.
#[stable(feature = "try_from", since = "1.26.0")]
type Error;

/// Performs the conversion.
#[stable(feature = "try_from", since = "1.26.0")]
fn try_into(self) -> Result<T, Self::Error>;
}

/// Attempt to construct `Self` via a conversion.
#[unstable(feature = "try_from", issue = "33417")]
#[stable(feature = "try_from", since = "1.26.0")]
pub trait TryFrom<T>: Sized {
/// The type returned in the event of a conversion error.
#[stable(feature = "try_from", since = "1.26.0")]
type Error;

/// Performs the conversion.
#[stable(feature = "try_from", since = "1.26.0")]
fn try_from(value: T) -> Result<Self, Self::Error>;
}

Expand Down Expand Up @@ -405,7 +409,7 @@ impl<T> From<T> for T {


// TryFrom implies TryInto
#[unstable(feature = "try_from", issue = "33417")]
#[stable(feature = "try_from", since = "1.26.0")]
impl<T, U> TryInto<U> for T where U: TryFrom<T>
{
type Error = U::Error;
Expand All @@ -417,7 +421,7 @@ impl<T, U> TryInto<U> for T where U: TryFrom<T>

// Infallible conversions are semantically equivalent to fallible conversions
// with an uninhabited error type.
#[unstable(feature = "try_from", issue = "33417")]
#[stable(feature = "try_from", since = "1.26.0")]
impl<T, U> TryFrom<U> for T where T: From<U> {
type Error = !;

Expand Down
74 changes: 72 additions & 2 deletions src/libcore/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ macro_rules! step_impl_unsigned {
#[inline]
#[allow(unreachable_patterns)]
fn add_usize(&self, n: usize) -> Option<Self> {
match <$t>::try_from(n) {
match <$t>::private_try_from(n) {
Ok(n_as_t) => self.checked_add(n_as_t),
Err(_) => None,
}
Expand Down Expand Up @@ -123,7 +123,7 @@ macro_rules! step_impl_signed {
#[inline]
#[allow(unreachable_patterns)]
fn add_usize(&self, n: usize) -> Option<Self> {
match <$unsigned>::try_from(n) {
match <$unsigned>::private_try_from(n) {
Ok(n_as_unsigned) => {
// Wrapping in unsigned space handles cases like
// `-120_i8.add_usize(200) == Some(80_i8)`,
Expand Down Expand Up @@ -461,3 +461,73 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {

#[stable(feature = "fused", since = "1.26.0")]
impl<A: Step> FusedIterator for ops::RangeInclusive<A> {}

/// Compensate removal of some impls per
/// https://github.com/rust-lang/rust/pull/49305#issuecomment-376293243
trait PrivateTryFromUsize: Sized {
fn private_try_from(n: usize) -> Result<Self, ()>;
}

impl<T> PrivateTryFromUsize for T where T: TryFrom<usize> {
#[inline]
fn private_try_from(n: usize) -> Result<Self, ()> {
T::try_from(n).map_err(|_| ())
}
}

// no possible bounds violation
macro_rules! try_from_unbounded {
($($target:ty),*) => {$(
impl PrivateTryFromUsize for $target {
#[inline]
fn private_try_from(value: usize) -> Result<Self, ()> {
Ok(value as $target)
}
}
)*}
}

// unsigned to signed (only positive bound)
macro_rules! try_from_upper_bounded {
($($target:ty),*) => {$(
impl PrivateTryFromUsize for $target {
#[inline]
fn private_try_from(u: usize) -> Result<$target, ()> {
if u > (<$target>::max_value() as usize) {
Err(())
} else {
Ok(u as $target)
}
}
}
)*}
}


#[cfg(target_pointer_width = "16")]
mod ptr_try_from_impls {
use super::PrivateTryFromUsize;

try_from_unbounded!(u16, u32, u64, u128);
try_from_unbounded!(i32, i64, i128);
}

#[cfg(target_pointer_width = "32")]
mod ptr_try_from_impls {
use super::PrivateTryFromUsize;

try_from_upper_bounded!(u16);
try_from_unbounded!(u32, u64, u128);
try_from_upper_bounded!(i32);
try_from_unbounded!(i64, i128);
}

#[cfg(target_pointer_width = "64")]
mod ptr_try_from_impls {
use super::PrivateTryFromUsize;

try_from_upper_bounded!(u16, u32);
try_from_unbounded!(u64, u128);
try_from_upper_bounded!(i32, i64);
try_from_unbounded!(i128);
}
99 changes: 30 additions & 69 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3647,7 +3647,7 @@ macro_rules! from_str_radix_int_impl {
from_str_radix_int_impl! { isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128 }

/// The error type returned when a checked integral type conversion fails.
#[unstable(feature = "try_from", issue = "33417")]
#[stable(feature = "try_from", since = "1.26.0")]
#[derive(Debug, Copy, Clone)]
pub struct TryFromIntError(());

Expand All @@ -3662,39 +3662,24 @@ impl TryFromIntError {
}
}

#[unstable(feature = "try_from", issue = "33417")]
#[stable(feature = "try_from", since = "1.26.0")]
impl fmt::Display for TryFromIntError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.__description().fmt(fmt)
}
}

#[unstable(feature = "try_from", issue = "33417")]
#[stable(feature = "try_from", since = "1.26.0")]
impl From<!> for TryFromIntError {
fn from(never: !) -> TryFromIntError {
never
}
}

// no possible bounds violation
macro_rules! try_from_unbounded {
($source:ty, $($target:ty),*) => {$(
#[unstable(feature = "try_from", issue = "33417")]
impl TryFrom<$source> for $target {
type Error = !;

#[inline]
fn try_from(value: $source) -> Result<Self, Self::Error> {
Ok(value as $target)
}
}
)*}
}

// only negative bounds
macro_rules! try_from_lower_bounded {
($source:ty, $($target:ty),*) => {$(
#[unstable(feature = "try_from", issue = "33417")]
#[stable(feature = "try_from", since = "1.26.0")]
impl TryFrom<$source> for $target {
type Error = TryFromIntError;

Expand All @@ -3713,7 +3698,7 @@ macro_rules! try_from_lower_bounded {
// unsigned to signed (only positive bound)
macro_rules! try_from_upper_bounded {
($source:ty, $($target:ty),*) => {$(
#[unstable(feature = "try_from", issue = "33417")]
#[stable(feature = "try_from", since = "1.26.0")]
impl TryFrom<$source> for $target {
type Error = TryFromIntError;

Expand All @@ -3732,7 +3717,7 @@ macro_rules! try_from_upper_bounded {
// all other cases
macro_rules! try_from_both_bounded {
($source:ty, $($target:ty),*) => {$(
#[unstable(feature = "try_from", issue = "33417")]
#[stable(feature = "try_from", since = "1.26.0")]
impl TryFrom<$source> for $target {
type Error = TryFromIntError;

Expand Down Expand Up @@ -3789,82 +3774,44 @@ try_from_both_bounded!(i128, u64, u32, u16, u8);
try_from_upper_bounded!(usize, isize);
try_from_lower_bounded!(isize, usize);

try_from_upper_bounded!(usize, u8);
try_from_upper_bounded!(usize, i8, i16);
try_from_both_bounded!(isize, u8);
try_from_both_bounded!(isize, i8);

#[cfg(target_pointer_width = "16")]
mod ptr_try_from_impls {
use super::TryFromIntError;
use convert::TryFrom;

try_from_upper_bounded!(usize, u8);
try_from_unbounded!(usize, u16, u32, u64, u128);
try_from_upper_bounded!(usize, i8, i16);
try_from_unbounded!(usize, i32, i64, i128);

try_from_both_bounded!(isize, u8);
// Fallible across platfoms, only implementation differs
try_from_lower_bounded!(isize, u16, u32, u64, u128);
try_from_both_bounded!(isize, i8);
try_from_unbounded!(isize, i16, i32, i64, i128);

rev!(try_from_unbounded, usize, u16);
rev!(try_from_upper_bounded, usize, u32, u64, u128);
rev!(try_from_lower_bounded, usize, i8, i16);
rev!(try_from_both_bounded, usize, i32, i64, i128);

rev!(try_from_unbounded, isize, u8);
rev!(try_from_upper_bounded, isize, u16, u32, u64, u128);
rev!(try_from_unbounded, isize, i16);
rev!(try_from_both_bounded, isize, i32, i64, i128);
}

#[cfg(target_pointer_width = "32")]
mod ptr_try_from_impls {
use super::TryFromIntError;
use convert::TryFrom;

try_from_upper_bounded!(usize, u8, u16);
try_from_unbounded!(usize, u32, u64, u128);
try_from_upper_bounded!(usize, i8, i16, i32);
try_from_unbounded!(usize, i64, i128);

try_from_both_bounded!(isize, u8, u16);
// Fallible across platfoms, only implementation differs
try_from_both_bounded!(isize, u16);
try_from_lower_bounded!(isize, u32, u64, u128);
try_from_both_bounded!(isize, i8, i16);
try_from_unbounded!(isize, i32, i64, i128);

rev!(try_from_unbounded, usize, u16, u32);
rev!(try_from_upper_bounded, usize, u64, u128);
rev!(try_from_lower_bounded, usize, i8, i16, i32);
rev!(try_from_both_bounded, usize, i64, i128);

rev!(try_from_unbounded, isize, u8, u16);
rev!(try_from_upper_bounded, isize, u32, u64, u128);
rev!(try_from_unbounded, isize, i16, i32);
rev!(try_from_both_bounded, isize, i64, i128);
}

#[cfg(target_pointer_width = "64")]
mod ptr_try_from_impls {
use super::TryFromIntError;
use convert::TryFrom;

try_from_upper_bounded!(usize, u8, u16, u32);
try_from_unbounded!(usize, u64, u128);
try_from_upper_bounded!(usize, i8, i16, i32, i64);
try_from_unbounded!(usize, i128);

try_from_both_bounded!(isize, u8, u16, u32);
// Fallible across platfoms, only implementation differs
try_from_both_bounded!(isize, u16, u32);
try_from_lower_bounded!(isize, u64, u128);
try_from_both_bounded!(isize, i8, i16, i32);
try_from_unbounded!(isize, i64, i128);

rev!(try_from_unbounded, usize, u16, u32, u64);
rev!(try_from_upper_bounded, usize, u128);
rev!(try_from_lower_bounded, usize, i8, i16, i32, i64);
rev!(try_from_both_bounded, usize, i128);

rev!(try_from_unbounded, isize, u8, u16, u32);
rev!(try_from_upper_bounded, isize, u64, u128);
rev!(try_from_unbounded, isize, i16, i32, i64);
rev!(try_from_both_bounded, isize, i128);
}

#[doc(hidden)]
Expand Down Expand Up @@ -4074,6 +4021,20 @@ impl_from! { u32, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]
impl_from! { u32, i128, #[stable(feature = "i128", since = "1.26.0")] }
impl_from! { u64, i128, #[stable(feature = "i128", since = "1.26.0")] }

// The C99 standard defines bounds on INTPTR_MIN, INTPTR_MAX, and UINTPTR_MAX
// which imply that pointer-sized integers must be at least 16 bits:
// https://port70.net/~nsz/c/c99/n1256.html#7.18.2.4
impl_from! { u16, usize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
impl_from! { u8, isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
impl_from! { i16, isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }

// RISC-V defines the possibility of a 128-bit address space (RV128).

// CHERI proposes 256-bit “capabilities”. Unclear if this would be relevant to usize/isize.
// https://www.cl.cam.ac.uk/research/security/ctsrd/pdfs/20171017a-cheri-poster.pdf
// http://www.csl.sri.com/users/neumann/2012resolve-cheri.pdf


// Note: integers can only be represented with full precision in a float if
// they fit in the significand, which is 24 bits in f32 and 53 bits in f64.
// Lossy float conversions are not implemented at this time.
Expand Down
3 changes: 3 additions & 0 deletions src/libcore/prelude/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ pub use cmp::{PartialEq, PartialOrd, Eq, Ord};
#[stable(feature = "core_prelude", since = "1.4.0")]
#[doc(no_inline)]
pub use convert::{AsRef, AsMut, Into, From};
#[stable(feature = "try_from", since = "1.26.0")]
#[doc(no_inline)]
pub use convert::{TryFrom, TryInto};
#[stable(feature = "core_prelude", since = "1.4.0")]
#[doc(no_inline)]
pub use default::Default;
Expand Down
1 change: 0 additions & 1 deletion src/libcore/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#![feature(step_trait)]
#![feature(test)]
#![feature(trusted_len)]
#![feature(try_from)]
#![feature(try_trait)]
#![feature(exact_chunks)]
#![feature(atomic_nand)]
Expand Down
Loading