Statically Sized, dynamically sized, and other.

submited by
Style Pass
2024-06-06 19:30:03

The symptom of this particular miscompilation is glaringly obvious: any Rust program will crash when attempting to format a string.

When compiling Rust code, the compiler will make some pretty straightforward assumptions about the values of certain types. For example, it will assume that a bool has a value of either 0 or 1, and not something crazy like 134.

To optimize and properly compile this piece of code, the frontend of the compiler will turn it from Rust into a simplified form called MIR.

As you can see, the compiler frontend will tell the backend what to do if the enum animal has a tag 0 (is a dog), tag 1 (is a Fish), or has some other tag.

However, since the compiler frontend knows there may be no other variant, it will tell the backend (the part tuning MIR into the final executable) that it can assume the tag is either 0 or 1.

This information is encoded using the Unreachable Block Terminator. If a block ends with unreachable, the compiler may assume the block itself is unreachable, so it can safely remove this block.

Leave a Comment