Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What exactly are the rules for UnsafeCell::get_mut? #333

Closed
CAD97 opened this issue Apr 30, 2022 · 2 comments
Closed

What exactly are the rules for UnsafeCell::get_mut? #333

CAD97 opened this issue Apr 30, 2022 · 2 comments

Comments

@CAD97
Copy link

CAD97 commented Apr 30, 2022

Miri is somewhat inconsistent here:

use std::cell::UnsafeCell;

fn main() {
    let mut cell = UnsafeCell::new(0);
    let ptr = cell.get();
    unsafe { *ptr += 1; }

    // 3 alternatives:

    *cell.get_mut() += 1; // miri doesn't complain
    // *(&mut cell).get_mut() += 1; // miri complains
    // *UnsafeCell::get_mut(&mut cell) += 1; // miri complains

    unsafe { *ptr += 1; }

    dbg!(cell.get_mut());
}

(origininally discovered by @steffahn on urlo)

@RalfJung
Copy link
Member

RalfJung commented Apr 30, 2022

This is another 2-phase borrow weirdness -- cell.get_mut() creates a two-phase borrow which has SharedReadWrite permission and thus joins the equivalence class that ptr is already in. Hence both of these pointers can be used interchangeably.

Whether that is what we want to happen I am not sure. ;)

@RalfJung
Copy link
Member

So, closing as duplicate of #85.

@RalfJung RalfJung closed this as not planned Won't fix, can't repro, duplicate, stale Jun 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants