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

value is borrowed too far #13342

Closed
stepancheg opened this issue Apr 5, 2014 · 2 comments
Closed

value is borrowed too far #13342

stepancheg opened this issue Apr 5, 2014 · 2 comments
Labels
A-lifetimes Area: Lifetimes / regions

Comments

@stepancheg
Copy link
Contributor

Code:

struct Bar { value: ~str }

struct TreeMap { dummy: () }

impl TreeMap {
    fn find_mut<'s>(&'s mut self) -> Option<&'s mut Bar> {
        fail!();
    }
}


fn foo<'a>(map: &'a mut TreeMap) -> &'a mut Bar {
    {
        let r = map.find_mut();
        if r.is_some() {
            return r.unwrap();
        }
    } // <-- first borrow could end here

    {
        let s = map.find_mut();
        if s.is_some() {
            return s.unwrap();
        }
    }

    fail!();
} // <-- compiler reports it ends here

fn main() {}

Compiler emits the error:

/home/vagrant/hashmap.rs:21:17: 21:20 error: cannot borrow `*map` as mutable more than once at a time
/home/vagrant/hashmap.rs:21         let s = map.find_mut();
                                            ^~~
/home/vagrant/hashmap.rs:14:17: 14:20 note: previous borrow of `*map` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `*map` until the borrow ends
/home/vagrant/hashmap.rs:14         let r = map.find_mut();
                                            ^~~
/home/vagrant/hashmap.rs:28:2: 28:2 note: previous borrow ends here
/home/vagrant/hashmap.rs:12 fn foo<'a>(map: &'a mut TreeMap) -> &'a mut Bar {
...
/home/vagrant/hashmap.rs:28 }
                            ^
error: aborting due to previous error

Compiler reports that first borrow ends at the end of function, however, variable r is destroyed near 6th line of the function.

@alexcrichton
Copy link
Member

Possible dupe of #9113

cc @nikomatsakis

@nikomatsakis
Copy link
Contributor

dup of #6393

matthiaskrgr pushed a commit to matthiaskrgr/rust that referenced this issue Oct 11, 2022
…ro, r=Veykril

Revert "Add proc-macro dependency to rustc crates"

1. This panics since it indexes into the wrong thing, so fixes rust-lang/rust-analyzer#13340
2. This didn't fix what I thought it would either
Reverts rust-lang/rust-analyzer#13328
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions
Projects
None yet
Development

No branches or pull requests

3 participants