2020-12-27 11:33:56 -06:00
|
|
|
#![feature(const_refs_to_cell)]
|
|
|
|
|
2018-10-25 13:53:05 -05:00
|
|
|
use std::cell::*;
|
|
|
|
|
2020-12-30 10:48:07 -06:00
|
|
|
// not ok, because this creates a dangling pointer, just like `let x = Cell::new(42).as_ptr()` would
|
2018-10-25 13:53:05 -05:00
|
|
|
static FOO: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr());
|
2020-12-27 11:33:56 -06:00
|
|
|
//~^ ERROR encountered dangling pointer
|
|
|
|
const FOO_CONST: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr());
|
|
|
|
//~^ ERROR encountered dangling pointer
|
2018-10-25 13:53:05 -05:00
|
|
|
|
2020-12-30 10:48:07 -06:00
|
|
|
// Ok, these are just base values and it is the `Wrap` author's job to uphold `Send` and `Sync`
|
|
|
|
// invariants, since they used `unsafe impl`.
|
2018-10-25 13:53:05 -05:00
|
|
|
static FOO3: Wrap<Cell<u32>> = Wrap(Cell::new(42));
|
2020-12-27 11:33:56 -06:00
|
|
|
const FOO3_CONST: Wrap<Cell<u32>> = Wrap(Cell::new(42));
|
|
|
|
|
2020-12-30 10:48:07 -06:00
|
|
|
// ok, we are referring to the memory of another static item.
|
2018-10-25 13:53:05 -05:00
|
|
|
static FOO4: Wrap<*mut u32> = Wrap(FOO3.0.as_ptr());
|
2020-12-30 10:48:07 -06:00
|
|
|
|
|
|
|
// not ok, the use of a constant here is equivalent to an inline declaration of the value, so
|
|
|
|
// its memory will get freed before the constant is finished evaluating, thus creating a dangling
|
|
|
|
// pointer. This would happen exactly the same at runtime.
|
2020-12-27 11:33:56 -06:00
|
|
|
const FOO4_CONST: Wrap<*mut u32> = Wrap(FOO3_CONST.0.as_ptr());
|
|
|
|
//~^ ERROR encountered dangling pointer
|
2018-10-25 13:53:05 -05:00
|
|
|
|
2020-12-30 10:48:07 -06:00
|
|
|
// not ok, because the `as_ptr` call takes a reference to a temporary that will get freed
|
|
|
|
// before the constant is finished evaluating.
|
2018-10-25 13:53:05 -05:00
|
|
|
const FOO2: *mut u32 = Cell::new(42).as_ptr();
|
2020-12-27 11:33:56 -06:00
|
|
|
//~^ ERROR encountered dangling pointer
|
2018-10-25 13:53:05 -05:00
|
|
|
|
|
|
|
struct IMSafeTrustMe(UnsafeCell<u32>);
|
|
|
|
unsafe impl Send for IMSafeTrustMe {}
|
|
|
|
unsafe impl Sync for IMSafeTrustMe {}
|
|
|
|
|
|
|
|
static BAR: IMSafeTrustMe = IMSafeTrustMe(UnsafeCell::new(5));
|
|
|
|
|
|
|
|
|
2020-12-27 11:33:56 -06:00
|
|
|
|
2018-10-25 13:53:05 -05:00
|
|
|
struct Wrap<T>(T);
|
|
|
|
unsafe impl<T> Send for Wrap<T> {}
|
|
|
|
unsafe impl<T> Sync for Wrap<T> {}
|
|
|
|
|
|
|
|
static BAR_PTR: Wrap<*mut u32> = Wrap(BAR.0.get());
|
|
|
|
|
2020-12-27 19:09:55 -06:00
|
|
|
const fn fst_ref<T, U>(x: &(T, U)) -> &T { &x.0 }
|
|
|
|
|
2018-10-25 15:10:41 -05:00
|
|
|
fn main() {}
|