Skip to content

Commit fc8027f

Browse files
authored
Rollup merge of #97803 - Gankra:term, r=dtolnay
Impl Termination for Infallible and then make the Result impls of Termination more generic This allows things like `Result<ExitCode, E>` to 'just work'
2 parents c3b7d7b + 0894660 commit fc8027f

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed

library/std/src/process.rs

+15-22
Original file line numberDiff line numberDiff line change
@@ -2140,16 +2140,6 @@ impl Termination for () {
21402140
}
21412141
}
21422142

2143-
#[stable(feature = "termination_trait_lib", since = "1.61.0")]
2144-
impl<E: fmt::Debug> Termination for Result<(), E> {
2145-
fn report(self) -> ExitCode {
2146-
match self {
2147-
Ok(()) => ().report(),
2148-
Err(err) => Err::<!, _>(err).report(),
2149-
}
2150-
}
2151-
}
2152-
21532143
#[stable(feature = "termination_trait_lib", since = "1.61.0")]
21542144
impl Termination for ! {
21552145
fn report(self) -> ExitCode {
@@ -2158,28 +2148,31 @@ impl Termination for ! {
21582148
}
21592149

21602150
#[stable(feature = "termination_trait_lib", since = "1.61.0")]
2161-
impl<E: fmt::Debug> Termination for Result<!, E> {
2151+
impl Termination for Infallible {
21622152
fn report(self) -> ExitCode {
2163-
let Err(err) = self;
2164-
// Ignore error if the write fails, for example because stderr is
2165-
// already closed. There is not much point panicking at this point.
2166-
let _ = writeln!(io::stderr(), "Error: {err:?}");
2167-
ExitCode::FAILURE
2153+
match self {}
21682154
}
21692155
}
21702156

21712157
#[stable(feature = "termination_trait_lib", since = "1.61.0")]
2172-
impl<E: fmt::Debug> Termination for Result<Infallible, E> {
2158+
impl Termination for ExitCode {
2159+
#[inline]
21732160
fn report(self) -> ExitCode {
2174-
let Err(err) = self;
2175-
Err::<!, _>(err).report()
2161+
self
21762162
}
21772163
}
21782164

21792165
#[stable(feature = "termination_trait_lib", since = "1.61.0")]
2180-
impl Termination for ExitCode {
2181-
#[inline]
2166+
impl<T: Termination, E: fmt::Debug> Termination for Result<T, E> {
21822167
fn report(self) -> ExitCode {
2183-
self
2168+
match self {
2169+
Ok(val) => val.report(),
2170+
Err(err) => {
2171+
// Ignore error if the write fails, for example because stderr is
2172+
// already closed. There is not much point panicking at this point.
2173+
let _ = writeln!(io::stderr(), "Error: {err:?}");
2174+
ExitCode::FAILURE
2175+
}
2176+
}
21842177
}
21852178
}

src/test/ui/rfc-1937-termination-trait/termination-trait-test-wrong-type.stderr

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0277]: `main` has invalid return type `Result<f32, ParseFloatError>`
1+
error[E0277]: `main` has invalid return type `f32`
22
--> $DIR/termination-trait-test-wrong-type.rs:6:1
33
|
44
LL | #[test]
@@ -8,11 +8,8 @@ LL | | "0".parse()
88
LL | | }
99
| |_^ `main` can only return types that implement `Termination`
1010
|
11-
= help: the trait `Termination` is not implemented for `Result<f32, ParseFloatError>`
12-
= help: the following other types implement trait `Termination`:
13-
Result<!, E>
14-
Result<(), E>
15-
Result<Infallible, E>
11+
= help: the trait `Termination` is not implemented for `f32`
12+
= note: required because of the requirements on the impl of `Termination` for `Result<f32, ParseFloatError>`
1613
note: required by a bound in `assert_test_result`
1714
--> $SRC_DIR/test/src/lib.rs:LL:COL
1815
|

0 commit comments

Comments
 (0)