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

Borrowing an immutable reference of a mutable reference through a function call in a loop is not accepted #51132

Open
shepmaster opened this Issue May 28, 2018 · 2 comments

Comments

Projects
None yet
5 participants
@shepmaster
Copy link
Member

shepmaster commented May 28, 2018

This code appears to be sound because event should either leave the loop or be thrown away before the next iteration:

#![feature(nll)]

fn next<'buf>(buffer: &'buf mut String) -> &'buf str {
    loop {
        let event = parse(buffer);

        if true {
            return event;
        }
    }
}

fn parse<'buf>(_buffer: &'buf mut String) -> &'buf str {
    unimplemented!()
}

fn main() {}

The current (1.28.0-nightly 2018-05-25 990d8aa) implementation still marks this as an error:

error[E0499]: cannot borrow `*buffer` as mutable more than once at a time
 --> src/main.rs:5:27
  |
5 |         let event = parse(buffer);
  |                           ^^^^^^ mutable borrow starts here in previous iteration of loop
  |
note: borrowed value must be valid for the lifetime 'buf as defined on the function body at 3:1...
 --> src/main.rs:3:1
  |
3 | fn next<'buf>(buffer: &'buf mut String) -> &'buf str {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@nikomatsakis said:

this should throw an error (for now) but this error should eventually go away. This is precisely a case where "Location sensitivity" is needed, but we removed that feature in the name of performance — once polonius support lands, though, this code would be accepted. That may or may not be before the edition.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Jun 29, 2018

Tagging as NLL-deferred. This is a polonius problem. =)

@PvdBerg1998

This comment has been minimized.

Copy link

PvdBerg1998 commented Feb 8, 2019

Recently bumped into this problem. Would love to see this fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment