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

Cannot move out of boxed value during deconstruction #34859

Closed
jonhoo opened this issue Jul 16, 2016 · 2 comments
Closed

Cannot move out of boxed value during deconstruction #34859

jonhoo opened this issue Jul 16, 2016 · 2 comments

Comments

@jonhoo
Copy link
Contributor

jonhoo commented Jul 16, 2016

On stable, the following code does not compile:

enum X {
    A(String, Box<X>),
}

fn foo(x: Box<X>) {
    let X::A(a, b) = *x;
}

The compiler says that extracting b is a "use of moved value: x", and that x was moved when extracting a. The issue also occurs if using match, or when using the box_patterns feature. However, this clearly should be legal to do.

The following code also breaks in a similar way, but with a different error message ("use of collaterally moved value: (x:X::A).1"; which is not even mentioned by --explain E0382):

enum X {
    A(String, Box<X>),
    C,
}

fn foo(x: Box<X>) {
    if let X::A(a, b) = *x {
    }
}

It was pointed out on IRC that this can be worked around by writing {*x} instead of *x, but that's not particularly satisfactory. Someone then said that this was likely a bug with the deref-move special case for Box, where it doesn't handle partial moves correctly.

@jonhoo
Copy link
Contributor Author

jonhoo commented Jul 16, 2016

Should also mention that the code works correctly is s/String/i32/, presumably because i32 is Copy, and so only b is really moved.

@apasel422
Copy link
Contributor

Thanks for the report! Closing as duplicate of #30564 (and a few other closed issues, e.g. #33550).

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

2 participants