]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/miri_unleashed/mutable_references.rs
Rollup merge of #98640 - cuviper:stable-rust-analyzer, r=Mark-Simulacrum
[rust.git] / src / test / ui / consts / miri_unleashed / mutable_references.rs
1 // compile-flags: -Zunleash-the-miri-inside-of-you
2 #![allow(const_err)]
3
4 use std::cell::UnsafeCell;
5
6 // a test demonstrating what things we could allow with a smarter const qualification
7
8 // this is fine because is not possible to mutate through an immutable reference.
9 static FOO: &&mut u32 = &&mut 42;
10
11 // this is fine because accessing an immutable static `BAR` is equivalent to accessing `*&BAR`
12 // which puts the mutable reference behind an immutable one.
13 static BAR: &mut () = &mut ();
14
15 struct Foo<T>(T);
16
17 // this is fine for the same reason as `BAR`.
18 static BOO: &mut Foo<()> = &mut Foo(());
19
20 // interior mutability is fine
21 struct Meh {
22     x: &'static UnsafeCell<i32>,
23 }
24 unsafe impl Sync for Meh {}
25 static MEH: Meh = Meh {
26     x: &UnsafeCell::new(42),
27 };
28
29 // this is fine for the same reason as `BAR`.
30 static OH_YES: &mut i32 = &mut 42;
31
32 fn main() {
33     unsafe {
34         *MEH.x.get() = 99;
35     }
36     *OH_YES = 99; //~ ERROR cannot assign to `*OH_YES`, as `OH_YES` is an immutable static item
37 }