Skip to content

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

@chenyukang

Description

@chenyukang

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions