rust-lang / reference

The Rust Reference
https://doc.rust-lang.org/nightly/reference/
Apache License 2.0
1.23k stars 479 forks source link

Document union alignment guarantee? #871

Open tesuji opened 4 years ago

tesuji commented 4 years ago

Currently https://github.com/rust-lang/reference/blob/master/src/items/unions.md only document size guarantee of union. Can Rust guarantee that alignment of union somehow? Like it is the same as biggest alignment of all fields?

ehuss commented 4 years ago

AFAIK, the Rust representation is not defined. The alignment of C unions is documented here: https://doc.rust-lang.org/nightly/reference/type-layout.html#reprc-unions

tesuji commented 4 years ago

Ah, thank you. It would be nice to write this directly. I was <ctrl-F> with align keyword in the page and found nothing.

ehuss commented 4 years ago

Yea, it would probably be OK to call that out since unions are (I think) usually used with repr(C) and all the examples use that. My reluctance is that every type is affected by layout attributes, and I wouldn't want to link to that page from every type.

tesuji commented 4 years ago

Not much primitive types and composed types like struct, union and enum. Union is special, because it's mostly used in FFI with C.