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

Treating fn as trait emits many unhelpful errors #82515

Closed
Plecra opened this issue Feb 25, 2021 · 1 comment · Fixed by #106176
Closed

Treating fn as trait emits many unhelpful errors #82515

Plecra opened this issue Feb 25, 2021 · 1 comment · Fixed by #106176
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Plecra
Copy link

Plecra commented Feb 25, 2021

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() {}
@Plecra Plecra added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 25, 2021
@Plecra
Copy link
Author

Plecra commented Feb 25, 2021

Alternatively, @memoryruins suggested using E0404

error[E0404]: expected trait, found keyword `fn`
 --> src/lib.rs:1:11
  |
4 | fn map(f: impl fn()) {}
  |                ^^ not a trait

@JohnTitor JohnTitor added A-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. labels Mar 1, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 28, 2022
… r=estebank

Recover `fn` keyword as `Fn` trait in bounds

`impl fn()` -> `impl Fn()`

Fixes rust-lang#82515
@bors bors closed this as completed in d07be1a Dec 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants