diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs index b31b71aef65..06344ed5ff7 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 6fb67ea9c9a..ae7f1fe98c9 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 4522ce3a8d6..797e7937045 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(); }