Repository: rust
Issue: Improve suggestion when generic type parameter is missing from struct method definition
Labels: A-diagnostics,T-compiler

Hi

Whilst working through the Rust Book final project ([Building a multithreaded web server](https://doc.rust-lang.org/stable/book/ch20-02-multithreaded.html#building-the-threadpool-struct-using-compiler-driven-development)), I encountered the following compiler error which could be more helpful. 

I was attempting to type out the following `execute()` method definition from here:
https://github.com/rust-lang/book/blob/b54090a99ec7c4b46a5203a9c927fdbc311bb1f5/listings/ch20-web-server/no-listing-03-define-execute/src/lib.rs#L12-L16

However I missed off the `<F>` type parameter from `pub fn execute<F>(...)`.

Feel free to rename the issue if the title could be made clearer :-)

---

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=25febf7c12b3b31793463c18a9924c18

```rust
pub struct ThreadPool;

impl ThreadPool {
    // ...

    pub fn execute(&self, f: F)
    where
        F: FnOnce() + Send + 'static,
    {
    }
}
```

The current output is:

```
error[E0412]: cannot find type `F` in this scope
  --> src/lib.rs:8:9
   |
8  |           F: FnOnce() + Send + 'static,
   |           ^ help: a trait with a similar name exists: `Fn`

error[E0412]: cannot find type `F` in this scope
  --> src/lib.rs:6:30
   |
6  |       pub fn execute(&self, f: F)
   |                                ^ help: a trait with a similar name exists: `Fn`
```

Ideally the output should look like:

```
error[E0412]: cannot find type `F` in this scope
  --> src/lib.rs:6:30
   |
6  |       pub fn execute(&self, f: F)
   |                                ^
help: you might be missing a type parameter
   |
6  |       pub fn execute<F>(&self, f: F)
   |                     ^^^

error[E0412]: cannot find type `F` in this scope
  --> src/lib.rs:8:9
   |
8  |           F: FnOnce() + Send + 'static,
   |           ^ 
```

Note that for the same typo made on a function (as opposed to a struct method), there is already useful help output about the missing `<F>`.

ie: 
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=47ff7814dbad0d8a506e0b2594ea3976

```rust
pub fn execute(f: F)
where
    F: FnOnce() + Send + 'static,
{
    // ...
}
```

Gives the more helpful:

```
error[E0412]: cannot find type `F` in this scope
  --> src/lib.rs:3:5
   |
3  |       F: FnOnce() + Send + 'static,
   |       ^
   |
help: a trait with a similar name exists
   |
3  |     Fn: FnOnce() + Send + 'static,
   |     ^^
help: you might be missing a type parameter
   |
1  | pub fn execute<F>(f: F)
   |               ^^^

error[E0412]: cannot find type `F` in this scope
  --> src/lib.rs:1:19
   |
1  |   pub fn execute(f: F)
   |                     ^
   |
help: a trait with a similar name exists
   |
1  | pub fn execute(f: Fn)
   |                   ^^
help: you might be missing a type parameter
   |
1  | pub fn execute<F>(f: F)
   |               ^^^
```

...though that said, in that error message, perhaps only the `help: you might be missing a type parameter` should be shown, and not `help: a trait with a similar name exists`, given that I don't think a Trait would be valid before a colon inside the where clause?