Skip to content

Commit

Permalink
Rollup merge of rust-lang#56012 - RalfJung:unsafe-cell, r=nikomatsakis
Browse files Browse the repository at this point in the history
avoid shared ref in UnsafeCell::get

Avoid taking a shared reference in `UnsafeCell::get`. This *should* be taking a raw reference (see rust-lang/rfcs#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
  • Loading branch information
kennytm authored Nov 17, 2018
2 parents 577c360 + 25d46f3 commit a818165
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit a818165

Please sign in to comment.