Rollup merge of #56012 - RalfJung:unsafe-cell, r=nikomatsakis

avoid shared ref in UnsafeCell::get

Avoid taking a shared reference in `UnsafeCell::get`. This *should* be taking a raw reference (see https://github.com/rust-lang/rfcs/pull/2582), but that operation is not currently available, so I propose we exploit `repr(transparent)` instead and cast the pointer around.

This is required to make `UnsafeCell::get` pass the [stacked borrows implementation](https://www.ralfj.de/blog/2018/11/16/stacked-borrows-implementation.html) in miri (currently, `UnsafeCell::get` is on a whitelist, but that is of course not very satisfying). It shouldn't affect normal execution/codegen. Would be great if we could get this landed and shrink miri's whitelist!

Cc @nikomatsakis
This commit is contained in:
Pietro Albini 2018-11-18 23:24:57 +01:00 committed by kennytm
commit 2a68c0075a
No known key found for this signature in database
GPG Key ID: FEF6C8051D0E013C

View File

@ -1509,7 +1509,9 @@ impl<T: ?Sized> UnsafeCell<T> {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub const fn get(&self) -> *mut T {
&self.value as *const T as *mut T
// We can just cast the pointer from `UnsafeCell<T>` to `T` because of
// #[repr(transparent)]
self as *const UnsafeCell<T> as *const T as *mut T
}
}