Skip to content

For function returning simple closure, suggest possible way to specify the return type #100936

Closed
@Rageking8

Description

@Rageking8

Given the following code: link

fn func() {
    || 123
}

fn main() {
    println!("{}", func()())
}

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:2:5
  |
1 | fn func() {
  |           - help: a return type might be missing here: `-> _`
2 |     || 123
  |     ^^^^^^ expected `()`, found closure
  |
  = note: expected unit type `()`
               found closure `[closure@src/main.rs:2:5: 2:7]`

error[[E0618]](https://doc.rust-lang.org/nightly/error-index.html#E0618): expected function, found `()`
 --> src/main.rs:6:20
  |
1 | fn func() {
  | --------- `func` defined here returns `()`
...
6 |     println!("{}", func()())
  |                    ^^^^^^--
  |                    |
  |                    call expression requires function

Some errors have detailed explanations: E0308, E0618.
For more information about an error, try `rustc --explain E0308`.
error: could not compile `playground` due to 2 previous errors

Ideally the output should suggest how to specify the return type when a function returns a closure.

Metadata

Metadata

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