Repository: rust
Issue: E0495 Diagnostic regression - missing actual source of error
Labels: A-diagnostics,T-compiler

<!--
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: [playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=8fd726cb8a57173a376f00028de996f8) on nightly `2022-08-07`

```rust
use std::marker::PhantomData;

struct Invariant<'ctx>(&'ctx mut &'ctx ());
#[derive(Clone, Copy)]
struct Token<'ctx>(PhantomData<Invariant<'ctx>>);

pub struct Builder<'ctx> {
    _ctx: PhantomData<Invariant<'ctx>>,
}

#[derive(Clone, Copy)]
pub struct State<'ctx> {
    _ctx: PhantomData<Invariant<'ctx>>,
}

impl<'ctx> Builder<'ctx> {
    pub fn with(f: impl FnOnce(&mut Builder<'_>)) {
        let mut builder = Builder {
            _ctx: PhantomData,
        };

        f(&mut builder);
    }

    pub fn new_state(&mut self) -> State<'ctx> {
        State { _ctx: PhantomData }
    }

    pub fn accept(&mut self, _state: State<'ctx>) {}
}

fn test() {
    Builder::with(|b1| {
        Builder::with(|b2| {
            let state = b2.new_state();
            b1.accept(state);
        });
    });
}
```

The current output is as follows:

Note how the actual erroneous function call is completely missing! (`a.check_same(b.0)`)

```rs
Compiling playground v0.0.1 (/playground)
error[[E0521]](https://doc.rust-lang.org/nightly/error-index.html#E0521): borrowed data escapes outside of closure
  --> src/lib.rs:35:25
   |
33 |     Builder::with(|b1| {
   |                    -- `b1` declared here, outside of the closure body
34 |         Builder::with(|b2| {
   |                        -- `b2` is a reference that is only valid in the closure body
35 |             let state = b2.new_state();
   |                         ^^^^^^^^^^^^^^ `b2` escapes the closure body here
   |
   = note: requirement occurs because of a mutable reference to `Builder<'_>`
   = note: mutable references are invariant over their type parameter
   = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance

error[[E0521]](https://doc.rust-lang.org/nightly/error-index.html#E0521): borrowed data escapes outside of closure
  --> src/lib.rs:35:25
   |
33 |     Builder::with(|b1| {
   |                    --
   |                    |
   |                    `b1` is a reference that is only valid in the closure body
   |                    has type `&mut Builder<'1>`
34 |         Builder::with(|b2| {
35 |             let state = b2.new_state();
   |                         ^^^^^^^^^^^^^^
   |                         |
   |                         `b1` escapes the closure body here
   |                         argument requires that `'1` must outlive `'static`

For more information about this error, try `rustc --explain E0521`.
error: could not compile `playground` due to 2 previous errors
```

<!-- The following is not always necessary. -->
Ideally the output should look like: (NOTE: this is the output of the last stable release, `1.62.1`)

Note how the actual erroneous function call listed as the source of the error.

```rs
Compiling playground v0.0.1 (/playground)
error[[E0495]](https://doc.rust-lang.org/stable/error-index.html#E0495): cannot infer an appropriate lifetime for lifetime parameter `'ctx` due to conflicting requirements
  --> src/lib.rs:35:28
   |
35 |             let state = b2.new_state();
   |                            ^^^^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined here...
  --> src/lib.rs:33:19
   |
33 |       Builder::with(|b1| {
   |  ___________________^
34 | |         Builder::with(|b2| {
35 | |             let state = b2.new_state();
36 | |             b1.accept(state);
37 | |         });
38 | |     });
   | |_____^
note: ...but the lifetime must also be valid for the anonymous lifetime #2 defined here...
  --> src/lib.rs:34:23
   |
34 |           Builder::with(|b2| {
   |  _______________________^
35 | |             let state = b2.new_state();
36 | |             b1.accept(state);
37 | |         });
   | |_________^
note: ...so that the types are compatible
  --> src/lib.rs:36:16
   |
36 |             b1.accept(state);
   |                ^^^^^^
   = note: expected `&mut Builder<'_>`
              found `&mut Builder<'_>`

For more information about this error, try `rustc --explain E0495`.
error: could not compile `playground` due to previous error
```

<!--
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.
-->