From 85f13f0d42fe7ee0a5850a271e8a1c975ad5b85f Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Fri, 8 Feb 2019 14:54:22 +0100 Subject: [PATCH 1/5] Add a convert::Infallible empty enum, make string::ParseError an alias --- src/liballoc/string.rs | 37 ++--------------- src/libcore/convert.rs | 93 ++++++++++++++++++++++++++++++++++++++++++ src/libstd/path.rs | 3 +- 3 files changed, 97 insertions(+), 36 deletions(-) diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs index 84c35c6f1bd2b..b714df5d36b6a 100644 --- a/src/liballoc/string.rs +++ b/src/liballoc/string.rs @@ -486,7 +486,7 @@ impl String { /// [`str::from_utf8`]: ../../std/str/fn.from_utf8.html /// [`as_bytes`]: struct.String.html#method.as_bytes /// [`FromUtf8Error`]: struct.FromUtf8Error.html - /// [`Err`]: ../../stdresult/enum.Result.html#variant.Err + /// [`Err`]: ../../std/result/enum.Result.html#variant.Err #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn from_utf8(vec: Vec) -> Result { @@ -2073,48 +2073,17 @@ impl ops::DerefMut for String { /// [`String`]: struct.String.html /// [`from_str`]: ../../std/str/trait.FromStr.html#tymethod.from_str #[stable(feature = "str_parse_error", since = "1.5.0")] -#[derive(Copy)] -pub enum ParseError {} +pub type ParseError = core::convert::Infallible; #[stable(feature = "rust1", since = "1.0.0")] impl FromStr for String { - type Err = ParseError; + type Err = core::convert::Infallible; #[inline] fn from_str(s: &str) -> Result { Ok(String::from(s)) } } -#[stable(feature = "str_parse_error", since = "1.5.0")] -impl Clone for ParseError { - fn clone(&self) -> ParseError { - match *self {} - } -} - -#[stable(feature = "str_parse_error", since = "1.5.0")] -impl fmt::Debug for ParseError { - fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self {} - } -} - -#[stable(feature = "str_parse_error2", since = "1.8.0")] -impl fmt::Display for ParseError { - fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self {} - } -} - -#[stable(feature = "str_parse_error", since = "1.5.0")] -impl PartialEq for ParseError { - fn eq(&self, _: &ParseError) -> bool { - match *self {} - } -} - -#[stable(feature = "str_parse_error", since = "1.5.0")] -impl Eq for ParseError {} /// A trait for converting a value to a `String`. /// diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs index 4a7c6e15a4df1..b31b71aef6585 100644 --- a/src/libcore/convert.rs +++ b/src/libcore/convert.rs @@ -499,3 +499,96 @@ impl AsRef for str { self } } + +//////////////////////////////////////////////////////////////////////////////// +// THE NO-ERROR ERROR TYPE +//////////////////////////////////////////////////////////////////////////////// + +/// The error type for errors that can never happen. +/// +/// Since this enum has no variant, 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`]. +/// +/// For example, the [`TryFrom`] trait (conversion that returns a [`Result`]) +/// has a blanket implementation for all types where a reverse [`Into`] implementation exists. +/// +/// ```ignore (illustrates std code, duplicating the impl in a doctest would be an error) +/// impl TryFrom for T where U: Into { +/// type Error = Infallible; +/// +/// fn try_from(value: U) -> Result { +/// Ok(U::into(value)) // Never returns `Err` +/// } +/// } +/// ``` +/// +/// # 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 {} +/// ``` +/// +/// 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. +/// +/// [`Ok`]: ../result/enum.Result.html#variant.Ok +/// [`Result`]: ../result/enum.Result.html +/// [`TryFrom`]: trait.TryFrom.html +/// [`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 {} + } +} + +use fmt; + +#[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 {} diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 0f1d627fa1e85..d9199666e5831 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -78,7 +78,6 @@ use iter::{self, FusedIterator}; use ops::{self, Deref}; use rc::Rc; use str::FromStr; -use string::ParseError; use sync::Arc; use ffi::{OsStr, OsString}; @@ -1453,7 +1452,7 @@ impl From for PathBuf { #[stable(feature = "path_from_str", since = "1.32.0")] impl FromStr for PathBuf { - type Err = ParseError; + type Err = core::convert::Infallible; fn from_str(s: &str) -> Result { Ok(PathBuf::from(s)) From 2f7120397f5178fd3b389c2551a03991f3f4ee31 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Fri, 8 Feb 2019 14:55:44 +0100 Subject: [PATCH 2/5] Use convert::Infallible instead of never in the blanket TryFrom impl --- src/libcore/convert.rs | 2 +- src/libcore/num/mod.rs | 14 ++++++++++++-- src/test/run-pass/try_from.rs | 4 ++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs index b31b71aef6585..06344ed5ff768 100644 --- a/src/libcore/convert.rs +++ b/src/libcore/convert.rs @@ -467,7 +467,7 @@ impl TryInto for T where U: TryFrom // with an uninhabited error type. #[unstable(feature = "try_from", issue = "33417")] impl TryFrom for T where U: Into { - type Error = !; + type Error = Infallible; fn try_from(value: U) -> Result { Ok(U::into(value)) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 6fb67ea9c9acb..ae7f1fe98c994 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -2,7 +2,7 @@ #![stable(feature = "rust1", since = "1.0.0")] -use convert::TryFrom; +use convert::{TryFrom, Infallible}; use fmt; use intrinsics; use mem; @@ -4531,9 +4531,19 @@ impl fmt::Display for TryFromIntError { } #[unstable(feature = "try_from", issue = "33417")] +impl From for TryFromIntError { + fn from(x: Infallible) -> TryFromIntError { + match x {} + } +} + +#[unstable(feature = "never_type", issue = "35121")] impl From for TryFromIntError { fn from(never: !) -> TryFromIntError { - never + // 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/test/run-pass/try_from.rs b/src/test/run-pass/try_from.rs index 4522ce3a8d617..797e7937045cd 100644 --- a/src/test/run-pass/try_from.rs +++ b/src/test/run-pass/try_from.rs @@ -6,7 +6,7 @@ #![feature(try_from, never_type)] -use std::convert::TryInto; +use std::convert::{TryInto, Infallible}; struct Foo { t: T, @@ -32,6 +32,6 @@ impl Into> for Foo { } pub fn main() { - let _: Result, !> = Foo { t: 10 }.try_into(); + let _: Result, Infallible> = Foo { t: 10 }.try_into(); } From c80a8f51dcdc90dd8a5234f3bef6160814eee5df Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Fri, 8 Feb 2019 15:00:47 +0100 Subject: [PATCH 3/5] Stabilize TryFrom and TryInto --- src/libcore/array.rs | 8 ++++---- src/libcore/char/convert.rs | 6 +++--- src/libcore/char/mod.rs | 2 +- src/libcore/convert.rs | 12 +++++++---- src/libcore/num/mod.rs | 20 +++++++------------ src/libcore/tests/lib.rs | 1 - src/librustc_apfloat/lib.rs | 1 - src/librustc_mir/lib.rs | 1 - src/libstd/error.rs | 6 +++--- src/libstd/lib.rs | 1 - .../run-pass/try-from-int-error-partial-eq.rs | 1 - src/test/run-pass/try_from.rs | 2 +- src/test/ui/e0119/conflict-with-std.rs | 1 - src/test/ui/e0119/conflict-with-std.stderr | 6 +++--- 14 files changed, 30 insertions(+), 38 deletions(-) diff --git a/src/libcore/array.rs b/src/libcore/array.rs index 3a27a39af4ace..9c6ecc4350246 100644 --- a/src/libcore/array.rs +++ b/src/libcore/array.rs @@ -49,7 +49,7 @@ unsafe impl> FixedSizeArray 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.34.0")] #[derive(Debug, Copy, Clone)] pub struct TryFromSliceError(()); @@ -138,7 +138,7 @@ macro_rules! array_impls { } } - #[unstable(feature = "try_from", issue = "33417")] + #[stable(feature = "try_from", since = "1.34.0")] impl<'a, T> TryFrom<&'a [T]> for [T; $N] where T: Copy { type Error = TryFromSliceError; @@ -147,7 +147,7 @@ macro_rules! array_impls { } } - #[unstable(feature = "try_from", issue = "33417")] + #[stable(feature = "try_from", since = "1.34.0")] impl<'a, T> TryFrom<&'a [T]> for &'a [T; $N] { type Error = TryFromSliceError; @@ -161,7 +161,7 @@ macro_rules! array_impls { } } - #[unstable(feature = "try_from", issue = "33417")] + #[stable(feature = "try_from", since = "1.34.0")] impl<'a, T> TryFrom<&'a mut [T]> for &'a mut [T; $N] { type Error = TryFromSliceError; diff --git a/src/libcore/char/convert.rs b/src/libcore/char/convert.rs index 4a1a236b669f0..6a5abfb408f5b 100644 --- a/src/libcore/char/convert.rs +++ b/src/libcore/char/convert.rs @@ -218,7 +218,7 @@ impl FromStr for char { } -#[unstable(feature = "try_from", issue = "33417")] +#[stable(feature = "try_from", since = "1.34.0")] impl TryFrom for char { type Error = CharTryFromError; @@ -233,11 +233,11 @@ impl TryFrom 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.34.0")] #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct CharTryFromError(()); -#[unstable(feature = "try_from", issue = "33417")] +#[stable(feature = "try_from", since = "1.34.0")] impl fmt::Display for CharTryFromError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { "converted integer out of range for `char`".fmt(f) diff --git a/src/libcore/char/mod.rs b/src/libcore/char/mod.rs index 15e153bdfada2..f3369c4d94010 100644 --- a/src/libcore/char/mod.rs +++ b/src/libcore/char/mod.rs @@ -30,7 +30,7 @@ pub use self::convert::{from_u32, from_digit}; pub use self::convert::from_u32_unchecked; #[stable(feature = "char_from_str", since = "1.20.0")] pub use self::convert::ParseCharError; -#[unstable(feature = "try_from", issue = "33417")] +#[stable(feature = "try_from", since = "1.34.0")] pub use self::convert::CharTryFromError; #[stable(feature = "decode_utf16", since = "1.9.0")] pub use self::decode::{decode_utf16, DecodeUtf16, DecodeUtf16Error}; diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs index 06344ed5ff768..65aa91e2dab09 100644 --- a/src/libcore/convert.rs +++ b/src/libcore/convert.rs @@ -370,22 +370,26 @@ pub trait From: Sized { /// /// [`TryFrom`]: trait.TryFrom.html /// [`Into`]: trait.Into.html -#[unstable(feature = "try_from", issue = "33417")] +#[stable(feature = "try_from", since = "1.34.0")] pub trait TryInto: Sized { /// The type returned in the event of a conversion error. + #[stable(feature = "try_from", since = "1.34.0")] type Error; /// Performs the conversion. + #[stable(feature = "try_from", since = "1.34.0")] fn try_into(self) -> Result; } /// Attempt to construct `Self` via a conversion. -#[unstable(feature = "try_from", issue = "33417")] +#[stable(feature = "try_from", since = "1.34.0")] pub trait TryFrom: Sized { /// The type returned in the event of a conversion error. + #[stable(feature = "try_from", since = "1.34.0")] type Error; /// Performs the conversion. + #[stable(feature = "try_from", since = "1.34.0")] fn try_from(value: T) -> Result; } @@ -453,7 +457,7 @@ impl From for T { // TryFrom implies TryInto -#[unstable(feature = "try_from", issue = "33417")] +#[stable(feature = "try_from", since = "1.34.0")] impl TryInto for T where U: TryFrom { type Error = U::Error; @@ -465,7 +469,7 @@ impl TryInto for T where U: TryFrom // 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.34.0")] impl TryFrom for T where U: Into { type Error = Infallible; diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index ae7f1fe98c994..ffd9e4a5cab05 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -2004,7 +2004,6 @@ assert_eq!(value, ", $swap_op, "); When starting from a slice rather than an array, fallible conversion APIs can be used: ``` -#![feature(try_from)] use std::convert::TryInto; fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " { @@ -2036,7 +2035,6 @@ assert_eq!(value, ", $swap_op, "); When starting from a slice rather than an array, fallible conversion APIs can be used: ``` -#![feature(try_from)] use std::convert::TryInto; fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " { @@ -2078,7 +2076,6 @@ assert_eq!(value, ", $swap_op, "); When starting from a slice rather than an array, fallible conversion APIs can be used: ``` -#![feature(try_from)] use std::convert::TryInto; fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " { @@ -3771,7 +3768,6 @@ assert_eq!(value, ", $swap_op, "); When starting from a slice rather than an array, fallible conversion APIs can be used: ``` -#![feature(try_from)] use std::convert::TryInto; fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " { @@ -3803,7 +3799,6 @@ assert_eq!(value, ", $swap_op, "); When starting from a slice rather than an array, fallible conversion APIs can be used: ``` -#![feature(try_from)] use std::convert::TryInto; fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " { @@ -3845,7 +3840,6 @@ assert_eq!(value, ", $swap_op, "); When starting from a slice rather than an array, fallible conversion APIs can be used: ``` -#![feature(try_from)] use std::convert::TryInto; fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " { @@ -4508,7 +4502,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.34.0")] #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct TryFromIntError(()); @@ -4523,14 +4517,14 @@ impl TryFromIntError { } } -#[unstable(feature = "try_from", issue = "33417")] +#[stable(feature = "try_from", since = "1.34.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.34.0")] impl From for TryFromIntError { fn from(x: Infallible) -> TryFromIntError { match x {} @@ -4550,7 +4544,7 @@ impl From for TryFromIntError { // no possible bounds violation macro_rules! try_from_unbounded { ($source:ty, $($target:ty),*) => {$( - #[unstable(feature = "try_from", issue = "33417")] + #[stable(feature = "try_from", since = "1.34.0")] impl TryFrom<$source> for $target { type Error = TryFromIntError; @@ -4565,7 +4559,7 @@ macro_rules! try_from_unbounded { // 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.34.0")] impl TryFrom<$source> for $target { type Error = TryFromIntError; @@ -4584,7 +4578,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.34.0")] impl TryFrom<$source> for $target { type Error = TryFromIntError; @@ -4603,7 +4597,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.34.0")] impl TryFrom<$source> for $target { type Error = TryFromIntError; diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs index 3e8549f8ae366..06df75b1ee8ff 100644 --- a/src/libcore/tests/lib.rs +++ b/src/libcore/tests/lib.rs @@ -27,7 +27,6 @@ #![feature(str_internals)] #![feature(test)] #![feature(trusted_len)] -#![feature(try_from)] #![feature(try_trait)] #![feature(align_offset)] #![feature(reverse_bits)] diff --git a/src/librustc_apfloat/lib.rs b/src/librustc_apfloat/lib.rs index 46d046d7b41b4..1b0bcdd0b5b48 100644 --- a/src/librustc_apfloat/lib.rs +++ b/src/librustc_apfloat/lib.rs @@ -35,7 +35,6 @@ #![deny(rust_2018_idioms)] #![feature(nll)] -#![feature(try_from)] // See librustc_cratesio_shim/Cargo.toml for a comment explaining this. #[allow(unused_extern_crates)] extern crate rustc_cratesio_shim; diff --git a/src/librustc_mir/lib.rs b/src/librustc_mir/lib.rs index 909f96956695d..bc8e94122273a 100644 --- a/src/librustc_mir/lib.rs +++ b/src/librustc_mir/lib.rs @@ -24,7 +24,6 @@ Rust MIR: a lowered representation of Rust. Also: an experiment! #![feature(unicode_internals)] #![feature(step_trait)] #![feature(slice_concat_ext)] -#![feature(try_from)] #![feature(reverse_bits)] #![feature(try_blocks)] diff --git a/src/libstd/error.rs b/src/libstd/error.rs index 6348b411a4c64..cd930240465cf 100644 --- a/src/libstd/error.rs +++ b/src/libstd/error.rs @@ -466,14 +466,14 @@ impl Error for num::ParseIntError { } } -#[unstable(feature = "try_from", issue = "33417")] +#[stable(feature = "try_from", since = "1.34.0")] impl Error for num::TryFromIntError { fn description(&self) -> &str { self.__description() } } -#[unstable(feature = "try_from", issue = "33417")] +#[stable(feature = "try_from", since = "1.34.0")] impl Error for array::TryFromSliceError { fn description(&self) -> &str { self.__description() @@ -548,7 +548,7 @@ impl Error for cell::BorrowMutError { } } -#[unstable(feature = "try_from", issue = "33417")] +#[stable(feature = "try_from", since = "1.34.0")] impl Error for char::CharTryFromError { fn description(&self) -> &str { "converted integer out of range for `char`" diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index e57cb2ce5fd15..47d2f1132459a 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -281,7 +281,6 @@ #![feature(rustc_private)] #![feature(thread_local)] #![feature(toowned_clone_into)] -#![feature(try_from)] #![feature(try_reserve)] #![feature(unboxed_closures)] #![feature(untagged_unions)] diff --git a/src/test/run-pass/try-from-int-error-partial-eq.rs b/src/test/run-pass/try-from-int-error-partial-eq.rs index 83ede6759257d..e9b53f2d1c8df 100644 --- a/src/test/run-pass/try-from-int-error-partial-eq.rs +++ b/src/test/run-pass/try-from-int-error-partial-eq.rs @@ -1,4 +1,3 @@ -#![feature(try_from)] #![allow(unused_must_use)] use std::convert::TryFrom; diff --git a/src/test/run-pass/try_from.rs b/src/test/run-pass/try_from.rs index 797e7937045cd..e42f2c3e3930d 100644 --- a/src/test/run-pass/try_from.rs +++ b/src/test/run-pass/try_from.rs @@ -4,7 +4,7 @@ // This test was added to show the motivation for doing this // over `TryFrom` being blanket impl for all `T: From` -#![feature(try_from, never_type)] +#![feature(never_type)] use std::convert::{TryInto, Infallible}; diff --git a/src/test/ui/e0119/conflict-with-std.rs b/src/test/ui/e0119/conflict-with-std.rs index e639bc5db5e0f..6dc81f33dfcc4 100644 --- a/src/test/ui/e0119/conflict-with-std.rs +++ b/src/test/ui/e0119/conflict-with-std.rs @@ -1,4 +1,3 @@ -#![feature(try_from)] use std::marker::PhantomData; use std::convert::{TryFrom, AsRef}; diff --git a/src/test/ui/e0119/conflict-with-std.stderr b/src/test/ui/e0119/conflict-with-std.stderr index c2ae321aa5e50..1cfb3c1dabae4 100644 --- a/src/test/ui/e0119/conflict-with-std.stderr +++ b/src/test/ui/e0119/conflict-with-std.stderr @@ -1,5 +1,5 @@ error[E0119]: conflicting implementations of trait `std::convert::AsRef` for type `std::boxed::Box`: - --> $DIR/conflict-with-std.rs:7:1 + --> $DIR/conflict-with-std.rs:6:1 | LL | impl AsRef for Box { //~ ERROR conflicting implementations | ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -9,7 +9,7 @@ LL | impl AsRef for Box { //~ ERROR conflicting implementations where T: ?Sized; error[E0119]: conflicting implementations of trait `std::convert::From` for type `S`: - --> $DIR/conflict-with-std.rs:14:1 + --> $DIR/conflict-with-std.rs:13:1 | LL | impl From for S { //~ ERROR conflicting implementations | ^^^^^^^^^^^^^^^^^^ @@ -18,7 +18,7 @@ LL | impl From for S { //~ ERROR conflicting implementations - impl std::convert::From for T; error[E0119]: conflicting implementations of trait `std::convert::TryFrom` for type `X`: - --> $DIR/conflict-with-std.rs:21:1 + --> $DIR/conflict-with-std.rs:20:1 | LL | impl TryFrom for X { //~ ERROR conflicting implementations | ^^^^^^^^^^^^^^^^^^^^^ From b2cf9a02b2bce6ed984b0c967e2daf0af7b13629 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Wed, 13 Feb 2019 18:01:37 +0100 Subject: [PATCH 4/5] Add `impl From for Infallible` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The reverse conversion unfortunately causes unexpected errors like: ``` error[E0277]: the trait bound `!: std::convert::From<()>` is not satisfied --> src/librustc_metadata/encoder.rs:105:9 | 105 | self.emit_usize(seq.len)?; | ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<()>` is not implemented for `!` | = help: the following implementations were found: > = note: the trait is implemented for `()`. Possibly this error has been caused by changes to Rust's type-inference algorithm (see: https://github.com/rust-lang/rust/issues/48950 for more info). Consider whether you meant to use the type `()` here instead. = note: required by `std::convert::From::from` ``` I don’t understand why this error happens. If I’m reading the code correctly the return types of `emit_usize` and of the method that contains line 105 are both `Result<(), !>`, so the expansion of the `?` operator should involve `!: From`, not `From<()>`. Is this a type inference bug? --- src/libcore/convert.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs index 65aa91e2dab09..c6c41fb99a466 100644 --- a/src/libcore/convert.rs +++ b/src/libcore/convert.rs @@ -596,3 +596,10 @@ impl PartialEq for Infallible { #[stable(feature = "convert_infallible", since = "1.34.0")] impl Eq for Infallible {} + +#[stable(feature = "convert_infallible", since = "1.34.0")] +impl From for Infallible { + fn from(x: !) -> Self { + x + } +} From cf267540ebabdeac1f2045819cd6bac561017e29 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Sun, 17 Feb 2019 21:30:00 +0100 Subject: [PATCH 5/5] Review comments --- src/libcore/convert.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs index c6c41fb99a466..6b059f31d90e1 100644 --- a/src/libcore/convert.rs +++ b/src/libcore/convert.rs @@ -41,6 +41,8 @@ #![stable(feature = "rust1", since = "1.0.0")] +use fmt; + /// An identity function. /// /// Two things are important to note about this function: @@ -571,8 +573,6 @@ impl Clone for Infallible { } } -use fmt; - #[stable(feature = "convert_infallible", since = "1.34.0")] impl fmt::Debug for Infallible { fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -597,6 +597,20 @@ impl PartialEq for Infallible { #[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 {