Stream: t-compiler/help

Topic: Why E0277 is triggered here?

Yan (Jul 10 2020 at 18:23, on Zulip):

In the code snippets, test and test2 works fine, but test3 cannot get compiled. The error message shows an error of E0277. Why test and test2 works fine, but test3 failed to compile? It seems that the Option type has an implementation of the From trait, which is implicitly invoked in test.

   Compiling playground v0.0.1 (/playground)
error[E0277]: `?` couldn't convert the error to `std::option::NoneError`
  --> src/
30 |     r?;
   |      ^ the trait `std::convert::From<std::io::Error>` is not implemented for `std::option::NoneError`
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = note: required by `std::convert::From::from`
help: consider converting the `Result<T, _>` into an `Option<T>` using `Result::ok`
30 |     r.ok()?;
   |      ^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.
Jake Goulding (Jul 10 2020 at 18:41, on Zulip):

It's not clear to me why you think that ? should convert a Result to an Option.

Yan (Jul 10 2020 at 18:44, on Zulip):

the compiler said that question mark invoke From trait implicitly, but the behavior is not the same.

Yan (Jul 10 2020 at 18:45, on Zulip):

Yan (Jul 10 2020 at 18:47, on Zulip):

I think the magic happens in test is the invocation of From impl of Poll.

Jake Goulding (Jul 10 2020 at 18:47, on Zulip):

? does invoke From, but what From implementation do you expect to be invoked to turn a Result to Option?

Jake Goulding (Jul 10 2020 at 18:48, on Zulip):

I also don't understand what you are attempting to demonstrate between the three functions.

Yan (Jul 10 2020 at 18:49, on Zulip):

in function test, question mark converts Err(...) to Some(Err(...)), but test3 failed to compile.

Jake Goulding (Jul 10 2020 at 18:52, on Zulip):

See also and

Yan (Jul 10 2020 at 19:03, on Zulip):

Thank you for pointing out that, now I see the answer. There is a specialized implementation of Try for Poll<Option<Result<T, E>>> code. I just think for the question mark, it's a recursive invocation of convert methods. - -!

Yan (Jul 10 2020 at 19:50, on Zulip):

After some reading, I learned that the Try trait is the place where the magic happened. It's a feature of the RFC-1859. Thanks @Jake Goulding ;)

Yan (Jul 10 2020 at 19:55, on Zulip):

The only problem left is, Try trait is a nightly feature and why I got effected in stable rust?

Jake Goulding (Jul 10 2020 at 20:02, on Zulip):

Implementing Try is unstable, but the standard library is allowed to use stable features.

eddyb (Jul 11 2020 at 01:43, on Zulip):

@Yan specifically, ? invokes From on the error type, i.e. between E1 and E2 in Result<T, E1> vs Result<T, E1>, not on the whole type

eddyb (Jul 11 2020 at 01:43, on Zulip):

so it's like .map_err(From::from)

Last update: Sep 28 2020 at 16:30UTC