Reordering of writes via differently-typed pointers

As @hanna-kruppe already said, Rust does not do type-based aliasing and (if I have my way) it will never do so. We have unions solely for the purpose of FFI. I see C using unions to “get around” TBAA as more of a hack, and it’s not even free – C has a bunch of really strange rules for accessing unions that are specifically needed to make TBAA work. (Namely, it is only okay to break TBAA if you do it “through the union”; just taking a pointer to a union field and then using that is not enough. This programs most likely has undefined behavior, and it probably does even if you inline read_float.)

So, the part where you first use a u8x16 pointer and then a u8 pointer to access the same memory is perfectly fine. The only thing you need to be worried about is the rules associated with the reference types – here is my latest proposal for these rules, but nothing is decided yet. If you are doing all accesses through raw pointers, there is no problem; with references, you have to take care that during the lifetime of an &mut (from their creation to their last use) no other access is made to the same locations.

2 Likes