Skip to content

value is borrowed too far #13342

Closed
Closed
@stepancheg

Description

@stepancheg

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions