Description
Casting a *const T
to *mut T
may lead to memory corruption since it allows mutation of shared state. Even if the *const T
happened to be unique, it is still undefined behavior and the optimizer may break such code in interesting ways. In a nutshell, this is as bad as transmuting a &
into &mut
. The compiler should warn against doing this.
Update: as pointed out below, there are cases when that does not immediately trigger UB, but in those cases there is no reason to do this in the first place.
This often occurs when people try to consume a data structure and create a new one from it, e.g.
let new_slice = core::slice::from_raw_parts_mut(old_slice.as_ptr() as *mut B, len)
in which case the proper solution is to rewrite it as
let new_slice = core::slice::from_raw_parts_mut(old_slice.as_mut_ptr(), len)
This also may happen when people try to mutate shared state through a &
, in which case they need a Cell
, RefCell
or an UnsafeCell
instead.
Playground with a real-world snippet that fails MIRI: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=b28a15e3d99616b03caafdd794550946
This pattern seems to be quite widespread - quoting @RalfJung on Zulip:
I have seen at least 2 or 3 cases over the last few weeks for a const-to-mut raw ptr cast being the give-away for mutation of shared data
I have already requested a Clippy lint for this, but this looks important enough to warn against by default, without relying on optional tooling to catch this.