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

Incomplete borrow checking with closures #16361

Closed
bfops opened this issue Aug 8, 2014 · 1 comment · Fixed by #16460
Closed

Incomplete borrow checking with closures #16361

bfops opened this issue Aug 8, 2014 · 1 comment · Fixed by #16460
Labels
A-lifetimes Area: lifetime related

Comments

@bfops
Copy link
Contributor

bfops commented Aug 8, 2014

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();
}
@pcwalton
Copy link
Contributor

pcwalton commented Aug 8, 2014

Nominating for 1.0 P-backcompat-lang

pcwalton added a commit to pcwalton/rust that referenced this issue Aug 12, 2014
This fixes borrow checking for closures. Code like this will break:

    struct Foo {
        x: int,
    }

    pub fn main() {
        let mut this = &mut Foo {
            x: 1,
        };
        let r = || {
            let p = &this.x;
            &mut this.x;
        };
        r()
    }

Change this code to not take multiple mutable references to the same value. For
example:

    struct Foo {
        x: int,
    }

    pub fn main() {
        let mut this = &mut Foo {
            x: 1,
        };
        let r = || {
            &mut this.x;
        };
        r()
    }

Closes rust-lang#16361.

[breaking-change]
bors added a commit that referenced this issue Aug 13, 2014
…tsakis

This fixes borrow checking for closures. Code like this will break:

    struct Foo {
        x: int,
    }

    pub fn main() {
        let mut this = &mut Foo {
            x: 1,
        };
        let r = || {
            let p = &this.x;
            &mut this.x;
        };
        r()
    }

Change this code to not take multiple mutable references to the same value. For
example:

    struct Foo {
        x: int,
    }

    pub fn main() {
        let mut this = &mut Foo {
            x: 1,
        };
        let r = || {
            &mut this.x;
        };
        r()
    }

Closes #16361.

[breaking-change]

r? @nikomatsakis
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 21, 2024
Add a stable #visual-studio anchor to the Manual

Helpful for rust-lang/www.rust-lang.org#1915 so we have a persistent place to link as "Visual Studio" from the Rust website.

Syntax reference: https://docs.asciidoctor.org/asciidoc/latest/sections/custom-ids/#assign-auxiliary-ids
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: lifetime related
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants