Repository: rust
Issue: Diagnostic regression for proc-macro generated code
Labels: A-diagnostics,P-medium,regression-from-stable-to-stable,C-bug,E-needs-mcve,D-verbose

<!--
Thank you for filing a regression report! 🐛 A regression is something that changed between versions of Rust but was not supposed to.

Please provide a short summary of the regression, along with any information you feel is relevant to replicate it.
-->

### Code

I tried this code:

```rust
use diesel::prelude::*; // 2.1.5

table! {
    users(id) {
        id -> Integer,
        name -> Text,
    }
}

#[derive(Insertable)]
struct User {
    id: String,
    name: i32,
}

fn main() {}

```

I expected to see a meaningful error that points to the relevant fields that cause this issue:

```
error[E0277]: the trait bound `String: diesel::Expression` is not satisfied
  --> src/main.rs:12:5
   |
12 |     id: String,
   |     ^^ the trait `diesel::Expression` is not implemented for `String`
   |
   = help: the following other types implement trait `diesel::Expression`:
             columns::name
             columns::id
             Box<T>
             columns::star
             diesel::expression::ops::numeric::Add<Lhs, Rhs>
             diesel::expression::ops::numeric::Sub<Lhs, Rhs>
             diesel::expression::ops::numeric::Mul<Lhs, Rhs>
             diesel::expression::ops::numeric::Div<Lhs, Rhs>
           and 119 others
   = note: required for `String` to implement `AsExpression<diesel::sql_types::Integer>`

error[E0277]: the trait bound `i32: diesel::Expression` is not satisfied
  --> src/main.rs:13:5
   |
13 |     name: i32,
   |     ^^^^ the trait `diesel::Expression` is not implemented for `i32`
   |
   = help: the following other types implement trait `diesel::Expression`:
             columns::name
             columns::id
             Box<T>
             columns::star
             diesel::expression::ops::numeric::Add<Lhs, Rhs>
             diesel::expression::ops::numeric::Sub<Lhs, Rhs>
             diesel::expression::ops::numeric::Mul<Lhs, Rhs>
             diesel::expression::ops::numeric::Div<Lhs, Rhs>
           and 119 others
   = note: required for `i32` to implement `AsExpression<diesel::sql_types::Text>`

error[E0277]: the trait bound `String: diesel::Expression` is not satisfied
  --> src/main.rs:12:5
   |
12 |     id: String,
   |     ^^ the trait `diesel::Expression` is not implemented for `String`
   |
   = help: the following other types implement trait `diesel::Expression`:
             columns::name
             columns::id
             Box<T>
             columns::star
             diesel::expression::ops::numeric::Add<Lhs, Rhs>
             diesel::expression::ops::numeric::Sub<Lhs, Rhs>
             diesel::expression::ops::numeric::Mul<Lhs, Rhs>
             diesel::expression::ops::numeric::Div<Lhs, Rhs>
           and 119 others
   = note: required for `&'insert String` to implement `diesel::Expression`
   = note: required for `&'insert String` to implement `AsExpression<diesel::sql_types::Integer>`

error[E0277]: the trait bound `i32: diesel::Expression` is not satisfied
  --> src/main.rs:13:5
   |
13 |     name: i32,
   |     ^^^^ the trait `diesel::Expression` is not implemented for `i32`
   |
   = help: the following other types implement trait `diesel::Expression`:
             columns::name
             columns::id
             Box<T>
             columns::star
             diesel::expression::ops::numeric::Add<Lhs, Rhs>
             diesel::expression::ops::numeric::Sub<Lhs, Rhs>
             diesel::expression::ops::numeric::Mul<Lhs, Rhs>
             diesel::expression::ops::numeric::Div<Lhs, Rhs>
           and 119 others
   = note: required for `&'insert i32` to implement `diesel::Expression`
   = note: required for `&'insert i32` to implement `AsExpression<diesel::sql_types::Text>`

```

Instead, this happened: Rustc emits an error message that duplicates the actual error several times. First it only points to the derive, later versions of the same error message point to the correct fields:

```
error[E0277]: the trait bound `String: diesel::Expression` is not satisfied
  --> src/main.rs:10:10
   |
10 | #[derive(Insertable)]
   |          ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `String`, which is required by `String: AsExpression<diesel::sql_types::Integer>`
   |
   = help: the following other types implement trait `diesel::Expression`:
             &'a T
             (T0, T1)
             (T0, T1, T2)
             (T0, T1, T2, T3)
             (T0, T1, T2, T3, T4)
             (T0, T1, T2, T3, T4, T5)
             (T0, T1, T2, T3, T4, T5, T6)
             (T0, T1, T2, T3, T4, T5, T6, T7)
           and 119 others
   = note: required for `String` to implement `AsExpression<diesel::sql_types::Integer>`
   = note: this error originates in the derive macro `Insertable` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `i32: diesel::Expression` is not satisfied
  --> src/main.rs:10:10
   |
