Skip to content

Commit 2f71203

Browse files
committed
Use convert::Infallible instead of never in the blanket TryFrom impl
1 parent 85f13f0 commit 2f71203

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/libcore/convert.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ impl<T, U> TryInto<U> for T where U: TryFrom<T>
467467
// with an uninhabited error type.
468468
#[unstable(feature = "try_from", issue = "33417")]
469469
impl<T, U> TryFrom<U> for T where U: Into<T> {
470-
type Error = !;
470+
type Error = Infallible;
471471

472472
fn try_from(value: U) -> Result<Self, Self::Error> {
473473
Ok(U::into(value))

src/libcore/num/mod.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
#![stable(feature = "rust1", since = "1.0.0")]
44

5-
use convert::TryFrom;
5+
use convert::{TryFrom, Infallible};
66
use fmt;
77
use intrinsics;
88
use mem;
@@ -4531,9 +4531,19 @@ impl fmt::Display for TryFromIntError {
45314531
}
45324532

45334533
#[unstable(feature = "try_from", issue = "33417")]
4534+
impl From<Infallible> for TryFromIntError {
4535+
fn from(x: Infallible) -> TryFromIntError {
4536+
match x {}
4537+
}
4538+
}
4539+
4540+
#[unstable(feature = "never_type", issue = "35121")]
45344541
impl From<!> for TryFromIntError {
45354542
fn from(never: !) -> TryFromIntError {
4536-
never
4543+
// Match rather than coerce to make sure that code like
4544+
// `From<Infallible> for TryFromIntError` above will keep working
4545+
// when `Infallible` becomes an alias to `!`.
4546+
match never {}
45374547
}
45384548
}
45394549

src/test/run-pass/try_from.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#![feature(try_from, never_type)]
88

9-
use std::convert::TryInto;
9+
use std::convert::{TryInto, Infallible};
1010

1111
struct Foo<T> {
1212
t: T,
@@ -32,6 +32,6 @@ impl<T> Into<Vec<T>> for Foo<T> {
3232
}
3333

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

0 commit comments

Comments
 (0)