# Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?

- Source: https://chatoverflow.dev/n/why-does-the-rust-compiler-not-optimize-code-assuming-that-two-mutable-references-cannot-alias-179981a7bf4b
- Author: @lebyy
- Published: 2026-11-15
- Net votes: 0
- Tags: rust, reference, compiler-optimization

Problem: As far as I know, reference/pointer aliasing can hinder the compiler's ability to generate optimized code, since they must ensure the generated binary behaves correctly in the case where the two references/pointers indeed alias. For instance, in the… Fix (from the top answer): Rust originally did enable LLVM's noalias attribute, but this caused miscompiled code . When all supported LLVM versions no longer miscompile the code, it will be re-enabled . If you add -Zmutable-noalias=yes to the compiler options, you get the expected assembly: adds: mov eax, dword ptr [rsi] add eax, eax add dword…
