summaryrefslogtreecommitdiffstats
path: root/tests/mir-opt/copy-prop/reborrow.rs
blob: 91b77966ba84587f8ff27da614196dd46f5fcd47 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
// ignore-wasm32 compiled with panic=abort by default
// Check that CopyProp considers reborrows as not mutating the pointer.
// unit-test: CopyProp

#![feature(raw_ref_op)]

#[inline(never)]
fn opaque(_: impl Sized) {}

// EMIT_MIR reborrow.remut.CopyProp.diff
fn remut(mut x: u8) {
    let a = &mut x;
    let b = &mut *a; //< this cannot mutate a.
    let c = a; //< so `c` and `a` can be merged.
    opaque(c);
}

// EMIT_MIR reborrow.reraw.CopyProp.diff
fn reraw(mut x: u8) {
    let a = &mut x;
    let b = &raw mut *a; //< this cannot mutate a.
    let c = a; //< so `c` and `a` can be merged.
    opaque(c);
}

// EMIT_MIR reborrow.miraw.CopyProp.diff
fn miraw(mut x: u8) {
    let a = &raw mut x;
    let b = unsafe { &raw mut *a }; //< this cannot mutate a.
    let c = a; //< so `c` and `a` can be merged.
    opaque(c);
}

// EMIT_MIR reborrow.demiraw.CopyProp.diff
fn demiraw(mut x: u8) {
    let a = &raw mut x;
    let b = unsafe { &mut *a }; //< this cannot mutate a.
    let c = a; //< so `c` and `a` can be merged.
    opaque(c);
}

fn main() {
    remut(0);
    reraw(0);
    miraw(0);
    demiraw(0);
}