2021-03-31 08:09:16 +00:00
|
|
|
// stderr-per-bitwidth
|
2020-04-29 10:00:22 +02:00
|
|
|
// compile-flags: -Zunleash-the-miri-inside-of-you
|
|
|
|
|
|
|
|
use std::cell::UnsafeCell;
|
|
|
|
|
2020-05-04 11:29:16 +02:00
|
|
|
// this test ensures that our mutability story is sound
|
2020-04-29 10:00:22 +02:00
|
|
|
|
|
|
|
struct Meh {
|
|
|
|
x: &'static UnsafeCell<i32>,
|
|
|
|
}
|
|
|
|
unsafe impl Sync for Meh {}
|
|
|
|
|
|
|
|
// the following will never be ok! no interior mut behind consts, because
|
|
|
|
// all allocs interned here will be marked immutable.
|
2020-10-24 20:49:17 +02:00
|
|
|
const MUH: Meh = Meh { //~ ERROR: it is undefined behavior to use this value
|
2020-04-29 10:00:22 +02:00
|
|
|
x: &UnsafeCell::new(42),
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Synced {
|
|
|
|
x: UnsafeCell<i32>,
|
|
|
|
}
|
|
|
|
unsafe impl Sync for Synced {}
|
|
|
|
|
|
|
|
// Make sure we also catch this behind a type-erased `dyn Trait` reference.
|
|
|
|
const SNEAKY: &dyn Sync = &Synced { x: UnsafeCell::new(42) };
|
2020-10-24 20:49:17 +02:00
|
|
|
//~^ ERROR: it is undefined behavior to use this value
|
2020-04-29 10:00:22 +02:00
|
|
|
|
|
|
|
// Make sure we also catch mutable references.
|
|
|
|
const BLUNT: &mut i32 = &mut 42;
|
2020-10-25 10:22:56 +01:00
|
|
|
//~^ ERROR: it is undefined behavior to use this value
|
2020-04-29 10:00:22 +02:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
unsafe {
|
|
|
|
*MUH.x.get() = 99;
|
|
|
|
}
|
|
|
|
}
|