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

Closure spec causes the variable to be incorrectly captured #962

Open
arnaudgolfouse opened this issue Mar 4, 2024 · 0 comments
Open

Closure spec causes the variable to be incorrectly captured #962

arnaudgolfouse opened this issue Mar 4, 2024 · 0 comments

Comments

@arnaudgolfouse
Copy link
Collaborator

The following code:

#[requires(p.0@ == 1)]
#[ensures(result@ == 2)]
fn disjoint_captures(mut p: (i32, i32)) -> i32 {
    let p0 = &p.0;
    let mut clos = #[ensures(p.1@ == 1)] || {
        p.1 = 1;
    };
    clos();
    *p0 + p.1
}

When using edition 2021, should at least compile (it does without the specifications), but the closure's spec causes a borrow error:

error[E0502]: cannot borrow `p` as mutable because it is also borrowed as immutable
  --> /.../test.rs:5:42
   |
4  |     let p0 = &p.0;
   |              ---- immutable borrow occurs here
5  |     let mut clos = #[ensures(p.1@ == 1)] || {
   |                              ---         ^^ mutable borrow occurs here
   |                              |
   |                              second borrow occurs due to use of `p` in closure
6  |         p.1 = 1;
   |         --- capture is mutable because of use here
...
10 |     *p0 + p.1
   |     --- immutable borrow later used here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant