Repository: rust
Issue: Confusing lifetime suggestion with multiple references and impl Trait
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 link](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=acbea93c84794b22e175b4359e41ccfa))

```rust
fn pick_n<R: rand::Rng, T: Copy>(rng: &mut R, n: usize, vs: &[T]) -> impl Iterator<Item = T> {
    let mut mask = vec![true; vs.len()];
    (0..n).map(move |_| loop {
        let k = rng.gen_range(0..vs.len());
        if mask[k] {
            mask[k] = false;
            break vs[k];
        }
    })
}
```

The current output is:

```
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
 --> src/lib.rs:1:70
  |
1 | fn pick_n<R: rand::Rng, T: Copy>(rng: &mut R, n: usize, vs: &[T]) -> impl Iterator<Item = T> {
  |                                       ------                         ^^^^^^^^^^^^^^^^^^^^^^^
  |                                       |
  |                                       hidden type `Map<std::ops::Range<usize>, [closure@src/lib.rs:3:16: 9:6]>` captures the anonymous lifetime defined here
  |
help: to declare that the `impl Trait` captures `'_`, you can add an explicit `'_` lifetime bound
  |
1 | fn pick_n<R: rand::Rng, T: Copy>(rng: &mut R, n: usize, vs: &[T]) -> impl Iterator<Item = T> + '_ {
  |                                                                                              ++++

error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
 --> src/lib.rs:1:70
  |
1 | fn pick_n<R: rand::Rng, T: Copy>(rng: &mut R, n: usize, vs: &[T]) -> impl Iterator<Item = T> {
  |                                                             ----     ^^^^^^^^^^^^^^^^^^^^^^^
  |                                                             |
  |                                                             hidden type `Map<std::ops::Range<usize>, [closure@src/lib.rs:3:16: 9:6]>` captures the anonymous lifetime defined here
  |
help: to declare that the `impl Trait` captures `'_`, you can add an explicit `'_` lifetime bound
  |
1 | fn pick_n<R: rand::Rng, T: Copy>(rng: &mut R, n: usize, vs: &[T]) -> impl Iterator<Item = T> + '_ {
  |                                                                                              ++++

For more information about this error, try `rustc --explain E0700`.
error: could not compile `playground` due to 2 previous errors
```

This is confusing, because making the suggested change (of adding `'_` to the `impl Trait`) doesn't actually work ([playground Link](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=a8785a3e0196a2e54f3a0d97d5991ae4)):

```
error[E0106]: missing lifetime specifier
 --> src/lib.rs:1:96
  |
1 | fn pick_n<R: rand::Rng, T: Copy>(rng: &mut R, n: usize, vs: &[T]) -> impl Iterator<Item = T> + '_ {
  |                                       ------                ----                               ^^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `rng` or `vs`
help: consider introducing a named lifetime parameter
  |
1 | fn pick_n<'a, R: rand::Rng, T: Copy>(rng: &'a mut R, n: usize, vs: &'a [T]) -> impl Iterator<Item = T> + 'a {
  |           +++                              ++                       ++                                   ~~

For more information about this error, try `rustc --explain E0106`.
error: could not compile `playground` due to previous error
```

I'm not sure of the exact right text, but the output should suggest adding `'a` to all of the references _and_ the `impl Trait` right away, rather than suggesting adding `'_` to the `impl Trait`.