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

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 · 4 comments
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-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@shepmaster
Copy link
Member

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.

@shepmaster shepmaster added E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. A-NLL Area: Non Lexical Lifetimes (NLL) WG-compiler-nll NLL-complete Working towards the "valid code works" goal labels May 28, 2018
@nikomatsakis nikomatsakis added NLL-deferred and removed NLL-complete Working towards the "valid code works" goal labels Jun 29, 2018
@nikomatsakis
Copy link
Contributor

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

@XAMPPRocky XAMPPRocky added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Oct 2, 2018
@matthewjasper matthewjasper added NLL-polonius Issues related for using Polonius in the borrow checker and removed NLL-deferred labels Dec 1, 2018
@PvdBerg1998
Copy link

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

@let4be
Copy link

let4be commented May 10, 2021

Any update on this?...

@Legend-of-iPhoenix
Copy link

Legend-of-iPhoenix commented Jul 6, 2021

It's worth noting for people coming across this issue in the future that inlining the function being called (in this case, parse) generally resolves this error- see https://stackoverflow.com/a/50570026

Not sure what to do when the function being called is sizable/too messy to inline, though :(

@JohnTitor JohnTitor removed the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label May 5, 2022
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-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants