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

False borrow checker error: cannot use self.x because it was mutably borrowed #29975

Closed
istankovic opened this issue Nov 21, 2015 · 1 comment
Labels
A-borrow-checker Area: The borrow checker

Comments

@istankovic
Copy link
Contributor

Currently the borrow checker emits errors for simple cases like
the following:

struct Foo {
    x: isize,
}

impl Foo {
    fn foo(&mut self) {
        self.bar(self.x);
    }

    fn bar(&mut self, v: isize) {
        self.x = v + 1;
    }
}

fn main() {
}

The above results in

foo.rs:7:18: 7:24 error: cannot use `self.x` because it was mutably borrowed [E0503]
foo.rs:7         self.bar(self.x);
                          ^~~~~~
foo.rs:7:9: 7:13 note: borrow of `*self` occurs here
foo.rs:7         self.bar(self.x);
                 ^~~~
error: aborting due to previous error

This is with rustc 1.6.0-nightly (d5fde83ae 2015-11-12).
An ugly workaround for this is to make an explicit copy of self.x, e.g.

    fn foo(&mut self) {
        let tmp = self.x;
        self.bar(tmp);
    }
@Gankra Gankra added the A-borrow-checker Area: The borrow checker label Nov 22, 2015
@Manishearth
Copy link
Member

Thanks for the bug report! This is being tracked at
rust-lang/rfcs#811, and probably will get fixed once the MIR work is done.

Previously: #6268, #20403

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker
Projects
None yet
Development

No branches or pull requests

3 participants