Skip to content

Commit aad1d3b

Browse files
authored
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 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
2 parents db982d7 + 25d46f3 commit aad1d3b

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/libcore/cell.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,9 @@ impl<T: ?Sized> UnsafeCell<T> {
15091509
#[inline]
15101510
#[stable(feature = "rust1", since = "1.0.0")]
15111511
pub const fn get(&self) -> *mut T {
1512-
&self.value as *const T as *mut T
1512+
// We can just cast the pointer from `UnsafeCell<T>` to `T` because of
1513+
// #[repr(transparent)]
1514+
self as *const UnsafeCell<T> as *const T as *mut T
15131515
}
15141516
}
15151517

0 commit comments

Comments
 (0)