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'
This commit is contained in:
Yuki Okushi 2022-06-18 10:03:22 +09:00 committed by GitHub
commit fc8027f188
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 30 deletions

View File

@ -2140,16 +2140,6 @@ impl Termination for () {
}
}
#[stable(feature = "termination_trait_lib", since = "1.61.0")]
impl<E: fmt::Debug> Termination for Result<(), E> {
fn report(self) -> ExitCode {
match self {
Ok(()) => ().report(),
Err(err) => Err::<!, _>(err).report(),
}
}
}
#[stable(feature = "termination_trait_lib", since = "1.61.0")]
impl Termination for ! {
fn report(self) -> ExitCode {
@ -2158,21 +2148,9 @@ impl Termination for ! {
}
#[stable(feature = "termination_trait_lib", since = "1.61.0")]
impl<E: fmt::Debug> Termination for Result<!, E> {
impl Termination for Infallible {
fn report(self) -> ExitCode {
let Err(err) = self;
// Ignore error if the write fails, for example because stderr is
// already closed. There is not much point panicking at this point.
let _ = writeln!(io::stderr(), "Error: {err:?}");
ExitCode::FAILURE
}
}
#[stable(feature = "termination_trait_lib", since = "1.61.0")]
impl<E: fmt::Debug> Termination for Result<Infallible, E> {
fn report(self) -> ExitCode {
let Err(err) = self;
Err::<!, _>(err).report()
match self {}
}
}
@ -2183,3 +2161,18 @@ impl Termination for ExitCode {
self
}
}
#[stable(feature = "termination_trait_lib", since = "1.61.0")]
impl<T: Termination, E: fmt::Debug> Termination for Result<T, E> {
fn report(self) -> ExitCode {
match self {
Ok(val) => val.report(),
Err(err) => {
// Ignore error if the write fails, for example because stderr is
// already closed. There is not much point panicking at this point.
let _ = writeln!(io::stderr(), "Error: {err:?}");
ExitCode::FAILURE
}
}
}
}

View File

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