From 089229a1935fa9795cfdefa518c8f8c3beb66db8 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 13 Oct 2019 04:58:45 +0200 Subject: [PATCH] Redefine `core::convert::Infallible` as `!`. --- src/libcore/convert.rs | 95 ++++-------------------------------------- src/libcore/num/mod.rs | 12 +----- src/libstd/error.rs | 7 ---- 3 files changed, 8 insertions(+), 106 deletions(-) diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs index e16c384026045..89a269bdb8eb9 100644 --- a/src/libcore/convert.rs +++ b/src/libcore/convert.rs @@ -40,8 +40,6 @@ #![stable(feature = "rust1", since = "1.0.0")] -use crate::fmt; - /// The identity function. /// /// Two things are important to note about this function: @@ -426,9 +424,7 @@ pub trait TryInto: Sized { /// - `TryFrom for U` implies [`TryInto`]` for T` /// - [`try_from`] is reflexive, which means that `TryFrom for T` /// is implemented and cannot fail -- the associated `Error` type for -/// calling `T::try_from()` on a value of type `T` is [`Infallible`]. -/// When the [`!`] type is stabilized [`Infallible`] and [`!`] will be -/// equivalent. +/// calling `T::try_from()` on a value of type `T` is [`!`]. /// /// `TryFrom` can be implemented as follows: /// @@ -477,7 +473,6 @@ pub trait TryInto: Sized { /// [`TryInto`]: trait.TryInto.html /// [`i32::MAX`]: ../../std/i32/constant.MAX.html /// [`!`]: ../../std/primitive.never.html -/// [`Infallible`]: enum.Infallible.html #[stable(feature = "try_from", since = "1.34.0")] pub trait TryFrom: Sized { /// The type returned in the event of a conversion error. @@ -615,9 +610,9 @@ impl AsRef for str { // THE NO-ERROR ERROR TYPE //////////////////////////////////////////////////////////////////////////////// -/// The error type for errors that can never happen. +/// A type alias for [the `!` “never” type][never]. /// -/// Since this enum has no variant, a value of this type can never actually exist. +/// `Infallible` represents types of errors that can never happen since `!` has no valid values. /// This can be useful for generic APIs that use [`Result`] and parameterize the error type, /// to indicate that the result is always [`Ok`]. /// @@ -634,33 +629,10 @@ impl AsRef for str { /// } /// ``` /// -/// # Future compatibility -/// -/// This enum has the same role as [the `!` “never” type][never], -/// which is unstable in this version of Rust. -/// When `!` is stabilized, we plan to make `Infallible` a type alias to it: -/// -/// ```ignore (illustrates future std change) -/// pub type Infallible = !; -/// ``` -/// -/// … and eventually deprecate `Infallible`. -/// -/// -/// However there is one case where `!` syntax can be used -/// before `!` is stabilized as a full-fleged type: in the position of a function’s return type. -/// Specifically, it is possible implementations for two different function pointer types: -/// -/// ``` -/// trait MyTrait {} -/// impl MyTrait for fn() -> ! {} -/// impl MyTrait for fn() -> std::convert::Infallible {} -/// ``` +/// # Eventual deprecation /// -/// With `Infallible` being an enum, this code is valid. -/// However when `Infallible` becomes an alias for the never type, -/// the two `impl`s will start to overlap -/// and therefore will be disallowed by the language’s trait coherence rules. +/// Previously, `Infallible` was defined as `enum Infallible {}`. +/// Now that it is merely a type alias to `!`, we will eventually deprecate `Infallible`. /// /// [`Ok`]: ../result/enum.Result.html#variant.Ok /// [`Result`]: ../result/enum.Result.html @@ -668,57 +640,4 @@ impl AsRef for str { /// [`Into`]: trait.Into.html /// [never]: ../../std/primitive.never.html #[stable(feature = "convert_infallible", since = "1.34.0")] -#[derive(Copy)] -pub enum Infallible {} - -#[stable(feature = "convert_infallible", since = "1.34.0")] -impl Clone for Infallible { - fn clone(&self) -> Infallible { - match *self {} - } -} - -#[stable(feature = "convert_infallible", since = "1.34.0")] -impl fmt::Debug for Infallible { - fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self {} - } -} - -#[stable(feature = "convert_infallible", since = "1.34.0")] -impl fmt::Display for Infallible { - fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self {} - } -} - -#[stable(feature = "convert_infallible", since = "1.34.0")] -impl PartialEq for Infallible { - fn eq(&self, _: &Infallible) -> bool { - match *self {} - } -} - -#[stable(feature = "convert_infallible", since = "1.34.0")] -impl Eq for Infallible {} - -#[stable(feature = "convert_infallible", since = "1.34.0")] -impl PartialOrd for Infallible { - fn partial_cmp(&self, _other: &Self) -> Option { - match *self {} - } -} - -#[stable(feature = "convert_infallible", since = "1.34.0")] -impl Ord for Infallible { - fn cmp(&self, _other: &Self) -> crate::cmp::Ordering { - match *self {} - } -} - -#[stable(feature = "convert_infallible", since = "1.34.0")] -impl From for Infallible { - fn from(x: !) -> Self { - x - } -} +pub type Infallible = !; diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index c2f85b9535e78..4313248f2636a 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -4,7 +4,7 @@ #![stable(feature = "rust1", since = "1.0.0")] -use crate::convert::{TryFrom, Infallible}; +use crate::convert::TryFrom; use crate::fmt; use crate::intrinsics; use crate::mem; @@ -4722,18 +4722,8 @@ impl fmt::Display for TryFromIntError { } #[stable(feature = "try_from", since = "1.34.0")] -impl From for TryFromIntError { - fn from(x: Infallible) -> TryFromIntError { - match x {} - } -} - -#[stable(feature = "never_type", since = "1.41.0")] impl From for TryFromIntError { fn from(never: !) -> TryFromIntError { - // Match rather than coerce to make sure that code like - // `From for TryFromIntError` above will keep working - // when `Infallible` becomes an alias to `!`. match never {} } } diff --git a/src/libstd/error.rs b/src/libstd/error.rs index 45f1160ca794d..74a9e7c9e331f 100644 --- a/src/libstd/error.rs +++ b/src/libstd/error.rs @@ -551,13 +551,6 @@ impl Error for string::FromUtf16Error { } } -#[stable(feature = "str_parse_error2", since = "1.8.0")] -impl Error for string::ParseError { - fn description(&self) -> &str { - match *self {} - } -} - #[stable(feature = "decode_utf16", since = "1.9.0")] impl Error for char::DecodeUtf16Error { fn description(&self) -> &str {