summary refs log tree commit diff
path: root/src/tools/miri/tests/pass/ptr_raw.rs
blob: 2f1843589072ffb0d6be979130ee16721c1d6131 (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
27
28
29
30
31
32
33
34
fn basic_raw() {
    let mut x = 12;
    let x = &mut x;

    assert_eq!(*x, 12);

    let raw = x as *mut i32;
    unsafe {
        *raw = 42;
    }

    assert_eq!(*x, 42);

    let raw = x as *mut i32;
    unsafe {
        *raw = 12;
    }
    *x = 23;

    assert_eq!(*x, 23);
}

fn assign_overlapping() {
    // Test an assignment where LHS and RHS alias.
    // In Mir, that's UB (see `fail/overlapping_assignment.rs`), but in surface Rust this is allowed.
    let mut mem = [0u32; 4];
    let ptr = &mut mem as *mut [u32; 4];
    unsafe { *ptr = *ptr };
}

fn main() {
    basic_raw();
    assign_overlapping();
}