Skip to content

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

Open
@olegnn

Description

@olegnn
Contributor

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)

Activity

added
A-closuresArea: Closures (`|…| { … }`)
A-diagnosticsArea: Messages for errors, warnings, and lints
C-bugCategory: This is a bug.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
on Dec 6, 2019
added a commit that references this issue on May 30, 2024
e6bd6c2
added a commit that references this issue on Jun 3, 2024

Auto merge of rust-lang#125778 - estebank:issue-67100, r=compiler-errors

compiler-errors

compiler-errors commented on Jun 3, 2024

@compiler-errors
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

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

        Participants

        @jonas-schievink@compiler-errors@olegnn

        Issue actions

          Incorrect hints for `Fn` type on stable and incorrect suggestions on nightly · Issue #67100 · rust-lang/rust