failure icon indicating copy to clipboard operation
failure copied to clipboard

Using `failure` with `#![termination_trait]` (`fn main() -> Result<T>`)

Open U007D opened this issue 7 years ago • 10 comments

I'm running into a couple of issues trying to use failure with #![termination_trait].

#![feature(termination_trait)]

extern crate failure;

use std::io::{Error as IoError, ErrorKind as IoErrorKind};
use failure::{Fail, Error as FailureError};

type Result<T> = std::result::Result<T, Fail>;

fn main() -> Result<()> {
    Err(IoError::from(IoErrorKind::InvalidData))?
}

results in:

error[E0277]: the trait bound `failure::Fail + 'static: std::marker::Sized` is not satisfied
  --> src/main.rs:10:1
   |
10 | / fn main() -> Result<()> {
11 | |     Err(IoError::from(IoErrorKind::InvalidData))?
12 | | }
   | |_^ `failure::Fail + 'static` does not have a constant size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `failure::Fail + 'static`
   = note: required by `std::result::Result`

error: aborting due to previous error

Switching the Result declaration to type Result<T> = std::result::Result<T, FailureError> yields:

error[E0277]: the trait bound `failure::Error: std::error::Error` is not satisfied
  --> src/main.rs:15:25
   |
10 |   fn main() -> Result<()> {
   |  _________________________^
11 | |     Err(IoError::from(IoErrorKind::InvalidData))?
12 | | }
   | |_^ the trait `std::error::Error` is not implemented for `failure::Error`
   |
   = note: required because of the requirements on the impl of `std::Termination` for `std::result::Result<(), failure::Error>`

error: aborting due to previous error

Is there a solution for this? If not, should this be a failure or a termination_trait issue?

Many thanks, U007D

U007D avatar Jan 15 '18 20:01 U007D

The issue you're getting is that you've used Fail as the error type, which is just a trait. You should use the Error type from failure instead, type Result<T> = Result<T, failure::Error>;.

I'll leave this open, please report back whether or not you run into more issues, I want to make sure failure works with this feature.

withoutboats avatar Jan 15 '18 21:01 withoutboats

I tried it using failure::Error too. But I see now that I accidentally pasted the Fail error message twice--I'd intended to post the message from using failure::Error. I've fixed my original posting.

U007D avatar Jan 15 '18 21:01 U007D

It looks like the termination trait requires that we implement the old error type. This is unfortunate. :-(

withoutboats avatar Jan 16 '18 01:01 withoutboats

Indeed. Although it is still feature-gated. I'm never sure where is the right place to leave comments, but the termination_trait PR is here. Is this issue something that you could weigh in on to help it be compatible with failure before stabilizing?

U007D avatar Jan 16 '18 02:01 U007D

When I finally have some time, I will improve the implementation to support failure :)

bkchr avatar Jan 17 '18 09:01 bkchr

Hi, @bkchr,

Awesome, thank you. Is this something I could help with?

U007D avatar Jan 17 '18 11:01 U007D

It's just changing the implementation, I think we should go the way @withoutboats proposed by restricting the type to Debug.

bkchr avatar Jan 17 '18 12:01 bkchr

Niko's recent "hacking on the compiler" writings inspired me to try building the compiler... So I forked rust and built it for the first time :tada:.

I ran ./x.py test before and after the change and got the same results both times. FYI, not all tests run because of a linker error; but of the ones that do run (hundreds), all pass except 1 (run-make/relocation-model).

https://github.com/U007D/rust/commit/b9b2ca8bd04e932a361adff2af813bd0f9f07e54

Assuming this diff looks right to you two, where should I target the PR? Back to rust-lang/rust/master? Or are you working from a branch that I should merge back to, @bkchr?

U007D avatar Jan 17 '18 23:01 U007D

@U007D Back to rust/master. My recent changes does not include anything you changed :)

bkchr avatar Jan 18 '18 08:01 bkchr

Done.

U007D avatar Jan 18 '18 14:01 U007D