Skip to content
Permalink
Browse files

Use convert::Infallible instead of never in the blanket TryFrom impl

  • Loading branch information...
SimonSapin committed Feb 8, 2019
1 parent 85f13f0 commit 2f7120397f5178fd3b389c2551a03991f3f4ee31
Showing with 15 additions and 5 deletions.
  1. +1 −1 src/libcore/convert.rs
  2. +12 −2 src/libcore/num/mod.rs
  3. +2 −2 src/test/run-pass/try_from.rs
@@ -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))
@@ -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<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 {}
}
}

@@ -6,7 +6,7 @@

#![feature(try_from, never_type)]

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

struct Foo<T> {
t: T,
@@ -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.
You can’t perform that action at this time.