Skip to content

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

Open
@olegnn

Description

@olegnn

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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