about summary refs log tree commit diff
path: root/tests/ui/consts/const-eval/ub-write-through-immutable.rs
blob: 28c7a617a4d5884c29be738179a8e422de778afd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//! Ensure we catch UB due to writing through a shared reference.
#![allow(invalid_reference_casting)]

use std::cell::UnsafeCell;
use std::mem;

const WRITE_AFTER_CAST: () = unsafe {
    let mut x = 0;
    let ptr = &x as *const i32 as *mut i32;
    *ptr = 0; //~ERROR: immutable
};

const WRITE_AFTER_TRANSMUTE: () = unsafe {
    let mut x = 0;
    let ptr: *mut i32 = mem::transmute(&x);
    *ptr = 0; //~ERROR: immutable
};

// it's okay when there is interior mutability;
const WRITE_INTERIOR_MUT: () = unsafe {
    let x = UnsafeCell::new(0);
    let ptr = &x as *const _ as *mut i32;
    *ptr = 0;
};

fn main() {}