Repository: rust
Issue: Duplicated error messages
Labels: A-diagnostics,A-trait-system,T-compiler,D-verbose

<!--
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: <!-- Please provide a link to play.rust-lang.org -->

```rust
#[macro_use]
extern crate diesel;

use diesel::expression::exists::Exists;
use diesel::expression::subselect::ValidSubselect;
use diesel::pg::Pg;
use diesel::query_builder::{SelectQuery, SelectStatement};
use diesel::query_dsl::methods::BoxedDsl;
use diesel::query_dsl::LoadQuery;
use diesel::*;

fn check_exists<'a, T>(
    conn: &PgConnection,
    check: dsl::IntoBoxed<'a, T, Pg>,
) -> Result<(), diesel::result::Error>
where
    T: BoxedDsl<'a, Pg>,
{
    use diesel::dsl::{exists, select};

    select(exists(check))
        .get_result::<bool>(conn)?
        .then(|| {})
        .expect("Does not exist");

    Ok(())
}
```

The current output is:

(as of `rustc 1.56.1 (59eed8a2a 2021-11-01)`)

```
error[E0277]: the trait bound `<T as BoxedDsl<'a, Pg>>::Output: SelectQuery` is not satisfied
   --> src/main.rs:21:12
    |
21  |     select(exists(check))
    |            ^^^^^^^^^^^^^ the trait `SelectQuery` is not implemented for `<T as BoxedDsl<'a, Pg>>::Output`
    |
    = note: required because of the requirements on the impl of `diesel::Expression` for `Subselect<<T as BoxedDsl<'a, Pg>>::Output, ()>`
    = note: 1 redundant requirements hidden
    = note: required because of the requirements on the impl of `diesel::Expression` for `Exists<<T as BoxedDsl<'a, Pg>>::Output>`
note: required by a bound in `diesel::select`
   --> /home/weiznich/.cargo/registry/src/github.com-1ecc6299db9ec823/diesel-1.4.8/src/query_builder/functions.rs:375:8
    |
375 |     T: Expression,
    |        ^^^^^^^^^^ required by this bound in `diesel::select`
help: consider further restricting the associated type
    |
17  |     T: BoxedDsl<'a, Pg>, <T as BoxedDsl<'a, Pg>>::Output: SelectQuery
    |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0277]: the trait bound `<T as BoxedDsl<'a, Pg>>::Output: ValidSubselect<()>` is not satisfied
   --> src/main.rs:21:12
    |
21  |     select(exists(check))
    |            ^^^^^^^^^^^^^ the trait `ValidSubselect<()>` is not implemented for `<T as BoxedDsl<'a, Pg>>::Output`
    |
    = note: required because of the requirements on the impl of `SelectableExpression<()>` for `Subselect<<T as BoxedDsl<'a, Pg>>::Output, ()>`
    = note: 1 redundant requirements hidden
    = note: required because of the requirements on the impl of `SelectableExpression<()>` for `Exists<<T as BoxedDsl<'a, Pg>>::Output>`
    = note: required because of the requirements on the impl of `SelectDsl<Exists<<T as BoxedDsl<'a, Pg>>::Output>>` for `diesel::query_builder::SelectStatement<()>`
note: required by a bound in `diesel::select`
   --> /home/weiznich/.cargo/registry/src/github.com-1ecc6299db9ec823/diesel-1.4.8/src/query_builder/functions.rs:376:26
    |
376 |     SelectStatement<()>: SelectDsl<T>,
    |                          ^^^^^^^^^^^^ required by this bound in `diesel::select`
help: consider further restricting the associated type
    |
17  |     T: BoxedDsl<'a, Pg>, <T as BoxedDsl<'a, Pg>>::Output: ValidSubselect<()>
    |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0277]: the trait bound `diesel::query_builder::SelectStatement<(), query_builder::select_clause::SelectClause<Exists<<T as BoxedDsl<'a, Pg>>::Output>>>: QueryFragment<_>` is not satisfied
  --> src/main.rs:22:10
   |
22 |         .get_result::<bool>(conn)?
   |          ^^^^^^^^^^ the trait `QueryFragment<_>` is not implemented for `diesel::query_builder::SelectStatement<(), query_builder::select_clause::SelectClause<Exists<<T as BoxedDsl<'a, Pg>>::Output>>>`
   |
   = help: the following implementations were found:
             <diesel::query_builder::SelectStatement<(), S, D, W, O, L, Of, G, LC> as QueryFragment<DB>>
             <diesel::query_builder::SelectStatement<F, S, D, W, O, L, Of, G, LC> as QueryFragment<DB>>
   = note: required because of the requirements on the impl of `LoadQuery<_, bool>` for `diesel::query_builder::SelectStatement<(), query_builder::select_clause::SelectClause<Exists<<T as BoxedDsl<'a, Pg>>::Output>>>`

error[E0277]: the trait bound `<T as BoxedDsl<'a, Pg>>::Output: SelectQuery` is not satisfied
  --> src/main.rs:22:10
   |
