-
Notifications
You must be signed in to change notification settings - Fork 501
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
Document field struct alignment guarantees? #1151
Comments
I'd propose a formulation, but I'm unsure about e.g. stuff like ZST... I'd add:
What do you think? |
I don't know the precise wording the reference likes, but yes repr(rust) must align all of its fields. Zero-sized-types must also be completely aligned. Most have align 1 so they are trivially always aligned. The major exception is There is otherwise no burden for keeping ZSTs aligned in repr(rust) because the compiler is always free to logically reorder them to the start of the struct which has maximum alignment. Because they have no size, they do not "waste" that alignment and the non-ZST fields can still start at offset 0. |
Ah, that is interpreting "no guarantees" way too far. But I agree it is probably worth being more explicit here. Some things are definitely guaranteed:
These guarantees are required for basic soundness, they are so fundamental that presumably the author of the "no guarantee" line could not imagine a violation even being considered. ;) |
As the author of that line, it's as @RalfJung says, except that I did know those two requirements at the time; I just failed to convey them anywhere at all. Tuple representation has the same problems. |
This recently posted article makes that point that, because the default representation makes no guarantees of data layout, one must use
write_unaligned
to write to the fields of a struct when initializing it field by field.This runs contrary to the the documentation of
MaybeUninit
about initializing a struct field by field, and also to what the nomicon has to say on the subject:Should we amend the reference to either link to the nomicon for more details, or to specify that fields that can be accessed in a struct are properly aligned (no
write_unaligned
necessary)?The text was updated successfully, but these errors were encountered: