]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/miri_unleashed/mutable_references_err.rs
e9f6f9140e778af0b7d582514d644846fb8e487c
[rust.git] / src / test / ui / consts / miri_unleashed / mutable_references_err.rs
1 // compile-flags: -Zunleash-the-miri-inside-of-you
2
3 #![allow(const_err)]
4
5 use std::cell::UnsafeCell;
6
7 // this test ensures that our mutability story is sound
8
9 struct Meh {
10     x: &'static UnsafeCell<i32>,
11 }
12 unsafe impl Sync for Meh {}
13
14 // the following will never be ok! no interior mut behind consts, because
15 // all allocs interned here will be marked immutable.
16 const MUH: Meh = Meh { //~ ERROR: it is undefined behavior to use this value
17     x: &UnsafeCell::new(42),
18 };
19
20 struct Synced {
21     x: UnsafeCell<i32>,
22 }
23 unsafe impl Sync for Synced {}
24
25 // Make sure we also catch this behind a type-erased `dyn Trait` reference.
26 const SNEAKY: &dyn Sync = &Synced { x: UnsafeCell::new(42) };
27 //~^ ERROR: it is undefined behavior to use this value
28
29 // Make sure we also catch mutable references.
30 const BLUNT: &mut i32 = &mut 42;
31 //~^ ERROR: mutable memory (`&mut`) is not allowed in constant
32
33 fn main() {
34     unsafe {
35         *MUH.x.get() = 99;
36     }
37 }