diff --git a/library/core/src/convert/mod.rs b/library/core/src/convert/mod.rs index 540b4aa0efd..7e675815edb 100644 --- a/library/core/src/convert/mod.rs +++ b/library/core/src/convert/mod.rs @@ -34,9 +34,6 @@ #![stable(feature = "rust1", since = "1.0.0")] -use crate::error::Error; -use crate::fmt; -use crate::hash::{Hash, Hasher}; use crate::marker::PointeeSized; mod num; @@ -872,7 +869,7 @@ const impl AsMut for str { /// The error type for errors that can never happen. /// -/// Since this enum has no variant, a value of this type can never actually exist. +/// Since this is an alias to the never type, a value of this type can never actually exist. /// This can be useful for generic APIs that use [`Result`] and parameterize the error type, /// to indicate that the result is always [`Ok`]. /// @@ -889,101 +886,7 @@ const impl AsMut 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-fledged type: in the position of a function’s return type. -/// Specifically, it is possible to have implementations for two different function pointer types: -/// -/// ``` -/// trait MyTrait {} -/// impl MyTrait for fn() -> ! {} -/// impl MyTrait for fn() -> std::convert::Infallible {} -/// ``` -/// -/// 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. +/// Note: since CURRENT_RUSTC_VERSION this is an alias to `!`. If targeting that or future versions, +/// prefer using the never type directly. #[stable(feature = "convert_infallible", since = "1.34.0")] -#[derive(Copy)] -pub enum Infallible {} - -#[stable(feature = "convert_infallible", since = "1.34.0")] -#[rustc_const_unstable(feature = "const_clone", issue = "142757")] -const 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 = "str_parse_error2", since = "1.8.0")] -impl Error for Infallible {} - -#[stable(feature = "convert_infallible", since = "1.34.0")] -#[rustc_const_unstable(feature = "const_cmp", issue = "143800")] -const impl PartialEq for Infallible { - fn eq(&self, _: &Infallible) -> bool { - match *self {} - } -} - -#[stable(feature = "convert_infallible", since = "1.34.0")] -#[rustc_const_unstable(feature = "const_cmp", issue = "143800")] -const impl Eq for Infallible {} - -#[stable(feature = "convert_infallible", since = "1.34.0")] -#[rustc_const_unstable(feature = "const_cmp", issue = "143800")] -const impl PartialOrd for Infallible { - fn partial_cmp(&self, _other: &Self) -> Option { - match *self {} - } -} - -#[stable(feature = "convert_infallible", since = "1.34.0")] -#[rustc_const_unstable(feature = "const_cmp", issue = "143800")] -const impl Ord for Infallible { - fn cmp(&self, _other: &Self) -> crate::cmp::Ordering { - match *self {} - } -} - -#[stable(feature = "convert_infallible", since = "1.34.0")] -#[rustc_const_unstable(feature = "const_convert", issue = "143773")] -const impl From for Infallible { - #[inline] - fn from(x: !) -> Self { - x - } -} - -#[stable(feature = "convert_infallible_hash", since = "1.44.0")] -impl Hash for Infallible { - fn hash(&self, _: &mut H) { - match *self {} - } -} +pub type Infallible = !; diff --git a/library/core/src/num/error.rs b/library/core/src/num/error.rs index 45d65a64dc3..d102c456f3b 100644 --- a/library/core/src/num/error.rs +++ b/library/core/src/num/error.rs @@ -1,6 +1,5 @@ //! Error types for conversion to integral types. -use crate::convert::Infallible; use crate::error::Error; use crate::fmt; @@ -35,23 +34,12 @@ impl fmt::Display for TryFromIntError { #[stable(feature = "try_from", since = "1.34.0")] impl Error for TryFromIntError {} -#[stable(feature = "try_from", since = "1.34.0")] -#[rustc_const_unstable(feature = "const_convert", issue = "143773")] -const impl From for TryFromIntError { - fn from(x: Infallible) -> TryFromIntError { - match x {} - } -} - #[stable(feature = "never_type", since = "CURRENT_RUSTC_VERSION")] #[rustc_const_unstable(feature = "const_convert", issue = "143773")] const impl From for TryFromIntError { #[inline] 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 {} + never } } diff --git a/library/core/src/ops/try_trait.rs b/library/core/src/ops/try_trait.rs index f7486cddaae..614b104d9c2 100644 --- a/library/core/src/ops/try_trait.rs +++ b/library/core/src/ops/try_trait.rs @@ -229,7 +229,7 @@ pub const trait Try: [const] FromResidual { all( from_desugaring = "QuestionMark", Self = "core::result::Result", - R = "core::option::Option", + R = "core::option::Option", ), message = "the `?` operator can only be used on `Result`s, not `Option`s, \ in {ItemContext} that returns `Result`", diff --git a/library/std/src/process.rs b/library/std/src/process.rs index a4c461737b6..6414a235698 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -162,7 +162,6 @@ ))] mod tests; -use crate::convert::Infallible; use crate::ffi::OsStr; use crate::io::prelude::*; use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut}; @@ -2700,13 +2699,6 @@ impl Termination for ! { } } -#[stable(feature = "termination_trait_lib", since = "1.61.0")] -impl Termination for Infallible { - fn report(self) -> ExitCode { - match self {} - } -} - #[stable(feature = "termination_trait_lib", since = "1.61.0")] impl Termination for ExitCode { #[inline]