Differing match results between opt levels when casting to #[repr(C)] enum

⚓ rust    📅 2025-06-04    👤 surdeus    👁️ 2      

surdeus

I've been doing some serialization work and recently encountered a really unexpected situation where depending on the compiler opt-level, a match statement can produce differing results. Does anyone have any insight on why this is? This almost seems like a compiler bug, since O0 and O1 shouldn't be following different logical branches with the same data, but it could easily be UB nose-demons manifesting due to the use of unsafe type casting.

To demonstrate, try the code below with opt-level=1 and opt-level=0. With no optimizations, the discriminant is read as none, however with opt-level 1 or higher, it's detected as some.

5 posts - 4 participants

Read full topic

🏷️ rust_feed