Skip to content

Commit

Permalink
Use convert::Infallible instead of never in the blanket TryFrom impl
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Feb 13, 2019
1 parent 85f13f0 commit 2f71203
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/libcore/convert.rs
Expand Up @@ -467,7 +467,7 @@ impl<T, U> TryInto<U> for T where U: TryFrom<T>
// with an uninhabited error type.
#[unstable(feature = "try_from", issue = "33417")]
impl<T, U> TryFrom<U> for T where U: Into<T> {
type Error = !;
type Error = Infallible;

fn try_from(value: U) -> Result<Self, Self::Error> {
Ok(U::into(value))
Expand Down
14 changes: 12 additions & 2 deletions src/libcore/num/mod.rs
Expand Up @@ -2,7 +2,7 @@

#![stable(feature = "rust1", since = "1.0.0")]

use convert::TryFrom;
use convert::{TryFrom, Infallible};
use fmt;
use intrinsics;
use mem;
Expand Down Expand Up @@ -4531,9 +4531,19 @@ impl fmt::Display for TryFromIntError {
}

#[unstable(feature = "try_from", issue = "33417")]
impl From<Infallible> 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<Infallible> for TryFromIntError` above will keep working
// when `Infallible` becomes an alias to `!`.
match never {}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/run-pass/try_from.rs
Expand Up @@ -6,7 +6,7 @@

#![feature(try_from, never_type)]

use std::convert::TryInto;
use std::convert::{TryInto, Infallible};

struct Foo<T> {
t: T,
Expand All @@ -32,6 +32,6 @@ impl<T> Into<Vec<T>> for Foo<T> {
}

pub fn main() {
let _: Result<Vec<i32>, !> = Foo { t: 10 }.try_into();
let _: Result<Vec<i32>, Infallible> = Foo { t: 10 }.try_into();
}

0 comments on commit 2f71203

Please sign in to comment.