<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="pub struct FragmentRepr(Vec&lt;FragmentRepr&gt;);
pub fn accepted(mut entry: &amp;mut FragmentRepr) {
    loop {
        let FragmentRepr(ref mut discrs) = *{entry};
        entry = &amp;mut discrs[0];
    }
}
pub fn rejected(mut entry: &amp;mut FragmentRepr) {
    loop {
        let FragmentRepr(ref mut discrs) = *entry;
        entry = &amp;mut discrs[0];
    }
}
fn main() {}"><pre class="notranslate"><span class="pl-k">pub</span> <span class="pl-k">struct</span> <span class="pl-smi">FragmentRepr</span><span class="pl-kos">(</span><span class="pl-smi">Vec</span><span class="pl-kos">&lt;</span><span class="pl-smi">FragmentRepr</span><span class="pl-kos">&gt;</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-k">pub</span> <span class="pl-k">fn</span> <span class="pl-en">accepted</span><span class="pl-kos">(</span><span class="pl-k">mut</span> <span class="pl-s1">entry</span><span class="pl-kos">:</span> <span class="pl-c1">&amp;</span><span class="pl-k">mut</span> <span class="pl-smi">FragmentRepr</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">loop</span> <span class="pl-kos">{</span>
        <span class="pl-k">let</span> <span class="pl-v">FragmentRepr</span><span class="pl-kos">(</span><span class="pl-k">ref</span> <span class="pl-k">mut</span> discrs<span class="pl-kos">)</span> = <span class="pl-c1">*</span><span class="pl-kos">{</span>entry<span class="pl-kos">}</span><span class="pl-kos">;</span>
        entry = <span class="pl-c1">&amp;</span><span class="pl-k">mut</span> discrs<span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span>
<span class="pl-k">pub</span> <span class="pl-k">fn</span> <span class="pl-en">rejected</span><span class="pl-kos">(</span><span class="pl-k">mut</span> <span class="pl-s1">entry</span><span class="pl-kos">:</span> <span class="pl-c1">&amp;</span><span class="pl-k">mut</span> <span class="pl-smi">FragmentRepr</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">loop</span> <span class="pl-kos">{</span>
        <span class="pl-k">let</span> <span class="pl-v">FragmentRepr</span><span class="pl-kos">(</span><span class="pl-k">ref</span> <span class="pl-k">mut</span> discrs<span class="pl-kos">)</span> = <span class="pl-c1">*</span>entry<span class="pl-kos">;</span>
        entry = <span class="pl-c1">&amp;</span><span class="pl-k">mut</span> discrs<span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span>
<span class="pl-k">fn</span> <span class="pl-en">main</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span><span class="pl-kos">}</span></pre></div>
<p dir="auto">Rust accepts the first function, but rejects the second with "error: cannot borrow <code class="notranslate">entry.0</code> as mutable more than once at a time".  It would be nice not to be forced to insert random curly braces to convince the borrow checker that my code is correct.</p>
<p dir="auto">UPDATE: This example works now, but needs someone to add a test. See <a href="https://github.com/rust-lang/rust/issues/46361#issuecomment-357991656" data-hovercard-type="issue" data-hovercard-url="/rust-lang/rust/issues/46361/hovercard">mentoring instructions</a> below.</p>
<hr>
<p dir="auto">I'm trying to compile some examples using nonlexical lifetimes. To do so, I'm compiling rustc using the <code class="notranslate">master</code> branch, and the <code class="notranslate">-Z nll</code> compiler option.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="rustc 1.24.0-dev
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.24.0-dev
LLVM version: 4.0"><pre class="notranslate"><code class="notranslate">rustc 1.24.0-dev
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.24.0-dev
LLVM version: 4.0
</code></pre></div>
<p dir="auto">By compiling "Problem case 4: mutating &amp;mut references" from <a href="https://github.com/nikomatsakis/nll-rfc/blob/master/0000-nonlexical-lifetimes.md">nll-rfc</a>, I get some errors.</p>
<details>
<summary>Program</summary>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="struct List&lt;T&gt; {
    value: T,
    next: Option&lt;Box&lt;List&lt;T&gt;&gt;&gt;,
}

