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 · 1 comment
Open
Labels
A-closures Area: Closures (`|…| { … }`) 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 (`|…| { … }`) 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
bors added a commit to rust-lang-ci/rust that referenced this issue Jun 3, 2024
Use parenthetical notation for `Fn` traits

Always use the `Fn(T) -> R` format when printing closure traits instead of `Fn<(T,), Output = R>`.

Address rust-lang#67100:

```
error[E0277]: expected a `Fn()` closure, found `F`
 --> file.rs:6:13
  |
6 |     call_fn(f)
  |     ------- ^ expected an `Fn()` closure, found `F`
  |     |
  |     required by a bound introduced by this call
  |
  = note: wrap the `F` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `call_fn`
 --> file.rs:1:15
  |
1 | fn call_fn<F: Fn() -> ()>(f: &F) {
  |               ^^^^^^^^^^ required by this bound in `call_fn`
help: consider further restricting this bound
  |
5 | fn call_any<F: std::any::Any + Fn()>(f: &F) {
  |                              ++++++
```
@bors bors closed this as completed in e6bd6c2 Jun 3, 2024
@compiler-errors
Copy link
Member

This wasn't fixed in general: #125778 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) 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

3 participants