I am investigating possible reasons that prevent the Rust compiler from optimizing certain code pieces. I found this comment in an issue in rust-lang that alerts me.
We must not optimize away storage of locals that are mutably borrowed, because as @matthewjasper notes in #61430, it isn't decided that the following is UB:
let mut x = String::new(); let p = &mut x as *mut String; let y = x; p.write(String::new());
I thought that the lifetime of x
ends when it is moved to y
. p
is dangling while being .write()
through. But why this is not decided as UB?