fn to_refs&lt;T&gt;(mut list: &amp;mut List&lt;T&gt;) -&gt; Vec&lt;&amp;mut T&gt; {
    let mut result = vec![];
    loop {
        result.push(&amp;mut list.value);
        if let Some(n) = list.next.as_mut() {
            list = &amp;mut n;
        } else {
            return result;
        }
    }
}

fn main() {
    println!(&quot;Hello World!&quot;);
}"><pre class="notranslate"><span class="pl-k">struct</span> <span class="pl-smi">List</span><span class="pl-kos">&lt;</span><span class="pl-smi">T</span><span class="pl-kos">&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-c1">value</span><span class="pl-kos">:</span> <span class="pl-smi">T</span><span class="pl-kos">,</span>
    <span class="pl-c1">next</span><span class="pl-kos">:</span> <span class="pl-smi">Option</span><span class="pl-kos">&lt;</span><span class="pl-smi">Box</span><span class="pl-kos">&lt;</span><span class="pl-smi">List</span><span class="pl-kos">&lt;</span><span class="pl-smi">T</span><span class="pl-kos">&gt;</span><span class="pl-kos">&gt;</span><span class="pl-kos">&gt;</span><span class="pl-kos">,</span>
<span class="pl-kos">}</span>

<span class="pl-k">fn</span> <span class="pl-en">to_refs</span><span class="pl-kos">&lt;</span><span class="pl-smi">T</span><span class="pl-kos">&gt;</span><span class="pl-kos">(</span><span class="pl-k">mut</span> <span class="pl-s1">list</span><span class="pl-kos">:</span> <span class="pl-c1">&amp;</span><span class="pl-k">mut</span> <span class="pl-smi">List</span><span class="pl-kos">&lt;</span><span class="pl-smi">T</span><span class="pl-kos">&gt;</span><span class="pl-kos">)</span> -&gt; <span class="pl-smi">Vec</span><span class="pl-kos">&lt;</span><span class="pl-c1">&amp;</span><span class="pl-k">mut</span> <span class="pl-smi">T</span><span class="pl-kos">&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-k">let</span> <span class="pl-k">mut</span> result = <span class="pl-en">vec</span><span class="pl-en">!</span><span class="pl-kos">[</span><span class="pl-kos">]</span><span class="pl-kos">;</span>
    <span class="pl-k">loop</span> <span class="pl-kos">{</span>
        result<span class="pl-kos">.</span><span class="pl-en">push</span><span class="pl-kos">(</span><span class="pl-c1">&amp;</span><span class="pl-k">mut</span> list<span class="pl-kos">.</span><span class="pl-c1">value</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
        <span class="pl-k">if</span> <span class="pl-k">let</span> <span class="pl-v">Some</span><span class="pl-kos">(</span>n<span class="pl-kos">)</span> = list<span class="pl-kos">.</span><span class="pl-c1">next</span><span class="pl-kos">.</span><span class="pl-en">as_mut</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
            list = <span class="pl-c1">&amp;</span><span class="pl-k">mut</span> n<span class="pl-kos">;</span>
        <span class="pl-kos">}</span> <span class="pl-k">else</span> <span class="pl-kos">{</span>
            <span class="pl-k">return</span> result<span class="pl-kos">;</span>
        <span class="pl-kos">}</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span>

<span class="pl-k">fn</span> <span class="pl-en">main</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-en">println</span><span class="pl-en">!</span><span class="pl-kos">(</span><span class="pl-s">"Hello World!"</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
</details>
<details>
<summary>Output of `rustc -g -Zverbose -Znll nll-problem-case-4.rs`:</summary>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="error[E0597]: `n` does not live long enough
  --&gt; nll-problem-case-4.rs:11:25
   |
11 |             list = &amp;mut n;
   |                         ^ does not live long enough
...
14 |         }
   |         - borrowed value only lives until here
   |
