Skip to content

Commit 089229a

Browse files
committed
Redefine core::convert::Infallible as !.
1 parent 6eb0627 commit 089229a

File tree

3 files changed

+8
-106
lines changed

3 files changed

+8
-106
lines changed

src/libcore/convert.rs

+7-88
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
4141
#![stable(feature = "rust1", since = "1.0.0")]
4242

43-
use crate::fmt;
44-
4543
/// The identity function.
4644
///
4745
/// Two things are important to note about this function:
@@ -426,9 +424,7 @@ pub trait TryInto<T>: Sized {
426424
/// - `TryFrom<T> for U` implies [`TryInto`]`<U> for T`
427425
/// - [`try_from`] is reflexive, which means that `TryFrom<T> for T`
428426
/// is implemented and cannot fail -- the associated `Error` type for
429-
/// calling `T::try_from()` on a value of type `T` is [`Infallible`].
430-
/// When the [`!`] type is stabilized [`Infallible`] and [`!`] will be
431-
/// equivalent.
427+
/// calling `T::try_from()` on a value of type `T` is [`!`].
432428
///
433429
/// `TryFrom<T>` can be implemented as follows:
434430
///
@@ -477,7 +473,6 @@ pub trait TryInto<T>: Sized {
477473
/// [`TryInto`]: trait.TryInto.html
478474
/// [`i32::MAX`]: ../../std/i32/constant.MAX.html
479475
/// [`!`]: ../../std/primitive.never.html
480-
/// [`Infallible`]: enum.Infallible.html
481476
#[stable(feature = "try_from", since = "1.34.0")]
482477
pub trait TryFrom<T>: Sized {
483478
/// The type returned in the event of a conversion error.
@@ -615,9 +610,9 @@ impl AsRef<str> for str {
615610
// THE NO-ERROR ERROR TYPE
616611
////////////////////////////////////////////////////////////////////////////////
617612

618-
/// The error type for errors that can never happen.
613+
/// A type alias for [the `!` “never” type][never].
619614
///
620-
/// Since this enum has no variant, a value of this type can never actually exist.
615+
/// `Infallible` represents types of errors that can never happen since `!` has no valid values.
621616
/// This can be useful for generic APIs that use [`Result`] and parameterize the error type,
622617
/// to indicate that the result is always [`Ok`].
623618
///
@@ -634,91 +629,15 @@ impl AsRef<str> for str {
634629
/// }
635630
/// ```
636631
///
637-
/// # Future compatibility
638-
///
639-
/// This enum has the same role as [the `!` “never” type][never],
640-
/// which is unstable in this version of Rust.
641-
/// When `!` is stabilized, we plan to make `Infallible` a type alias to it:
642-
///
643-
/// ```ignore (illustrates future std change)
644-
/// pub type Infallible = !;
645-
/// ```
646-
///
647-
/// … and eventually deprecate `Infallible`.
648-
///
649-
///
650-
/// However there is one case where `!` syntax can be used
651-
/// before `!` is stabilized as a full-fleged type: in the position of a function’s return type.
652-
/// Specifically, it is possible implementations for two different function pointer types:
653-
///
654-
/// ```
655-
/// trait MyTrait {}
656-
/// impl MyTrait for fn() -> ! {}
657-
/// impl MyTrait for fn() -> std::convert::Infallible {}
658-
/// ```
632+
/// # Eventual deprecation
659633
///
660-
/// With `Infallible` being an enum, this code is valid.
661-
/// However when `Infallible` becomes an alias for the never type,
662-
/// the two `impl`s will start to overlap
663-
/// and therefore will be disallowed by the language’s trait coherence rules.
634+
/// Previously, `Infallible` was defined as `enum Infallible {}`.
635+
/// Now that it is merely a type alias to `!`, we will eventually deprecate `Infallible`.
664636
///
665637
/// [`Ok`]: ../result/enum.Result.html#variant.Ok
666638
/// [`Result`]: ../result/enum.Result.html
667639
/// [`TryFrom`]: trait.TryFrom.html
668640
/// [`Into`]: trait.Into.html
669641
/// [never]: ../../std/primitive.never.html
670642
#[stable(feature = "convert_infallible", since = "1.34.0")]
671-
#[derive(Copy)]
672-
pub enum Infallible {}
673-
674-
#[stable(feature = "convert_infallible", since = "1.34.0")]
675-
impl Clone for Infallible {
676-
fn clone(&self) -> Infallible {
677-
match *self {}
678-
}
679-
}
680-
681-
#[stable(feature = "convert_infallible", since = "1.34.0")]
682-
impl fmt::Debug for Infallible {
683-
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
684-
match *self {}
685-
}
686-
}
687-
688-
#[stable(feature = "convert_infallible", since = "1.34.0")]
689-
impl fmt::Display for Infallible {
690-
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
691-
match *self {}
692-
}
693-
}
694-
695-
#[stable(feature = "convert_infallible", since = "1.34.0")]
696-
impl PartialEq for Infallible {
697-
fn eq(&self, _: &Infallible) -> bool {
698-
match *self {}
699-
}
700-
}
701-
702-
#[stable(feature = "convert_infallible", since = "1.34.0")]
703-
impl Eq for Infallible {}
704-
705-
#[stable(feature = "convert_infallible", since = "1.34.0")]
706-
impl PartialOrd for Infallible {
707-
fn partial_cmp(&self, _other: &Self) -> Option<crate::cmp::Ordering> {
708-
match *self {}
709-
}
710-
}
711-
712-
#[stable(feature = "convert_infallible", since = "1.34.0")]
713-
impl Ord for Infallible {
714-
fn cmp(&self, _other: &Self) -> crate::cmp::Ordering {
715-
match *self {}
716-
}
717-
}
718-
719-
#[stable(feature = "convert_infallible", since = "1.34.0")]
720-
impl From<!> for Infallible {
721-
fn from(x: !) -> Self {
722-
x
723-
}
724-
}
643+
pub type Infallible = !;

src/libcore/num/mod.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
#![stable(feature = "rust1", since = "1.0.0")]
66

7-
use crate::convert::{TryFrom, Infallible};
7+
use crate::convert::TryFrom;
88
use crate::fmt;
99
use crate::intrinsics;
1010
use crate::mem;
@@ -4722,18 +4722,8 @@ impl fmt::Display for TryFromIntError {
47224722
}
47234723

47244724
#[stable(feature = "try_from", since = "1.34.0")]
4725-
impl From<Infallible> for TryFromIntError {
4726-
fn from(x: Infallible) -> TryFromIntError {
4727-
match x {}
4728-
}
4729-
}
4730-
4731-
#[stable(feature = "never_type", since = "1.41.0")]
47324725
impl From<!> for TryFromIntError {
47334726
fn from(never: !) -> TryFromIntError {
4734-
// Match rather than coerce to make sure that code like
4735-
// `From<Infallible> for TryFromIntError` above will keep working
4736-
// when `Infallible` becomes an alias to `!`.
47374727
match never {}
47384728
}
47394729
}

src/libstd/error.rs

-7
Original file line numberDiff line numberDiff line change
@@ -551,13 +551,6 @@ impl Error for string::FromUtf16Error {
551551
}
552552
}
553553

554-
#[stable(feature = "str_parse_error2", since = "1.8.0")]
555-
impl Error for string::ParseError {
556-
fn description(&self) -> &str {
557-
match *self {}
558-
}
559-
}
560-
561554
#[stable(feature = "decode_utf16", since = "1.9.0")]
562555
impl Error for char::DecodeUtf16Error {
563556
fn description(&self) -> &str {

0 commit comments

Comments
 (0)