Zig Compiler's Behavior in Regards to "Strict Aliasing"

Aliasing in general is legal in Zig, however, some types have well-defined memory layout while others do not. For example, extern struct has well-defined memory layout, but struct does not.

Therefore, if you alias two things which have well-defined memory layout, the code is well-defined.

However, it is not well-defined to load memory through a differently typed alias when the type of memory in question has a non-well-defined memory layout, or the alias has a non-well-defined memory layout.

Where, exactly, to put the “illegal behavior” stamp is not decided yet, but probably it will be at the @ptrCast because that is where it is cheapest to put a safety check, and there is a plan for @ptrCast safety.

6 Likes