A ZST can't hold state, and its address is basically meaningless, with everything in a Vec being at the same address, for example. So what does it mean for &mut
to alias with ZSTs? Is it meaningful? Detectable?
Or, concretely, the bar
function below would clearly be invalid for anything with size>0, but with a ZST it seems indistinguishable from foo
, which is clearly safe.
fn foo(x: &mut [()]) -> (&mut (), &mut (), &mut [()]) {
let (a, x) = x.split_first_mut().unwrap();
let (b, x) = x.split_first_mut().unwrap();
assert!(std::ptr::eq(a, b));
(a, b, x)
}
fn bar(x: &mut [()]) -> (&mut (), &mut (), &mut [()]) {
let (a, x) = x.split_first_mut().unwrap();
let b = unsafe{ &mut *(a as *mut _) };
assert!(std::ptr::eq(a, b));
(a, b, &mut x[1..])
}
fn main() {
println!("{:?}", foo(&mut [();4]));
println!("{:?}", bar(&mut [();4]));
}