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

Option/Result get wrong diagnostic for closure as map_or first argument #102408

Closed
chenyukang opened this issue Sep 28, 2022 · 1 comment
Closed
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

@chenyukang
Copy link
Member

If map_or's first arguments is given a closure, the diagnostic now is as below, which is not right:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=a24f43e57b31d21c67716fd23888a507

fn main() {
    let k = 1 as usize;
    let x: Option<&str> = None;
    let _res = x.map_or(|| k * 2, |v| v.len());
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
 --> src/main.rs:5:39
  |
5 |     let _res = x.map_or(|| k * 2, |v| v.len());
  |                         --            ^^^^^^^ expected closure, found `usize`
  |                         |
  |                         the expected closure
  |
  = note: expected closure `[closure@src/main.rs:5:25: 5:27]`
                found type `usize`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

Ideally, we should suggest to use map_or_else.
Maybe related to #102320, which I was working on, but the above diagnostic message for arguments is not right, may be another issue.

@chenyukang chenyukang 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 Sep 28, 2022
@chenyukang
Copy link
Member Author

Understood it now, it's expecting the second argument return a same type with first argument -- a closure.

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

1 participant