Open
Description
Using the following flags
--force-warn clippy::transmute-ptr-to-ptr
this code:
// Stacked Borrows detects that we are casting & to &mut and so it changes why we fail
//@compile-flags: -Zmiri-disable-stacked-borrows
use std::mem::transmute;
#[allow(mutable_transmutes)]
fn main() {
unsafe {
let bs = b"this is a test";
transmute::<&[u8], &mut [u8]>(bs)[4] = 42; //~ ERROR: read-only
}
}
caused the following diagnostics:
Checking _static_memory_modification3 v0.1.0 (/tmp/icemaker_global_tempdir.1WUapHwIh9Kn/icemaker_clippyfix_tempdir.TwLaoJk7SvXJ/_static_memory_modification3)
warning: transmute from a reference to a reference
--> src/main.rs:10:9
|
10 | transmute::<&[u8], &mut [u8]>(bs)[4] = 42; //~ ERROR: read-only
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *(bs as *const [u8] as *mut [u8])`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#transmute_ptr_to_ptr
= note: requested on the command line with `--force-warn clippy::transmute-ptr-to-ptr`
warning: `_static_memory_modification3` (bin "_static_memory_modification3") generated 1 warning
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.12s
However after applying these diagnostics, the resulting code:
// Stacked Borrows detects that we are casting & to &mut and so it changes why we fail
//@compile-flags: -Zmiri-disable-stacked-borrows
use std::mem::transmute;
#[allow(mutable_transmutes)]
fn main() {
unsafe {
let bs = b"this is a test";
&mut *(bs as *const [u8] as *mut [u8]).wrapping_add(4) = 42; //~ ERROR: read-only
}
}
no longer compiled:
Checking _static_memory_modification3 v0.1.0 (/tmp/icemaker_global_tempdir.1WUapHwIh9Kn/icemaker_clippyfix_tempdir.TwLaoJk7SvXJ/_static_memory_modification3)
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> src/main.rs:10:48
|
10 | &mut *(bs as *const [u8] as *mut [u8]).wrapping_add(4) = 42; //~ ERROR: read-only
| ^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `[u8]`
note: required by a bound in `std::ptr::mut_ptr::<impl *mut T>::wrapping_add`
--> /home/matthias/.rustup/toolchains/master/lib/rustlib/src/rust/library/core/src/ptr/mut_ptr.rs:1213:12
|
1211 | pub const fn wrapping_add(self, count: usize) -> Self
| ------------ required by a bound in this associated function
1212 | where
1213 | T: Sized,
| ^^^^^ required by this bound in `std::ptr::mut_ptr::<impl *mut T>::wrapping_add`
error[E0070]: invalid left-hand side of assignment
--> src/main.rs:10:64
|
10 | &mut *(bs as *const [u8] as *mut [u8]).wrapping_add(4) = 42; //~ ERROR: read-only
| ------------------------------------------------------ ^
| |
| cannot assign to this expression
Some errors have detailed explanations: E0070, E0277.
For more information about an error, try `rustc --explain E0070`.
error: could not compile `_static_memory_modification3` (bin "_static_memory_modification3") due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `_static_memory_modification3` (bin "_static_memory_modification3" test) due to 2 previous errors
Version:
rustc 1.89.0-nightly (f315e6145 2025-06-06)
binary: rustc
commit-hash: f315e6145802e091ff9fceab6db627a4b4ec2b86
commit-date: 2025-06-06
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5