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

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

Closed
Rageking8 opened this issue Aug 24, 2022 · 4 comments · Fixed by #101019
Closed
Assignees
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

@Rageking8
Copy link
Contributor

Rageking8 commented Aug 24, 2022

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.

@Rageking8 Rageking8 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 Aug 24, 2022
@ChayimFriedman2
Copy link
Contributor

@rustbot claim

@Daudongit
Copy link

@Rageking8 Is some thing like this not ok ?

fn func() ->impl FnOnce()->u8{
    || 123u8
}

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

@ChayimFriedman2
Copy link
Contributor

@rustbot release-assignment

@Rageking8
Copy link
Contributor Author

Rageking8 commented Aug 25, 2022

@Daudongit It's ok but I was wondering if it's possible to make the compiler give some form of a suggestion to the user on how to do this. Cause the current diagnostics does not suggest how to fix it.

@compiler-errors compiler-errors self-assigned this Aug 26, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 30, 2022
…gestion, r=cjgillot

Suggest returning closure as `impl Fn`

Fixes rust-lang#100936
@bors bors closed this as completed in aa9bfde Aug 30, 2022
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

Successfully merging a pull request may close this issue.

4 participants