Make Infallible = !

This commit is contained in:
Waffle Lapkin
2025-08-15 02:10:40 +02:00
parent 39c6cacb34
commit cfc82ebbfc
4 changed files with 6 additions and 123 deletions

View File

@@ -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<str> 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<str> 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<crate::cmp::Ordering> {
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<H: Hasher>(&self, _: &mut H) {
match *self {}
}
}
pub type Infallible = !;

View File

@@ -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<Infallible> 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<Infallible> for TryFromIntError` above will keep working
// when `Infallible` becomes an alias to `!`.
match never {}
never
}
}

View File

@@ -229,7 +229,7 @@ pub const trait Try: [const] FromResidual {
all(
from_desugaring = "QuestionMark",
Self = "core::result::Result<T, E>",
R = "core::option::Option<core::convert::Infallible>",
R = "core::option::Option<!>",
),
message = "the `?` operator can only be used on `Result`s, not `Option`s, \
in {ItemContext} that returns `Result`",

View File

@@ -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]