reference icon indicating copy to clipboard operation
reference copied to clipboard

u64 has an alignment of 8 bytes on i686-pc-windows-msvc instead of 4 as implied in the reference

Open oberien opened this issue 3 years ago • 3 comments

The rust reference states:

Most primitives are generally aligned to their size, although this is platform-specific behavior. In particular, on x86 u64 and f64 are only aligned to 32 bits.

-- https://doc.rust-lang.org/reference/type-layout.html

This is true for i686-unknown-linux-gnu:

> cat foo.rs
fn main() {
    println!("{}", std::mem::align_of::<u64>());
}
> rustc --target i686-unknown-linux-gnu foo.rs && ./foo
4

However, currently on i686-pc-windows-msvc the alignment of u64 is 8 bytes:

> rustc --target i686-pc-windows-msvc foo.rs
> foo.exe
8

The C++ compiler on that platform (at least the one UE uses) aligns u64 inside classes and structs to 4 bytes. I had expected repr(C) in rust do to the same (especially having read the part above from the reference) and was surprised it didn't. As I assume that some code somewhere relies on u64 being aligned to 8 bytes on that target, should the documentation in the reference be changed to reflect this difference?

oberien avatar Jul 02 '22 22:07 oberien

Transferred to rust-lang/reference.

I imagine that could be reworded to something like "For example, i686-unknown-linux-gnu has 32-bit alignment for u64 and f64."

ehuss avatar Jul 03 '22 00:07 ehuss

wait this sounds bigger than a minor documentation issue, are we failing to FFI correctly on i686-pc-windows-msvc

Gankra avatar Jul 03 '22 00:07 Gankra

I believe i686 MSVC does 64-bit alignment for 64-bit values, being distinctly different from how Linux does it (barring gcc's -malign-double flag). However, I think (as you well know), layout and alignment in C is a mess, and more complicated than just saying "it is 64-bit".

ehuss avatar Jul 03 '22 00:07 ehuss