10 | #[derive(Insertable)]
   |          ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `i32`, which is required by `i32: AsExpression<diesel::sql_types::Text>`
   |
   = help: the following other types implement trait `diesel::Expression`:
             &'a T
             (T0, T1)
             (T0, T1, T2)
             (T0, T1, T2, T3)
             (T0, T1, T2, T3, T4)
             (T0, T1, T2, T3, T4, T5)
             (T0, T1, T2, T3, T4, T5, T6)
             (T0, T1, T2, T3, T4, T5, T6, T7)
           and 119 others
   = note: required for `i32` to implement `AsExpression<diesel::sql_types::Text>`
   = note: this error originates in the derive macro `Insertable` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `String: diesel::Expression` is not satisfied
  --> src/main.rs:10:10
   |
10 | #[derive(Insertable)]
   |          ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `String`, which is required by `&'insert String: AsExpression<diesel::sql_types::Integer>`
   |
   = help: the following other types implement trait `diesel::Expression`:
             &'a T
             (T0, T1)
             (T0, T1, T2)
             (T0, T1, T2, T3)
             (T0, T1, T2, T3, T4)
             (T0, T1, T2, T3, T4, T5)
             (T0, T1, T2, T3, T4, T5, T6)
             (T0, T1, T2, T3, T4, T5, T6, T7)
           and 119 others
   = note: required for `&'insert String` to implement `diesel::Expression`
   = note: required for `&'insert String` to implement `AsExpression<diesel::sql_types::Integer>`
   = note: this error originates in the derive macro `Insertable` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `i32: diesel::Expression` is not satisfied
  --> src/main.rs:10:10
   |
10 | #[derive(Insertable)]
   |          ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `i32`, which is required by `&'insert i32: AsExpression<diesel::sql_types::Text>`
   |
   = help: the following other types implement trait `diesel::Expression`:
             &'a T
             (T0, T1)
             (T0, T1, T2)
             (T0, T1, T2, T3)
             (T0, T1, T2, T3, T4)
             (T0, T1, T2, T3, T4, T5)
             (T0, T1, T2, T3, T4, T5, T6)
             (T0, T1, T2, T3, T4, T5, T6, T7)
           and 119 others
   = note: required for `&'insert i32` to implement `diesel::Expression`
   = note: required for `&'insert i32` to implement `AsExpression<diesel::sql_types::Text>`
   = note: this error originates in the derive macro `Insertable` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `String: diesel::Expression` is not satisfied
  --> src/main.rs:12:5
   |
12 |     id: String,
   |     ^^ the trait `diesel::Expression` is not implemented for `String`, which is required by `String: AsExpression<diesel::sql_types::Integer>`
   |
   = help: the following other types implement trait `diesel::Expression`:
             &'a T
             (T0, T1)
             (T0, T1, T2)
             (T0, T1, T2, T3)
             (T0, T1, T2, T3, T4)
             (T0, T1, T2, T3, T4, T5)
             (T0, T1, T2, T3, T4, T5, T6)
             (T0, T1, T2, T3, T4, T5, T6, T7)
           and 119 others
   = note: required for `String` to implement `AsExpression<diesel::sql_types::Integer>`

error[E0277]: the trait bound `i32: diesel::Expression` is not satisfied
  --> src/main.rs:13:5
   |
13 |     name: i32,
   |     ^^^^ the trait `diesel::Expression` is not implemented for `i32`, which is required by `i32: AsExpression<diesel::sql_types::Text>`
   |
   = help: the following other types implement trait `diesel::Expression`:
             &'a T
             (T0, T1)
             (T0, T1, T2)
             (T0, T1, T2, T3)
             (T0, T1, T2, T3, T4)
             (T0, T1, T2, T3, T4, T5)
             (T0, T1, T2, T3, T4, T5, T6)
             (T0, T1, T2, T3, T4, T5, T6, T7)
           and 119 others
   = note: required for `i32` to implement `AsExpression<diesel::sql_types::Text>`

error[E0277]: the trait bound `String: diesel::Expression` is not satisfied
  --> src/main.rs:12:5
   |
