Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NLL no longer figures out non-lexical lifetimes within loop #51526

Open
daboross opened this issue Jun 12, 2018 · 1 comment
Open

NLL no longer figures out non-lexical lifetimes within loop #51526

daboross opened this issue Jun 12, 2018 · 1 comment
Labels
A-NLL Area: Non-lexical lifetimes (NLL) C-enhancement Category: An issue proposing an enhancement or a PR with one. NLL-polonius Issues related for using Polonius in the borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@daboross
Copy link
Contributor

daboross commented Jun 12, 2018

Minimal useful example:

#![feature(nll)]
use std::collections::VecDeque;

fn next(queue: &mut VecDeque<u32>, above: u32) -> Option<&u32> {
    let result = loop {
        {
            let next = queue.front()?;
            if *next > above {
                break next;
            }
        }
        queue.pop_front();
    };
    
    Some(result)
}

This program compiled in nightly-2018-03-07, but does not compile on nightly-2018-05-27. Instead, it fails with this error:

error[E0502]: cannot borrow `*queue` as mutable because it is also borrowed as immutable
  --> src/main.rs:12:9
   |
7  |             let next = queue.front()?;
   |                        ----- immutable borrow occurs here
...
12 |         queue.pop_front();
   |         ^^^^^^^^^^^^^^^^^ mutable borrow occurs here
   |

(playground)

To my understanding, this program is in fact sane. next is always dropped by the time queue.pop_front() runs, and if it escapes and is returned, queue.pop_front() is not run.

The error is the same if return Some(next); is used instead of break next;.

@jkordish jkordish added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-NLL Area: Non-lexical lifetimes (NLL) WG-compiler-nll labels Jun 13, 2018
@matthewjasper
Copy link
Contributor

This is expected due to #50593, but still a bug in the long term.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-NLL Area: Non-lexical lifetimes (NLL) C-enhancement Category: An issue proposing an enhancement or a PR with one. NLL-polonius Issues related for using Polonius in the borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants