title: Function returning reference to self can't compile, while the same code can
body: <p>Given two snippets, which tries to return reference to a member value from self, I really can't understand why the one with separate function doesn't compile.</p>
  <p>This works (<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=58d1ba564e2a52fe2ac0eb395a363614" rel="nofollow noreferrer">godbolt</a>):</p>
  <pre class="lang-rust prettyprint-override"><code>struct Foo {
      a: bool,
      b: u32,
  }
  
  impl Foo {
      fn get(&amp;mut self) -&gt; &amp;u32 {
          if self.a {
              return &amp;self.b;
          }
          
          self.a = true;
          self.b = 10;
          
          return &amp;self.b;
      }
  }
  </code></pre>
  <p>While this doesn't (<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=18eb0c2fec5eac77227a90c245d7041e" rel="nofollow noreferrer">godbolt</a>):</p>
  <pre class="lang-rust prettyprint-override"><code>struct Foo {
      a: bool,
      b: u32,
  }
  
  impl Foo {
      fn try_get(&amp;self) -&gt; Option&lt;&amp;u32&gt; {
          if self.a {
              return Some(&amp;self.b);
          }
          return None;
      }
      
      fn get(&amp;mut self) -&gt; &amp;u32 {
          if let Some(val) = self.try_get() {
              return val;
          }
          
          self.a = true;
          self.b = 10;
          
          return &amp;self.b;
      }
  }
  </code></pre>
  <p>Error:</p>
  <pre><code>error[E0506]: cannot assign to `self.a` because it is borrowed
    --&gt; src/lib.rs:19:9
     |
  14 |     fn get(&amp;mut self) -&gt; &amp;u32 {
     |            - let's call the lifetime of this reference `'1`
  15 |         if let Some(val) = self.try_get() {
     |                            -------------- borrow of `self.a` occurs here
  16 |             return val;
     |                    --- returning this value requires that `*self` is borrowed for `'1`
  ...
  19 |         self.a = true;
     |         ^^^^^^^^^^^^^ assignment to borrowed `self.a` occurs here
  </code></pre>
  <p>As I understand, it can't see, that reference at the top will be released before mutable change, but why? It can prove it without the function, why can't it do it with?<br />
  Is there any way to make function call work?<br />
  For me this really looks like a bug</p>