12 |     id: String,
   |     ^^ the trait `diesel::Expression` is not implemented for `String`, which is required by `&'insert String: AsExpression<diesel::sql_types::Integer>`
   |
   = help: the following other types implement trait `diesel::Expression`:
             &'a T
             (T0, T1)
             (T0, T1, T2)
             (T0, T1, T2, T3)
             (T0, T1, T2, T3, T4)
             (T0, T1, T2, T3, T4, T5)
             (T0, T1, T2, T3, T4, T5, T6)
             (T0, T1, T2, T3, T4, T5, T6, T7)
           and 119 others
   = note: required for `&'insert String` to implement `diesel::Expression`
   = note: required for `&'insert String` to implement `AsExpression<diesel::sql_types::Integer>`

error[E0277]: the trait bound `i32: diesel::Expression` is not satisfied
  --> src/main.rs:13:5
   |
13 |     name: i32,
   |     ^^^^ the trait `diesel::Expression` is not implemented for `i32`, which is required by `&'insert i32: AsExpression<diesel::sql_types::Text>`
   |
   = help: the following other types implement trait `diesel::Expression`:
             &'a T
             (T0, T1)
             (T0, T1, T2)
             (T0, T1, T2, T3)
             (T0, T1, T2, T3, T4)
             (T0, T1, T2, T3, T4, T5)
             (T0, T1, T2, T3, T4, T5, T6)
             (T0, T1, T2, T3, T4, T5, T6, T7)
           and 119 others
   = note: required for `&'insert i32` to implement `diesel::Expression`
   = note: required for `&'insert i32` to implement `AsExpression<diesel::sql_types::Text>`

```

### Version it worked on

It most recently worked on: Rust 1.74

### Version with regression



`rustc --version --verbose`:
```
rustc 1.77.0 (aedd173a2 2024-03-17)
binary: rustc
commit-hash: aedd173a2c086e558c2b66d3743b344f977621a7
commit-date: 2024-03-17
host: x86_64-unknown-linux-gnu
release: 1.77.0
LLVM version: 17.0.6
```

I've checked the stable rust versions in between and the error message first changes in 1.75 which introduces the messages that point to `#[derive(Insertable)]` but back then these were shown at the end of the output instead as first error message. Bisecting between the two stable releases points to https://github.com/rust-lang/rust/pull/117159 as PR that introduced the duplicated messages.

<details>
<summary> Bisect Log</summary>

Regression in https://github.com/rust-lang-ci/rust/commit/dc8cfe13cc0d7e7d320f9be78932717fe80846a8
The PR introducing the regression in this rollup is #117159: Work around the fact that `check_mod_type_wf` may spuriousl…

searched nightlies: from nightly-2023-10-01 to nightly-2023-11-11
regressed nightly: nightly-2023-10-26
searched commit range: https://github.com/rust-lang/rust/compare/df871fbf053de3a855398964cd05fadbe91cf4fd...ab5c841a1f3c09edc5ea07722519627c960aed17
regressed commit: https://github.com/rust-lang/rust/commit/ab5c841a1f3c09edc5ea07722519627c960aed17

<details>
<summary>bisected with <a href='https://github.com/rust-lang/cargo-bisect-rustc'>cargo-bisect-rustc</a> v0.6.8</summary>


Host triple: x86_64-unknown-linux-gnu
Reproduce with:
```bash
cargo bisect-rustc --start 1.74.0 --end 1.75.0 --prompt 
```
</details>

</details>

The error then changed again between 1.76.0 and 1.77.0 by moving the message mentioning `#[derive(Insertable)]` at the begin of the error output. Bisecting points to https://github.com/rust-lang/rust/commit/791a53f380d5cf800191f25941c94ace5099876e as the commit that introduced that change.

<details>
<summary> Bisect Log</summary>
Attempting to search unrolled perf builds
ERROR: couldn't find perf build comment


searched nightlies: from nightly-2023-12-21 to nightly-2024-02-01
regressed nightly: nightly-2024-01-06
searched commit range: https://github.com/rust-lang/rust/compare/f688dd684faca5b31b156fac2c6e0ae81fc9bc90...595bc6f00369475047538fdae1ff8cea692ac385
regressed commit: https://github.com/rust-lang/rust/commit/791a53f380d5cf800191f25941c94ace5099876e

<details>
<summary>bisected with <a href='https://github.com/rust-lang/cargo-bisect-rustc'>cargo-bisect-rustc</a> v0.6.8</summary>


Host triple: x86_64-unknown-linux-gnu
Reproduce with:
```bash
cargo bisect-rustc --start 1.76.0 --end 1.77.0 --prompt 
```
</details>

</details>

@rustbot modify labels: +regression-from-stable-to-stable -regression-untriaged +A-diagnostics