Skip to content

Treating fn as trait emits many unhelpful errors #82515

Closed
@Plecra

Description

@Plecra

Given the following code:

fn map(f: impl fn()) {}

The current output is:

error: expected identifier, found keyword `fn`
 --> src/lib.rs:1:16
  |
1 | fn map(f: impl fn()) {}
  |                ^^ expected identifier, found keyword
  |
help: you can escape reserved keywords to use them as identifiers
  |
1 | fn map(f: impl r#fn()) {}
  |                ^^^^

error: expected one of `:` or `|`, found `)`
 --> src/lib.rs:1:20
  |
1 | fn map(f: impl fn()) {}
  |                    ^ expected one of `:` or `|`

error: expected one of `!`, `(`, `)`, `,`, `?`, `for`, lifetime, or path, found keyword `fn`
 --> src/lib.rs:1:16
  |
1 | fn map(f: impl fn()) {}
  |               -^^ expected one of 8 possible tokens
  |               |
  |               help: missing `,`

error: at least one trait must be specified
 --> src/lib.rs:1:11
  |
1 | fn map(f: impl fn()) {}
  |           ^^^^

error: aborting due to 4 previous errors

error: could not compile `playground`

Ideally the output should look like:

error[E0405]: cannot find trait `fn` in this scope
  --> src/lib.rs:2:16
   |
1  |   fn map(f: impl fn()) {}
   |                  ^^^^ help: a trait with a similar name exists (notice the capitalization): `Fn`
note: `fn` is the type of a function pointer, and cannot be used as a trait
help: <maybe something brief about the Fn{Mut, Once} traits>

This is based on the error generated by fn foo(v: impl send) {}

Related diagnostics

The issue also affects return-position impls, where more helpful guidance could be given

fn g() -> impl fn() {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-verboseDiagnostics: Too much output caused by a single piece of 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