rust-lang-deprecated / error-chain

Error boilerplate for Rust
Apache License 2.0
729 stars 111 forks source link

impl From<std::convert::Infallible> for Error #229

Open CraZySacX opened 6 years ago

CraZySacX commented 6 years ago

https://github.com/rust-lang/rust/pull/44174 landed in rust nightly and now I have to add

impl From<Infallible> for Error {
    fn from(infallible: Infallible) -> Self {
        match infallible {}
    }
}

to my error chain Error. Can/Should this impl be included in the library?

Yamakaky commented 6 years ago

I'm not sure I understand why this impl is needed?

CraZySacX commented 6 years ago

I'm not really sure either, but it was triggered from the following:

let len: isize = isize::try_from(self.conn.num_app_context)?;

where I was converting from a u32 in a FFI binding to isize.

CraZySacX commented 6 years ago

Here is the error if I comment out the impl.

error[E0277]: the trait bound `error::Error: std::convert::From<std::convert::Infallible>` is not satisfied
   --> src/context/params.rs:312:26
    |
312 |         let len: isize = isize::try_from(self.conn.num_app_context)?;
    |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<std::convert::Infallible>` is not implemented for `error::Error`
    |
    = help: the following implementations were found:
              <error::Error as std::convert::From<std::io::Error>>
              <error::Error as std::convert::From<std::ffi::NulError>>
              <error::Error as std::convert::From<std::env::VarError>>
              <error::Error as std::convert::From<std::num::TryFromIntError>>
            and 3 others
    = note: required by `std::convert::From::from`
Yamakaky commented 6 years ago

Oh, I see. Then yes, it would be good to add it to error-chain, maybe behind a feature until TryFrom comes to stable. I think this would be better:

impl From<Infallible> for Error {
    fn from(_: Infallible) -> Self {
        unreachable!()
    }
}
KodrAus commented 6 years ago

Ah I see what this was all about now. Looks like right now we'd still need to convert from the infallible error type even if it was !, but maybe that will change in the future given the never type is a special snowflake.