Coercing &mut to *const should not create a shared reference · Issue #56604 · rust-lang/rust (original) (raw)
It has long been a rule in Rust that you must not mutate through a shared reference, or a raw pointer obtained from a shared reference.
Unfortunately, that rule currently forbids the following code:
fn direct_mut_to_const_raw() { let x = &mut 0; let y: *const i32 = x; unsafe { *(y as *mut i32) = 1; } assert_eq!(*x, 1); }
The reason for this is that coercing &mut T
to *const T
implicitly first creates a shared reference and then coerces that to *const T
, meaning y
in the example above is technically a raw pointer obtained from a shared reference.
We should fix our coercion logic to no longer create this intermediate shared reference.
See #56161 for how we uncovered this problem.