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

Incorrect hints for Fn type on stable and incorrect suggestions on nightly #67100

Open
olegnn opened this issue Dec 6, 2019 · 0 comments
Open
Labels
A-closures Area: closures (`|args| { .. }`) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@olegnn
Copy link
Contributor

olegnn commented Dec 6, 2019

Code

fn call_fn<F: Fn() -> ()>(f: &F) {
    f()
}

fn call_any<F: std::any::Any>(f: &F) {
    call_fn(f)
}

fn main() {
    call_any(&|| {});
}

currently produces on stable

error[E0277]: expected a `std::ops::Fn<()>` closure, found `F`
 --> src/main.rs:6:13
  |
1 | fn call_fn<F: Fn() -> ()>(f: &F) {
  |    -------    ---------- required by this bound in `call_fn`
...
6 |     call_fn(f)
  |             ^ expected an `Fn<()>` closure, found `F`
  |
  = help: the trait `std::ops::Fn<()>` is not implemented for `F`
  = note: wrap the `F` in a closure with no arguments: `|| { /* code */ }
  = help: consider adding a `where F: std::ops::Fn<()>` bound
  • note: wrap the F in a closure with no arguments: || { /* code */ } - ???
  • where F: std::ops::Fn<()> - incorrect Fn syntax

On nightly situation is more interesting:

error[E0277]: expected a `std::ops::Fn<()>` closure, found `F`
 --> src/main.rs:6:13
  |
1 | fn call_fn<F: Fn() -> ()>(f: &F) {
  |    -------    ---------- required by this bound in `call_fn`
...
5 | fn call_any<F: std::any::Any>(f: &F) {
  |             -- help: consider further restricting this bound: `F: std::ops::Fn<()> +`
6 |     call_fn(f)
  |             ^ expected an `Fn<()>` closure, found `F`
  |
  = help: the trait `std::ops::Fn<()>` is not implemented for `F`
  = note: wrap the `F` in a closure with no arguments: `|| { /* code */ }
  • -- help: consider further restricting this bound: F: std::ops::Fn<()> + has missing type and incorrect Fn syntax (Fn() is correct)

(Playground)

@jonas-schievink jonas-schievink added A-closures Area: closures (`|args| { .. }`) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Dec 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: closures (`|args| { .. }`) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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

2 participants