3fdd8d5ef3
The implied deref to statics introduced by HIR->THIR lowering is only used to create place expressions, it lacks unsafe semantics. It is also confusing, as there is no visible `*ident` in the source. For both classes of "unsafe static" (extern static and static mut) allow this operation. We lack a clear story around `thread_local! { static mut }`, which is actually its own category of item that reuses the static syntax but has its own rules. It's possible they should be similarly included, but in the absence of a good reason one way or another, we do not bless it.
37 lines
826 B
Rust
37 lines
826 B
Rust
//@run-pass
|
|
#![feature(const_mut_refs)]
|
|
#![feature(sync_unsafe_cell)]
|
|
|
|
use std::cell::SyncUnsafeCell;
|
|
use std::ptr;
|
|
|
|
#[repr(C)]
|
|
struct SyncPtr {
|
|
foo: *mut u32,
|
|
}
|
|
unsafe impl Sync for SyncPtr {}
|
|
|
|
static mut STATIC: u32 = 42;
|
|
|
|
static INTERIOR_MUTABLE_STATIC: SyncUnsafeCell<u32> = SyncUnsafeCell::new(42);
|
|
|
|
// A static that mutably points to STATIC.
|
|
static PTR: SyncPtr = SyncPtr { foo: ptr::addr_of_mut!(STATIC) };
|
|
static INTERIOR_MUTABLE_PTR: SyncPtr =
|
|
SyncPtr { foo: ptr::addr_of!(INTERIOR_MUTABLE_STATIC) as *mut u32 };
|
|
|
|
fn main() {
|
|
let ptr = PTR.foo;
|
|
unsafe {
|
|
assert_eq!(*ptr, 42);
|
|
*ptr = 0;
|
|
assert_eq!(*PTR.foo, 0);
|
|
}
|
|
|
|
let ptr = INTERIOR_MUTABLE_PTR.foo;
|
|
unsafe {
|
|
assert_eq!(*ptr, 42);
|
|
*ptr = 0;
|
|
assert_eq!(*INTERIOR_MUTABLE_PTR.foo, 0);
|
|
}
|
|
}
|