22 |         .get_result::<bool>(conn)?
   |          ^^^^^^^^^^ the trait `SelectQuery` is not implemented for `<T as BoxedDsl<'a, Pg>>::Output`
   |
   = note: required because of the requirements on the impl of `diesel::Expression` for `Subselect<<T as BoxedDsl<'a, Pg>>::Output, ()>`
   = note: 1 redundant requirements hidden
   = note: required because of the requirements on the impl of `diesel::Expression` for `Exists<<T as BoxedDsl<'a, Pg>>::Output>`
   = note: required because of the requirements on the impl of `LoadQuery<_, bool>` for `diesel::query_builder::SelectStatement<(), query_builder::select_clause::SelectClause<Exists<<T as BoxedDsl<'a, Pg>>::Output>>>`
help: consider further restricting the associated type
   |
17 |     T: BoxedDsl<'a, Pg>, <T as BoxedDsl<'a, Pg>>::Output: SelectQuery
   |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0277]: the trait bound `<T as BoxedDsl<'a, Pg>>::Output: ValidSubselect<()>` is not satisfied
  --> src/main.rs:22:10
   |
22 |         .get_result::<bool>(conn)?
   |          ^^^^^^^^^^ the trait `ValidSubselect<()>` is not implemented for `<T as BoxedDsl<'a, Pg>>::Output`
   |
   = note: required because of the requirements on the impl of `SelectableExpression<()>` for `Subselect<<T as BoxedDsl<'a, Pg>>::Output, ()>`
   = note: 1 redundant requirements hidden
   = note: required because of the requirements on the impl of `SelectableExpression<()>` for `Exists<<T as BoxedDsl<'a, Pg>>::Output>`
   = note: required because of the requirements on the impl of `query_builder::select_clause::SelectClauseExpression<()>` for `query_builder::select_clause::SelectClause<Exists<<T as BoxedDsl<'a, Pg>>::Output>>`
   = note: required because of the requirements on the impl of `LoadQuery<_, bool>` for `diesel::query_builder::SelectStatement<(), query_builder::select_clause::SelectClause<Exists<<T as BoxedDsl<'a, Pg>>::Output>>>`
help: consider further restricting the associated type
   |
17 |     T: BoxedDsl<'a, Pg>, <T as BoxedDsl<'a, Pg>>::Output: ValidSubselect<()>
   |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0277]: the trait bound `<T as BoxedDsl<'a, Pg>>::Output: QueryId` is not satisfied
  --> src/main.rs:22:10
   |
22 |         .get_result::<bool>(conn)?
   |          ^^^^^^^^^^ the trait `QueryId` is not implemented for `<T as BoxedDsl<'a, Pg>>::Output`
   |
   = note: required because of the requirements on the impl of `QueryId` for `Exists<<T as BoxedDsl<'a, Pg>>::Output>`
   = note: 2 redundant requirements hidden
   = note: required because of the requirements on the impl of `QueryId` for `diesel::query_builder::SelectStatement<(), query_builder::select_clause::SelectClause<Exists<<T as BoxedDsl<'a, Pg>>::Output>>>`
   = note: required because of the requirements on the impl of `LoadQuery<_, bool>` for `diesel::query_builder::SelectStatement<(), query_builder::select_clause::SelectClause<Exists<<T as BoxedDsl<'a, Pg>>::Output>>>`
help: consider further restricting the associated type
   |
17 |     T: BoxedDsl<'a, Pg>, <T as BoxedDsl<'a, Pg>>::Output: QueryId
   |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0277]: the trait bound `diesel::query_builder::SelectStatement<()>: SelectDsl<Exists<<T as BoxedDsl<'a, Pg>>::Output>>` is not satisfied
  --> src/main.rs:21:5
   |
21 |     select(exists(check))
   |     ^^^^^^ the trait `SelectDsl<Exists<<T as BoxedDsl<'a, Pg>>::Output>>` is not implemented for `diesel::query_builder::SelectStatement<()>`
   |
help: consider extending the `where` bound, but there might be an alternative better way to express this requirement
   |
17 |     T: BoxedDsl<'a, Pg>, diesel::query_builder::SelectStatement<()>: SelectDsl<Exists<<T as BoxedDsl<'a, Pg>>::Output>>
   |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

Note that the third, fourth and fifth error message all point to the same line and all have a `= note: required because of the requirements on the impl of LoadQuery<_, bool> for diesel::query_builder::SelectStatement<(), query_builder::select_clause::SelectClause<Exists<<T as BoxedDsl<'a, Pg>>::Output>>>` line in their output.

<!-- The following is not always necessary. -->
Ideally the output should look like:

```
error[E0277]: the trait bound `<T as BoxedDsl<'a, Pg>>::Output: SelectQuery` is not satisfied
   --> src/main.rs:21:12
    |
