rust-lang / unsafe-code-guidelines

Forum for discussion about what unsafe code can and can't do

Home Page:https://rust-lang.github.io/unsafe-code-guidelines

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it sound to split an `UnsafeCell` into multiple `UnsafeCell`s?

joshlf opened this issue · comments

Miri doesn't complain about the following code, but is this guaranteed to be sound?

fn main() {
    let x = UnsafeCell::new([0u8; 2]);
    let x = &x;
    let x_ptr: *const UnsafeCell<[u8; 2]> = x;
    let y_ptr = x_ptr as *const [UnsafeCell<u8>; 2];
    let _y = unsafe { &*y_ptr };
}

I read #451 (comment) as implying this is intended to be sound, but I wanted to double-check.

Also I'd say this is a duplicate of #451; projection to struct fields and array "fields" should behave the same.

Sounds good, that's what I figured. Thanks!