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