Repository: rust
Issue: Missing trait error for type (used as a type parameter) containing a trait object can have a broader span than expected
Labels: A-diagnostics,A-trait-system,T-compiler,A-trait-objects,D-imprecise-spans

<!--
Thank you for filing a bug report! 🐛 Please provide a short summary of the bug,
along with any information you feel relevant to replicating the bug.

If you cannot produce a minimal reproduction case (something that would work in
isolation), please provide the steps or even link to a repository that causes
the problematic output to occur.
-->

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7a574fab349d3be4d4574b12df5a9932

```rust
struct TypeCombiner<A, B>(A, B);

trait MyTrait {
    type Assoc;
}

trait OtherTrait {}

struct A;
struct B;
struct C {
    a: A,
    b: B,
}

impl MyTrait for A {
    type Assoc = ();
}

// missing implementation for B

impl MyTrait for C {
    // span for error output of missing MyTrait for the `Box<T>` type is taken
    // from the whole `TypeCombiner<...>` when `T` is a trait object 
    type Assoc = TypeCombiner<
        <A as MyTrait>::Assoc,
        <Box<dyn OtherTrait> as MyTrait>::Assoc,
    >;
}
```

The current output is:

```
error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the trait bound `Box<(dyn OtherTrait + 'static)>: MyTrait` is not satisfied
  --> src/lib.rs:25:18
   |
25 |       type Assoc = TypeCombiner<
   |  __________________^
26 | |         <A as MyTrait>::Assoc,
27 | |         <Box<dyn OtherTrait> as MyTrait>::Assoc,
28 | |     >;
   | |_____^ the trait `MyTrait` is not implemented for `Box<(dyn OtherTrait + 'static)>`
   |
   = help: the following other types implement trait `MyTrait`:
             A
             C
```

<!-- The following is not always necessary. -->
Ideally the output should look like:

```
error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the trait bound `Box<(dyn OtherTrait + 'static)>: MyTrait` is not satisfied
  --> src/lib.rs:27:9
   |
27 |         <Box<dyn OtherTrait> as MyTrait>::Assoc,
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `Box<(dyn OtherTrait + 'static)>: MyTrait`
   |
   = help: the following other types implement trait `MyTrait`:
             A
             C
```

<!--
If the problem is not self-explanatory, please provide a rationale for the
change.
-->

<!--
If dramatically different output is caused by small changes, consider also
adding them here.

If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions. The output might also be
different depending on the Edition.
-->

Note that replacing `dyn OtherTrait` with a non trait object type like `u8` yields the desired error output. My first guess would be that this is related to expanding `dyn OtherTrait` to `(dyn OtherTrait + 'static)` (but this is just a random guess).

In my case, this causes error output in custom derive macro generated code to not point back to the original fields. Normally, the proc macro can give each type parameter the span info for the field it is derived from and then error output will show that field.