Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing compiler error when returning a Result<Result> via ?-operator #86547

Open
sdroege opened this issue Jun 22, 2021 · 5 comments
Open
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@sdroege
Copy link
Contributor

sdroege commented Jun 22, 2021

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=9aac41ba36103dac1f4ae20fa30c86f0

fn foo(x: Option<i32>) -> Result<(), &'static str> {
    let _ = x.ok_or_else(|| Err("nope"))?;
    Ok(())
}

fn main() {
    println!("{:?}", foo(Some(1)));
}

The current output is:

error[E0277]: `?` couldn't convert the error to `&str`
 --> src/main.rs:2:41
  |
1 | fn foo(x: Option<i32>) -> Result<(), &'static str> {
  |                           ------------------------ expected `&str` because of this
2 |     let _ = x.ok_or_else(|| Err("nope"))?;
  |                                         ^ the trait `From<Result<_, &str>>` is not implemented for `&str`
  |
  = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
  = note: required by `from`

Ideally the output would mention the double-Result wrapping ("got type Result<Result<...>> expected ...") and suggest removing one layer or Result::flatten() or similar.

It kind of does the former already if you squint but it's far from obvious ("the trait From<Result<_, &str>> is not implemented for &str").

@sdroege sdroege added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 22, 2021
@scottmcm
Copy link
Member

The error on nightly is a bit different now, post rust-lang/rfcs#3058

error[E0277]: `?` couldn't convert the error to `&str`
 --> src/main.rs:2:41
  |
1 | fn foo(x: Option<i32>) -> Result<(), &'static str> {
  |                           ------------------------ expected `&str` because of this
2 |     let _ = x.ok_or_else(|| Err("nope"))?;
  |                                         ^ the trait `From<Result<_, &str>>` is not implemented for `&str`
  |
  = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
  = note: required because of the requirements on the impl of `FromResidual<Result<Infallible, Result<_, &str>>>` for `Result<(), &str>`
  = note: required by `from_residual`

That does have the double-nest mentioned (Result<Infallible, Result<_, &str>>), though still not super-obviously.

(It'll be a bit clearer once ! stabilizes and it can shorten to Result<!, Result<_, &str>>.)

@756445638
Copy link

756445638 commented Jul 9, 2021

fn foo(x: Option<i32>) -> Result<(), &'static str> {
    let _ = x.ok_or_else(|| Err(      &         "nope"))?;
    Ok(())
}

fn main() {
    println!("{:?}", foo(Some(1)));
}

is this better, I think this should works fine and compile ok.

@756445638
Copy link

756445638 commented Jul 9, 2021

the compiler is right!!

try will desuger to this

/// Desugar `ExprKind::Try` from: `<expr>?` into:
/// ```rust
/// match Try::into_result(<expr>) {
///     Ok(val) => #[allow(unreachable_code)] val,
///     Err(err) => #[allow(unreachable_code)]
///                 // If there is an enclosing `try {...}`:
///                 break 'catch_target Try::from_error(From::from(err)),
///                 // Otherwise:
///                 return Try::from_error(From::from(err)),
/// }
/// ```
x.ok_or_else(|| Err("nope"))?

will be

match Try::into_result(x.ok_or_else(|| Err("nope"))) {
    Ok(val) => val ,
    Err(err) => return Try::from_error(err)
    //err is &str ,
    //and the trait `From<Result<_, &str>>` is not implemented for `&str`
    //and you cannot impl From<Result<_, &str>> for &str {}.
}

@scottmcm
Copy link
Member

scottmcm commented Jul 9, 2021

@756445638 Oops, I forgot to update that comment. Fixed with PR #86994

@756445638
Copy link

756445638 commented Jul 10, 2021

@scottmcm ok I just notice that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants