Tracking issue for RFC 2514, "Union initialization and Drop" #55149
Description
This is a tracking issue for the RFC "Union initialization and Drop" (rust-lang/rfcs#2514).
Successor of #32836.
Steps:
- Implement the RFC (Mentoring instructions: Tracking issue for RFC 2514, "Union initialization and Drop" #55149 (comment)): Change untagged_unions to not allow union fields with drop #62330
- Adjust documentation (see instructions on forge)
- Stabilization PR (see instructions on forge)
Unresolved questions:
-
There should be more tests in particular for all move-related behaviors (1, 2, 3, 4). Done in unions: test move behavior of non-Copy fields #75559.
-
Should we try to avoid the
DerefMut
-related pitfall? And if yes, should we maybe try harder, e.g. lint against using*
below a union type when describing a place? That would make people writelet v = &mut u.f; *v = Vec::new();
. It is not clear that this helps in terms of pointing out that an automatic drop may be happening. (Implementation at do not apply DerefMut on union field #75584.) -
We could allow moving out of a union field even if the union implements
Drop
. That would have the effect of making the union considered uninitialized, i.e., it would not be dropped implicitly when it goes out of scope. However, it might be useful to not let people do this accidentally. The same effect can always be achieved by having a dropless union wrapped in a newtypestruct
with the desiredDrop
. -
Should we allow
impl Copy for Union
even when the union has non-Copy
fields? (Proposed here.)
Implementation history
- Change untagged_unions to not allow union fields with drop #62330: union fields must be no-drop
- unions: test move behavior of non-Copy fields #75559: more tests for moving out of non-Copy union fields
- do not apply DerefMut on union field #75584: don't implicitly
DerefMut
in unions - stabilize union with 'ManuallyDrop' fields and 'impl Drop for Union' #77547: stabilize
ManuallyDrop<_>
union fields