-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Don't recommend ManuallyDrop to customize drop order #76150
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
r? @dtolnay |
I agree with this change to the guideline. I don't know if these docs date to before we had agreed to define the drop order or not. I would like the documentation to briefly explain the defined drop order of struct fields, though, and maybe the code sample using that to get a certain drop order. I know its contained in the associated link, but it would make the documentation clearer here. |
See https://internals.rust-lang.org/t/need-for-controlling-drop-order-of-fields/12914/21 for the discussion. TL;DR: ManuallyDrop is unsafe and footguny, but you can just ask the compiler to do all the work for you by re-ordering declarations.
Yeah, that would be helpful, included an example! |
r=me unless you want another review. |
@bors r+ looks good! |
📌 Commit 60b102d has been approved by |
@bors rollup |
Don't recommend ManuallyDrop to customize drop order See https://internals.rust-lang.org/t/need-for-controlling-drop-order-of-fields/12914/21 for the discussion. TL;DR: ManuallyDrop is unsafe and footguny, but you can just ask the compiler to do all the work for you by re-ordering declarations. Specifically, the original example from the docs is much better written as ```rust struct Peach; struct Banana; struct Melon; struct FruitBox { melon: Melon, // XXX: mind the relative drop order of the fields below peach: Peach, banana: Banana, } ```
Don't recommend ManuallyDrop to customize drop order See https://internals.rust-lang.org/t/need-for-controlling-drop-order-of-fields/12914/21 for the discussion. TL;DR: ManuallyDrop is unsafe and footguny, but you can just ask the compiler to do all the work for you by re-ordering declarations. Specifically, the original example from the docs is much better written as ```rust struct Peach; struct Banana; struct Melon; struct FruitBox { melon: Melon, // XXX: mind the relative drop order of the fields below peach: Peach, banana: Banana, } ```
…atic-morse Rollup of 13 pull requests Successful merges: - rust-lang#72734 (Reduce duplicate in liballoc reserve error handling) - rust-lang#76131 (Don't use `zip` to compare iterators during pretty-print hack) - rust-lang#76150 (Don't recommend ManuallyDrop to customize drop order) - rust-lang#76275 (Implementation of Write for some immutable ref structs) - rust-lang#76489 (Add explanation for E0756) - rust-lang#76581 (do not ICE on bound variables, return `TooGeneric` instead) - rust-lang#76655 (Make some methods of `Pin` unstable const) - rust-lang#76783 (Only get ImplKind::Impl once) - rust-lang#76807 (Use const-checking to forbid use of unstable features in const-stable functions) - rust-lang#76888 (use if let instead of single match arm expressions) - rust-lang#76914 (extend `Ty` and `TyCtxt` lints to self types) - rust-lang#77022 (Reduce boilerplate for BytePos and CharPos) - rust-lang#77032 (lint missing docs for extern items) Failed merges: r? `@ghost`
Reminder: Rustonomicon (still?) advises exactly the other way around 🙂 |
See
https://internals.rust-lang.org/t/need-for-controlling-drop-order-of-fields/12914/21
for the discussion.
TL;DR: ManuallyDrop is unsafe and footguny, but you can just ask the compiler to do all the work for you by re-ordering declarations.
Specifically, the original example from the docs is much better written as