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

Clarify UnsafeCell docs; fix #34496 #34520

Merged
merged 1 commit into from Aug 4, 2016

Conversation

Manishearth
Copy link
Member

No description provided.

@Manishearth
Copy link
Member Author

r? @gankro or @steveklabnik

/// that `&mut T` is unique. When building abstractions like `Cell`, `RefCell`, `Mutex`, etc,
/// you need to turn these optimizations off. `UnsafeCell` is the way to do this,
/// when `UnsafeCell<T>` is immutably aliased, it is still safe to obtain a mutable
/// reference to its interior and/or to mutate it. However, it us up to the abstraction designer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/us/is

@Manishearth
Copy link
Member Author

updated

@diwic
Copy link
Contributor

diwic commented Jun 29, 2016

👍

@aidanhs
Copy link
Member

aidanhs commented Jul 8, 2016

While these docs are being improved, could you mention that an UnsafeCell itself should never be mutable or you break its key functionality - #16022

@Manishearth
Copy link
Member Author

could you mention that an UnsafeCell itself should never be mutable or you break its key functionality

I'm not quite sure what this means. It is possible to, for example, nest RefCells; e.g. RefCell<Vec<RefCell>>, giving you an &mut to a RefCell.

@Manishearth
Copy link
Member Author

I don't see that issue being a problem, really. &mut is unique, even if there is an unsafecell behind it. UnsafeCell is not meant to be used to make &mut shareable, it is to be used to make & mutable. There is no key functionality being broken, that's &mut working as intended.

@aidanhs
Copy link
Member

aidanhs commented Jul 8, 2016

&mut is unique, even if there is an unsafecell behind it

Yes, but this may initially not be expected given the documentation has just talked about how UnsafeCell is special and lets you use mutably aliased data - reinforcing that it is not special when the UnsafeCell itself is mutably aliased doesn't hurt.

Concrete example:

fn main() {
    process::exit(foo() as i32);
}
fn foo() -> u8 {
    let mut x = UnsafeCell::new(1);
    let px = x.get();
    bar(&mut x, px);
    unsafe { x.into_inner() }
}
fn bar(v1: &mut UnsafeCell<u8>, v2: *mut u8) {
    unsafe {
        let a = *v1.get();
        *v2 = 3;
        *v1.get() = a;
    }
}

1.7.0 (the last version before &mut alias annotations were removed as an LLVM bug workaround) compiles this to return 3 (validly). My argument is that this UB caused by the two &mut isn't obvious to a first-time UnsafeCell user without a reinforcement that you should basically never have a &mut UnsafeCell because it 'overrides' the whole aliased mutable data thing (because, as you say, it's obeying the standard rules of &mut).

@aidanhs
Copy link
Member

aidanhs commented Jul 8, 2016

If you're not convinced then don't worry about it and I'll make a separate PR at some point.

@Manishearth Manishearth force-pushed the fix-unsafecell-docs branch 2 times, most recently from efac72b to 1fe8f9c Compare July 11, 2016 06:12
@Manishearth
Copy link
Member Author

Fixed

@steveklabnik steveklabnik self-assigned this Jul 11, 2016
/// from the cell. This is often done via runtime checks.
///
/// Note that while mutating or mutably aliasing the contents of an `& UnsafeCell<T>` is
/// okay (provided you enforce the invariants some othe way); it is still undefined behavior
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: should be "other"

@steveklabnik
Copy link
Member

@Manishearth just a few nits, and this PR is good to go

@Manishearth
Copy link
Member Author

@bors r=steveklabnik

fixed nits

@bors
Copy link
Contributor

bors commented Jul 26, 2016

📌 Commit 3873402 has been approved by steveklabnik

@bors
Copy link
Contributor

bors commented Jul 26, 2016

⌛ Testing commit 3873402 with merge 1d588dc...

@bors
Copy link
Contributor

bors commented Jul 26, 2016

💔 Test failed - auto-win-msvc-64-opt-rustbuild

@Manishearth
Copy link
Member Author

@bors retry

@bors
Copy link
Contributor

bors commented Jul 26, 2016

⌛ Testing commit 3873402 with merge ccd5daf...

@bors
Copy link
Contributor

bors commented Jul 26, 2016

💔 Test failed - auto-win-msvc-64-opt

@steveklabnik
Copy link
Member

@bors: retry

@bors
Copy link
Contributor

bors commented Aug 3, 2016

⌛ Testing commit 3873402 with merge 007f721...

bors added a commit that referenced this pull request Aug 3, 2016
@bors
Copy link
Contributor

bors commented Aug 3, 2016

⛄ The build was interrupted to prioritize another pull request.

@bors
Copy link
Contributor

bors commented Aug 4, 2016

⌛ Testing commit 3873402 with merge 40f3ee2...

bors added a commit that referenced this pull request Aug 4, 2016
@bors bors merged commit 3873402 into rust-lang:master Aug 4, 2016
@Manishearth Manishearth deleted the fix-unsafecell-docs branch December 2, 2016 19:02
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

Successfully merging this pull request may close these issues.

None yet

7 participants