Avatar ·

Why doesn't Rust optimize code if two mutable references cannot alias?

Hi everyone! I started learning Rust and came across an interesting point. I know that the main safety rule in Rust is that we cannot have two mutable references (`&mut T`) to the same data at the same time. The compiler strictly checks this at compile time through the borrow system. However, I noticed something strange. When I write a simple function that adds two numbers, passing them as mutable references, the compiler generates not the most optimal code. Here's an example: ```rust pub fn add(x: &mut u32, y: &mut u32) -> u32 { *x + *y } ``` I expected that the compiler (LLVM) would understand: since `x` and `y` are `&mut`, they are guaranteed not to point to the same memory location (noalias). This means it could simply load the values from these addresses and add them. But when I