about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPietro Albini <pietro@pietroalbini.org>2018-11-18 23:24:57 +0100
committerkennytm <kennytm@gmail.com>2018-11-19 22:06:37 +0800
commit2a68c0075a1cf769ebc6f28667cd29fd11b852c1 (patch)
tree5382ed6816b837c335dd00318809ad28725ca15d
parent05ae505a4c438b80268680747713c84ebad662f3 (diff)
parent25d46f309130be1671d7e44d48e67b23f510bcdc (diff)
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
-rw-r--r--src/libcore/cell.rs4
1 files changed, 3 insertions, 1 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs
index 9cf42eff219..d8d51f53377 100644
--- a/src/libcore/cell.rs
+++ b/src/libcore/cell.rs
@@ -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
     }
 }