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

Can't mutably borrow out of box pattern twice #31667

Closed
jethrogb opened this issue Feb 15, 2016 · 2 comments
Closed

Can't mutably borrow out of box pattern twice #31667

jethrogb opened this issue Feb 15, 2016 · 2 comments
Labels
A-borrow-checker Area: The borrow checker

Comments

@jethrogb
Copy link
Contributor

See the following code

#![feature(box_patterns)]

struct A {
    b: u8,
    c: u8,
}

struct S<T>(T);

fn foo_s(a: &mut Option<S<A>>) {
    match a {
        &mut Some(S(A{ref mut b,ref mut c})) => println!("{}{}",b,c),
        &mut None => println!("none")
    }
}

fn foo_box(a: &mut Option<Box<A>>) {
    match a {
        &mut Some(box A{ref mut b,ref mut c}) => println!("{}{}",b,c),
        &mut None => println!("none")
    }
}

foo_s compiles fine, but foo_box generates an error:

<anon>:19:35: 19:44 error: cannot borrow `a.0` (here through borrowing `a.0.c`) as mutable more than once at a time [E0499]
<anon>:19         &mut Some(box A{ref mut b,ref mut c}) => println!("{}{}",b,c),
                                            ^~~~~~~~~
<anon>:19:35: 19:44 help: see the detailed explanation for E0499
<anon>:19:25: 19:34 note: previous borrow of `a.0` occurs here (through borrowing `a.0.b`); the mutable borrow prevents subsequent moves, borrows, or modification of `a.0` until the borrow ends
<anon>:19         &mut Some(box A{ref mut b,ref mut c}) => println!("{}{}",b,c),
                                  ^~~~~~~~~
<anon>:21:6: 21:6 note: previous borrow ends here
<anon>:18     match a {
<anon>:19         &mut Some(box A{ref mut b,ref mut c}) => println!("{}{}",b,c),
<anon>:20         &mut None => println!("none")
<anon>:21     }
              ^
@nagisa nagisa added the A-borrow-checker Area: The borrow checker label Feb 16, 2016
@apasel422
Copy link
Contributor

This looks like a duplicate of #30104.

@alexcrichton
Copy link
Member

Indeed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker
Projects
None yet
Development

No branches or pull requests

4 participants