Skip to content

Incomplete borrow checking with closures #16361

Closed
@bfops

Description

@bfops

The following code allows me to create two mutable references to a given variable without failure; this is used to non-obviously invalidate a value. Removing the closure from the body of bar causes compile-time failure.

rustc 0.12.0-pre-nightly (8fe73f1 2014-08-06 23:41:05 +0000)

struct Foo {
    x: Vec<int>,
}

impl Foo {
    pub fn foo(&mut self, x: &mut Vec<int>) {
        let l = x.len() - 1;
        *x.get_mut(l) = 9999;
        x.remove(0);
    }

    pub fn bar(&mut self) {
        let r = || {
            let mut i = 0u;
            for x in self.x.iter() {
                println!("x[{}] {}", i, x);
                self.foo(&mut self.x);
                println!("x[{}] {}", i, x);
                i = i + 1;
            }
        };
        r()
    }
}

pub fn main() {
    Foo { x: Vec::from_slice([1,2,3]) }.bar();
}

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