note: borrowed value must be valid for the anonymous lifetime #1 defined on the function body at 6:1...
  --&gt; nll-problem-case-4.rs:6:1
   |
6  | / fn to_refs&lt;T&gt;(mut list: &amp;mut List&lt;T&gt;) -&gt; Vec&lt;&amp;mut T&gt; {
7  | |     let mut result = vec![];
8  | |     loop {
9  | |         result.push(&amp;mut list.value);
...  |
15 | |     }
16 | | }
   | |_^

error[E0499]: cannot borrow `list.value` as mutable more than once at a time
  --&gt; nll-problem-case-4.rs:9:26
   |
9  |         result.push(&amp;mut list.value);
   |                          ^^^^^^^^^^ mutable borrow starts here in previous iteration of loop
...
16 | }
   | - mutable borrow ends here

error[E0499]: cannot borrow `list.next` as mutable more than once at a time
  --&gt; nll-problem-case-4.rs:10:26
   |
10 |         if let Some(n) = list.next.as_mut() {
   |                          ^^^^^^^^^ mutable borrow starts here in previous iteration of loop
...
16 | }
   | - mutable borrow ends here

error[E0506]: cannot assign to `list` because it is borrowed
  --&gt; nll-problem-case-4.rs:11:13
   |
9  |         result.push(&amp;mut list.value);
   |                          ---------- borrow of `list` occurs here
10 |         if let Some(n) = list.next.as_mut() {
11 |             list = &amp;mut n;
   |             ^^^^^^^^^^^^^ assignment to borrowed `list` occurs here

error: aborting due to 4 previous errors"><pre class="notranslate"><code class="notranslate">error[E0597]: `n` does not live long enough
  --&gt; nll-problem-case-4.rs:11:25
   |
11 |             list = &amp;mut n;
   |                         ^ does not live long enough
...
14 |         }
   |         - borrowed value only lives until here
   |
note: borrowed value must be valid for the anonymous lifetime #1 defined on the function body at 6:1...
  --&gt; nll-problem-case-4.rs:6:1
   |
6  | / fn to_refs&lt;T&gt;(mut list: &amp;mut List&lt;T&gt;) -&gt; Vec&lt;&amp;mut T&gt; {
7  | |     let mut result = vec![];
8  | |     loop {
9  | |         result.push(&amp;mut list.value);
...  |
15 | |     }
16 | | }
   | |_^

error[E0499]: cannot borrow `list.value` as mutable more than once at a time
  --&gt; nll-problem-case-4.rs:9:26
   |
9  |         result.push(&amp;mut list.value);
   |                          ^^^^^^^^^^ mutable borrow starts here in previous iteration of loop
...
16 | }
   | - mutable borrow ends here

error[E0499]: cannot borrow `list.next` as mutable more than once at a time
  --&gt; nll-problem-case-4.rs:10:26
   |
10 |         if let Some(n) = list.next.as_mut() {
   |                          ^^^^^^^^^ mutable borrow starts here in previous iteration of loop
...
16 | }
   | - mutable borrow ends here

error[E0506]: cannot assign to `list` because it is borrowed
  --&gt; nll-problem-case-4.rs:11:13
   |
9  |         result.push(&amp;mut list.value);
   |                          ---------- borrow of `list` occurs here
10 |         if let Some(n) = list.next.as_mut() {
11 |             list = &amp;mut n;
   |             ^^^^^^^^^^^^^ assignment to borrowed `list` occurs here

error: aborting due to 4 previous errors
</code></pre></div>
</details>
<br>
<p dir="auto">My questions are:</p>
<ul dir="auto">
<li>Am I enabling correctly the experimental nll? What confuses me is that I compiled <code class="notranslate">rustc</code> with <code class="notranslate">./configure --enable-debug --enable-debuginfo</code>, but I still don't see the <code class="notranslate">debug!("mir_borrowck done");</code> message from the compiler.</li>
<li>Is "problem case 4" supposed to compile without errors in the future, when nll will be stable?</li>
</ul>