21  |     select(exists(check))
    |            ^^^^^^^^^^^^^ the trait `SelectQuery` is not implemented for `<T as BoxedDsl<'a, Pg>>::Output`
    |
    = note: required because of the requirements on the impl of `diesel::Expression` for `Subselect<<T as BoxedDsl<'a, Pg>>::Output, ()>`
    = note: 1 redundant requirements hidden
    = note: required because of the requirements on the impl of `diesel::Expression` for `Exists<<T as BoxedDsl<'a, Pg>>::Output>`
note: required by a bound in `diesel::select`
   --> /home/weiznich/.cargo/registry/src/github.com-1ecc6299db9ec823/diesel-1.4.8/src/query_builder/functions.rs:375:8
    |
375 |     T: Expression,
    |        ^^^^^^^^^^ required by this bound in `diesel::select`
help: consider further restricting the associated type
    |
17  |     T: BoxedDsl<'a, Pg>, <T as BoxedDsl<'a, Pg>>::Output: SelectQuery
    |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0277]: the trait bound `<T as BoxedDsl<'a, Pg>>::Output: ValidSubselect<()>` is not satisfied
   --> src/main.rs:21:12
    |
21  |     select(exists(check))
    |            ^^^^^^^^^^^^^ the trait `ValidSubselect<()>` is not implemented for `<T as BoxedDsl<'a, Pg>>::Output`
    |
    = note: required because of the requirements on the impl of `SelectableExpression<()>` for `Subselect<<T as BoxedDsl<'a, Pg>>::Output, ()>`
    = note: 1 redundant requirements hidden
    = note: required because of the requirements on the impl of `SelectableExpression<()>` for `Exists<<T as BoxedDsl<'a, Pg>>::Output>`
    = note: required because of the requirements on the impl of `SelectDsl<Exists<<T as BoxedDsl<'a, Pg>>::Output>>` for `diesel::query_builder::SelectStatement<()>`
note: required by a bound in `diesel::select`
   --> /home/weiznich/.cargo/registry/src/github.com-1ecc6299db9ec823/diesel-1.4.8/src/query_builder/functions.rs:376:26
    |
376 |     SelectStatement<()>: SelectDsl<T>,
    |                          ^^^^^^^^^^^^ required by this bound in `diesel::select`
help: consider further restricting the associated type
    |
17  |     T: BoxedDsl<'a, Pg>, <T as BoxedDsl<'a, Pg>>::Output: ValidSubselect<()>
    |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0277]: the trait bound `diesel::query_builder::SelectStatement<(), query_builder::select_clause::SelectClause<Exists<<T as BoxedDsl<'a, Pg>>::Output>>>: LoadQuery<PgConnection, bool>` is not satisfied
  --> src/main.rs:22:10
   |
22 |         .get_result::<bool>(conn)?
   |          ^^^^^^^^^^ the trait `LoadQuery<PgConnection, bool>` is not implemented for `diesel::query_builder::SelectStatement<(), query_builder::select_clause::SelectClause<Exists<<T as BoxedDsl<'a, Pg>>::Output>>>`
   |
help:  consider extending the `where` bound, but there might be an alternative better way to express this requirement
   |
17 |     T: BoxedDsl<'a, Pg>, diesel::query_builder::SelectStatement<(), query_builder::select_clause::SelectClause<Exists<<T as BoxedDsl<'a, Pg>>::Output>>>: LoadQuery<PgConnection, bool>
   |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0277]: the trait bound `diesel::query_builder::SelectStatement<()>: SelectDsl<Exists<<T as BoxedDsl<'a, Pg>>::Output>>` is not satisfied
  --> src/main.rs:21:5
   |
21 |     select(exists(check))
   |     ^^^^^^ the trait `SelectDsl<Exists<<T as BoxedDsl<'a, Pg>>::Output>>` is not implemented for `diesel::query_builder::SelectStatement<()>`
   |
help: consider extending the `where` bound, but there might be an alternative better way to express this requirement
   |
17 |     T: BoxedDsl<'a, Pg>, diesel::query_builder::SelectStatement<()>: SelectDsl<Exists<<T as BoxedDsl<'a, Pg>>::Output>>
   |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

<!--
If the problem is not self-explanatory, please provide a rationale for the
change.
-->

For reference a fixed version of this function looks like:
```rust

fn check_exists<'a, T>(
    conn: &PgConnection,
    check: dsl::IntoBoxed<'a, T, Pg>,
) -> Result<(), diesel::result::Error>
where
    T: BoxedDsl<'a, Pg>,
    dsl::IntoBoxed<'a, T, Pg>: SelectQuery + ValidSubselect<()>,
    dsl::Select<SelectStatement<()>, Exists<dsl::IntoBoxed<'a, T, Pg>>>: LoadQuery<PgConnection, bool>,
{
    use diesel::dsl::{exists, select};

    select(exists(check))
        .get_result::<bool>(conn)?
        .then(|| {})
        .expect("Does not exist");

    Ok(())
}
```

See https://github.com/diesel-rs/diesel/discussions/2936#discussioncomment-1604902 for a longer discussion of the corresponding error messages.

<!--
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.
-->