Repository: rust
Issue: Bad diagnostic on second borrow of mutable self
Labels: A-diagnostics,T-compiler,D-confusing

<!--
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.
-->

The following code:

```rust
use std::collections::HashMap;

struct Node {}

struct Example {
    nodes: HashMap<usize, Node>,
    edges: HashMap<usize, Vec<usize>>,
}

impl Example {
    pub fn why_no_work(&mut self) -> Vec<&Node> {
        let edges = self.edges.get(&0).unwrap();

        edges
            .iter()
            .map(move |id| self.nodes.get(id).unwrap())
            .collect()
    }
}
```

Gives the following diagnostics:

```
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
  --> src/lib.rs:16:39
   |
16 |             .map(move |id| self.nodes.get(id).unwrap())
   |                                       ^^^
   |
note: first, the lifetime cannot outlive the lifetime `'_` as defined on the body at 16:18...
  --> src/lib.rs:16:18
   |
16 |             .map(move |id| self.nodes.get(id).unwrap())
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that closure can access `self`
  --> src/lib.rs:16:28
   |
16 |             .map(move |id| self.nodes.get(id).unwrap())
   |                            ^^^^^^^^^^
note: but, the lifetime must be valid for the anonymous lifetime #1 defined on the method body at 11:5...
  --> src/lib.rs:11:5
   |
11 |     pub fn why_no_work(&mut self) -> Vec<&Node> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that the expression is assignable
  --> src/lib.rs:14:9
   |
14 | /         edges
15 | |             .iter()
16 | |             .map(move |id| self.nodes.get(id).unwrap())
17 | |             .collect()
   | |______________________^
   = note: expected `Vec<&Node>`
              found `Vec<&Node>`
```

This diagnostic mentions `autoref`, anonymous lifetimes, and more which can be very confusing. One way to fix this is to not capture `self` which is a mutable reference into the closure passed to `map` like so:

```rust
use std::collections::HashMap;

struct Node {}

struct Example {
    nodes: HashMap<usize, Node>,
    edges: HashMap<usize, Vec<usize>>,
}

impl Example {
    pub fn why_no_work(&mut self) -> Vec<&Node> {
        let edges = self.edges.get(&0).unwrap();
        let nodes = &self.nodes;
        edges
            .iter()
            .map(move |id| nodes.get(id).unwrap())
            .collect()
    }
}
```

I'm not exactly sure what the right approach is, but perhaps pointing out that `self` is being captured by the closure could help. This likely will go away with RFC 2229 though. 

### Meta
<!--
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.
-->

This is based on an issue originally [reported on the Rust subreddit](https://www.reddit.com/r/rust/comments/n1dqc4/struggling_to_understand_why_i_cant_use_map_in_a/).

`rustc --version --verbose`:
```
rustc --version --verbose
rustc 1.51.0 (2fd73fabe 2021-03-23)
binary: rustc
commit-hash: 2fd73fabe469357a12c2c974c140f67e7cdd76d0
commit-date: 2021-03-23
host: x86_64-unknown-linux-gnu
release: 1.51.0
LLVM version: 11.0.1
```