Repository: rust
Issue: Confusing "Mismatched types" error due to unsatisfied trait bound
Labels: A-diagnostics,T-compiler

### Code

```Rust
// #[derive(Clone)] // <- uncomment this to fix it
struct Thing;

fn main() {
    message_fn(Message1);
}

fn message_fn<M>(_: M)
where
    Thing: HasMessage<M>,
{
    unimplemented!()
}

trait HasMessage<T> {}

struct Message1;
impl<M: Clone> HasMessage<Message1> for M {}

struct Message2;
impl<M> HasMessage<Message2> for M {}
```

[playground link](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f73abdfb60368b97c5dc94b7232745f2)


### Current output

```Shell
   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/main.rs:5:16
  |
5 |     message_fn(Message1);
  |     ---------- ^^^^^^^^ expected `Message2`, found `Message1`
  |     |
  |     arguments to this function are incorrect
  |
note: function defined here
 --> src/main.rs:8:4
  |
8 | fn message_fn<M>(_: M)
  |    ^^^^^^^^^^    ----

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (bin "playground") due to 1 previous error
```


### Desired output

```Shell
Something mentioning the fact that `Thing` doesn't implement `Clone`.
```


### Rationale and extra context

It's unclear to me why rust decides that the `Message2` impl must be the intended impl in `main`. The intended impl is the `Message1` one.

The actual reason the code shouldn't compile is because the author of the code forgot to implement `Clone` for `Thing`, which is required for the `Message1` impl. However, figuring that out from the error message is difficult, especially in the original code where there are other stuff going on.

### Other cases

Adding a `Message3` results in a reasonable error

<details>
<summary>Code and error</summary>

```rust
// #[derive(Clone)] // <- uncomment this to fix it
struct Thing;

fn main() {
    message_fn(Message1);
}

fn message_fn<M>(_: M)
where
    Thing: HasMessage<M>,
{
    unimplemented!()
}

trait HasMessage<T> {}

struct Message1;
impl<M: Clone> HasMessage<Message1> for M {}

struct Message2;
impl<M> HasMessage<Message2> for M {}

struct Message3;
impl<M> HasMessage<Message3> for M {}
```

```
   Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `Thing: HasMessage<Message1>` is not satisfied
  --> src/main.rs:5:16
   |
5  |     message_fn(Message1);
   |     ---------- ^^^^^^^^ the trait `HasMessage<Message1>` is not implemented for `Thing`
   |     |
   |     required by a bound introduced by this call
   |
note: required for `Thing` to implement `HasMessage<Message1>`
  --> src/main.rs:18:16
   |
18 | impl<M: Clone> HasMessage<Message1> for M {}
   |         -----  ^^^^^^^^^^^^^^^^^^^^     ^
   |         |
   |         unsatisfied trait bound introduced here
note: required by a bound in `message_fn`
  --> src/main.rs:10:12
   |
8  | fn message_fn<M>(_: M)
   |    ---------- required by a bound in this function
9  | where
10 |     Thing: HasMessage<M>,
   |            ^^^^^^^^^^^^^ required by this bound in `message_fn`
help: consider borrowing here
   |
5  |     message_fn(&Message1);
   |                +

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 1 previous error
```

</details>

Deleting the `Message2` also results in a reasonable error

<details>
<summary>Code and error</summary>

```rust
// #[derive(Clone)] // <- uncomment this to fix it
struct Thing;

fn main() {
    message_fn(Message1);
}

fn message_fn<M>(_: M)
where
    Thing: HasMessage<M>,
{
    unimplemented!()
}

trait HasMessage<T> {}

struct Message1;
impl<M: Clone> HasMessage<Message1> for M {}
```

```
   Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `Thing: HasMessage<_>` is not satisfied
  --> src/main.rs:5:16
   |
5  |     message_fn(Message1);
   |     ---------- ^^^^^^^^ the trait `HasMessage<_>` is not implemented for `Thing`
   |     |
   |     required by a bound introduced by this call
   |
note: required for `Thing` to implement `HasMessage<Message1>`
  --> src/main.rs:18:16
   |
18 | impl<M: Clone> HasMessage<Message1> for M {}
   |         -----  ^^^^^^^^^^^^^^^^^^^^     ^
   |         |
   |         unsatisfied trait bound introduced here
note: required by a bound in `message_fn`
  --> src/main.rs:10:12
   |
8  | fn message_fn<M>(_: M)
   |    ---------- required by a bound in this function
9  | where
10 |     Thing: HasMessage<M>,
   |            ^^^^^^^^^^^^^ required by this bound in `message_fn`
help: consider borrowing here
   |
5  |     message_fn(&Message1);
   |                +

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 1 previous error
```

</details>

### Rust Version

```Shell
Playground's nightly rust (1.81.0-nightly (2024-06-17 59e2c01c2217a0154622))
```


### Anything else?

This code is reduced from real code found by @tqwewe

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_ASSIGN_START -->

<!-- TRIAGEBOT_ASSIGN_DATA_START$${"user":null}$$TRIAGEBOT_ASSIGN_DATA_END -->

<!-- TRIAGEBOT_ASSIGN_END -->
<!-- TRIAGEBOT_END -->