summaryrefslogtreecommitdiffstats
path: root/tests/mir-opt/building/custom/terminators.rs
blob: c23233fcf9aca05fa6531a1b327cc040f51f2be3 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#![feature(custom_mir, core_intrinsics)]

extern crate core;
use core::intrinsics::mir::*;

fn ident<T>(t: T) -> T {
    t
}

// EMIT_MIR terminators.direct_call.built.after.mir
#[custom_mir(dialect = "built")]
fn direct_call(x: i32) -> i32 {
    mir!(
        {
            Call(RET, retblock, ident(x))
        }

        retblock = {
            Return()
        }
    )
}

// EMIT_MIR terminators.indirect_call.built.after.mir
#[custom_mir(dialect = "built")]
fn indirect_call(x: i32, f: fn(i32) -> i32) -> i32 {
    mir!(
        {
            Call(RET, retblock, f(x))
        }

        retblock = {
            Return()
        }
    )
}

struct WriteOnDrop<'a>(&'a mut i32, i32);

impl<'a> Drop for WriteOnDrop<'a> {
    fn drop(&mut self) {
        *self.0 = self.1;
    }
}

// EMIT_MIR terminators.drop_first.built.after.mir
#[custom_mir(dialect = "built")]
fn drop_first<'a>(a: WriteOnDrop<'a>, b: WriteOnDrop<'a>) {
    mir!(
        {
            DropAndReplace(a, Move(b), retblock)
        }

        retblock = {
            Return()
        }
    )
}

// EMIT_MIR terminators.drop_second.built.after.mir
#[custom_mir(dialect = "built")]
fn drop_second<'a>(a: WriteOnDrop<'a>, b: WriteOnDrop<'a>) {
    mir!(
        {
            Drop(b, retblock)
        }

        retblock = {
            Return()
        }
    )
}

// EMIT_MIR terminators.assert_nonzero.built.after.mir
#[custom_mir(dialect = "built")]
fn assert_nonzero(a: i32) {
    mir!(
        {
            match a {
                0 => unreachable,
                _ => retblock
            }
        }

        unreachable = {
            Unreachable()
        }

        retblock = {
            Return()
        }
    )
}

fn main() {
    assert_eq!(direct_call(5), 5);
    assert_eq!(indirect_call(5, ident), 5);

    let mut a = 0;
    let mut b = 0;
    drop_first(WriteOnDrop(&mut a, 1), WriteOnDrop(&mut b, 1));
    assert_eq!((a, b), (1, 0));

    let mut a = 0;
    let mut b = 0;
    drop_second(WriteOnDrop(&mut a, 1), WriteOnDrop(&mut b, 1));
    assert_eq!((a, b), (0, 1));
}