Skip to content

E0121 suggests the use of the unique type of a returned function/closure rather than a fn pointer or a Fn trait #80179

Closed

Description

I tried this code:

fn f() -> i32 {
    0
}

fn returns_fn_pointer() -> _ {
    f
}

The resulting diagnostic suggests replacing _ with fn() -> i32 {f}, the unique type of f():

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
 --> src/lib.rs:5:28
  |
5 | fn returns_fn_pointer() -> _ {
  |                            ^
  |                            |
  |                            not allowed in type signatures
  |                            help: replace with the correct return type: `fn() -> i32 {f}`

This, of course, doesn't work:

error: expected item, found `{`
 --> src/lib.rs:5:44
  |
5 | fn returns_fn_pointer() -> fn() -> i32 {f} {
  |                                            ^ expected item

I'd expect the diagnostic to suggest replacing _ with fn() -> i32 or impl Fn() -> i32, both of which compile successfully.

Meta

rustc --version --verbose:

rustc 1.50.0-nightly (2225ee1b6 2020-12-11)
binary: rustc
commit-hash: 2225ee1b62ff089917434aefd9b2bf509cfa087f
commit-date: 2020-12-11
host: x86_64-unknown-linux-gnu
release: 1.50.0-nightly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions