reference icon indicating copy to clipboard operation
reference copied to clipboard

Confusion about whether union writes ever have to be placed in `unsafe` blocks

Open jruderman opened this issue 4 years ago • 1 comments

https://doc.rust-lang.org/reference/items/unions.html says:

Writes to Copy or ManuallyDrop union fields do not require reads for running destructors, so these writes don't have to be placed in unsafe blocks

but later also says:

When a union is dropped, it cannot know which of its fields needs to be dropped. For this reason, all union fields must either be of a Copy type or of the shape ManuallyDrop<_>.

If the latter is true, then the former seems to be written in an overly-qualified way.

jruderman avatar Sep 10 '21 02:09 jruderman

Yea, I can see how that seems a little awkward. I think this is a result of the evolution of unions, where the original RFC allowed drop types, and the way it was phrased then was a little different:

Writes to union fields may generally require reads (for running destructors), so these writes have to be placed in unsafe blocks too.

That chapter definitely could use a little cleanup to more clearly state up-front the restrictions on the fields. Regarding the "unsafe" aspect, that could be reworded to be more of an explanation of why writes are safe, to contrast with reads.

ehuss avatar Sep 10 '21 03:09 ehuss