Skip to content

Variable appears to be borrowed by itself with NLL #48001

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

Closed
Aaron1011 opened this issue Feb 4, 2018 · 4 comments
Closed

Variable appears to be borrowed by itself with NLL #48001

Aaron1011 opened this issue Feb 4, 2018 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-NLL Area: Non-lexical lifetimes (NLL) C-enhancement Category: An issue proposing an enhancement or a PR with one. NLL-complete Working towards the "valid code works" goal
Milestone

Comments

@Aaron1011
Copy link
Member

Compiling this code snippet:

#![feature(nll)]

struct Foo;

impl Foo {
    fn get_self(&mut self) -> Option<&mut Self> {
        Some(self)
    }

    fn new_self(&mut self) -> &mut Self {
        self
    }
    
    fn trigger_bug(&mut self) {
        let other = &mut (&mut *self);
        
        *other = match (*other).get_self() {
            Some(s) => s,
            None => (*other).new_self()
        };
        
        let c = other;
        
    }
}

fn main() {}

gives the following error:

error[E0505]: cannot move out of `other` because it is borrowed
  --> src/main.rs:22:17
   |
17 |         *other = match (*other).get_self() {
   |                        -------- borrow of `**other` occurs here
...
22 |         let c = other;
   |                 ^^^^^ move out of `other` occurs here

After the *other = ... assignment, it appears to be impossible to interact with other in any way, including moving and borrowing it. Effectively, it appears to be borrowed by itself.

@TimNN TimNN added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints A-NLL Area: Non-lexical lifetimes (NLL) labels Feb 6, 2018
@pnkfelix pnkfelix added WG-compiler-nll NLL-complete Working towards the "valid code works" goal labels May 8, 2018
@nikomatsakis
Copy link
Contributor

I believe this is because we only "kill" loans for assignments directly to local variables right now.

@nikomatsakis
Copy link
Contributor

Pushing to RC, this is a minor addition.

@nikomatsakis
Copy link
Contributor

This is also a dup of #46589, really, so maybe I'll just move the example there and close it.

@nikomatsakis
Copy link
Contributor

Closing as duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-NLL Area: Non-lexical lifetimes (NLL) C-enhancement Category: An issue proposing an enhancement or a PR with one. NLL-complete Working towards the "valid code works" goal
Projects
None yet
Development

No branches or pull requests

4 participants