Skip to content

Slightly extend help diagnostic for immediately invoked closure #101374

@Rageking8

Description

@Rageking8

Given the following code: link

fn main() {
    println!("{}", || 123 ());
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0618]](https://doc.rust-lang.org/nightly/error-index.html#E0618): expected function, found `{integer}`
 --> src/main.rs:2:23
  |
2 |     println!("{}", || 123 ());
  |                       ^^^---
  |                       |
  |                       call expression requires function

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

Ideally the output should suggest adding parentheses around the closure before calling it, since that suggestion is already present if the return value of the closure is wrapped in braces, as shown below.

fn main() {
    println!("{}", || { 123 } ());
}

Output with help suggestion:

Compiling playground v0.0.1 (/playground)
error[[E0618]](https://doc.rust-lang.org/nightly/error-index.html#E0618): expected function, found `{integer}`
 --> src/main.rs:2:23
  |
2 |     println!("{}", || { 123 } ());
  |                       ^^^^^^^---
  |                       |
  |                       call expression requires function
  |
help: if you meant to create this closure and immediately call it, surround the closure with parentheses
  |
2 |     println!("{}", (|| { 123 }) ());
  |                    +          +

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

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