-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Potential generalization of destructing struct destructuring #11855
Comments
cc me |
(Though, this probably should be written up as an RFC. Or perhaps first work out the kinks on http://discuss.rust-lang.org, which is how I found this in the first place...) |
Probably, but probably not now. It'd only be postponed anyways. |
This should probably be moved to the RFCs repo? |
Your wish is my command :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Currently structs with destructors are (sensibly) not allowed to be move-destructured (#3147), because then when their destructor runs, it would access deinitialized values.
This could potentially be generalized if we recognize that a destructor, in other words drop glue, is not an indivisible thing. It consists of the
Drop
impl for the struct itself, and the destructors for each of its fields. Therefore if we write:when the destructuring happens we could run the
Drop
impl forS
, but not the drop glue forA
andB
. Those are then moved out, and their destructors will run later, whenever they go out of scope.I believe this is sound:
Drop::drop()
takes&mut self
, so it can mutate the components ofS
but not deinitialize them. If we broaden our considerations tounsafe
code, then if the fields of a structS
have destructors themselves, then theDrop
impl forS
must, even today, leave them in a valid state, because those destructors will then access it. It is only cases where the fields ofS
do not have their own destructors, and theDrop
impl forS
usesunsafe
code to put them in an invalid state, which would become dangerous, and we would have to be very careful about.We'd obviously have to think about whether we actually want this (I haven't thought of any use cases yet), but as a theoretical possibility, I think it checks out.
The text was updated successfully, but these errors were encountered: