summaryrefslogtreecommitdiffstats
path: root/tests/mir-opt/building/custom/projections.rs
blob: ac23fe59097f35106b79fd0dab985855d72dd2cd (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
// skip-filecheck
#![feature(custom_mir, core_intrinsics)]

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

union U {
    a: i32,
    b: u32,
}

// EMIT_MIR projections.unions.built.after.mir
#[custom_mir(dialect = "built")]
fn unions(u: U) -> i32 {
    mir!({
        RET = u.a;
        Return()
    })
}

// EMIT_MIR projections.tuples.built.after.mir
#[custom_mir(dialect = "analysis", phase = "post-cleanup")]
fn tuples(i: (u32, i32)) -> (u32, i32) {
    mir!(
        type RET = (u32, i32);
        {
            RET.0 = i.0;
            RET.1 = i.1;
            Return()
        }
    )
}

// EMIT_MIR projections.unwrap.built.after.mir
#[custom_mir(dialect = "built")]
fn unwrap(opt: Option<i32>) -> i32 {
    mir!({
        RET = Field(Variant(opt, 1), 0);
        Return()
    })
}

// EMIT_MIR projections.unwrap_deref.built.after.mir
#[custom_mir(dialect = "built")]
fn unwrap_deref(opt: Option<&i32>) -> i32 {
    mir!({
        RET = *Field::<&i32>(Variant(opt, 1), 0);
        Return()
    })
}

// EMIT_MIR projections.set.built.after.mir
#[custom_mir(dialect = "built")]
fn set(opt: &mut Option<i32>) {
    mir!({
        place!(Field(Variant(*opt, 1), 0)) = 10;
        Return()
    })
}

// EMIT_MIR projections.simple_index.built.after.mir
#[custom_mir(dialect = "built")]
fn simple_index(a: [i32; 10], b: &[i32]) -> i32 {
    mir!({
        let temp = 3;
        RET = a[temp];
        RET = (*b)[temp];
        Return()
    })
}

// EMIT_MIR projections.copy_for_deref.built.after.mir
#[custom_mir(dialect = "runtime", phase = "initial")]
fn copy_for_deref(x: (&i32, i32)) -> i32 {
    mir!(
        let temp: &i32;
        {
            temp = CopyForDeref(x.0);
            RET = *temp;
            Return()
        }
    )
}

fn main() {
    assert_eq!(unions(U { a: 5 }), 5);
    assert_eq!(tuples((5, 6)), (5, 6));

    assert_eq!(unwrap(Some(5)), 5);
    assert_eq!(unwrap_deref(Some(&5)), 5);
    let mut o = Some(5);
    set(&mut o);
    assert_eq!(o, Some(10));

    assert_eq!(simple_index([0; 10], &[0; 10]), 0);

    let one = 1;
    assert_eq!(copy_for_deref((&one, one)), 1);
}