about summary refs log tree commit diff
path: root/tests/mir-opt/pre-codegen/simple_option_map.rs
blob: f0d7b51a64397dc855be016c247a7796734b2ba4 (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
//@ compile-flags: -O -C debuginfo=0 -Zmir-opt-level=2

#[inline]
fn map<T, U, F>(slf: Option<T>, f: F) -> Option<U>
where
    F: FnOnce(T) -> U,
{
    match slf {
        Some(x) => Some(f(x)),
        None => None,
    }
}

// EMIT_MIR simple_option_map.ezmap.PreCodegen.after.mir
pub fn ezmap(x: Option<i32>) -> Option<i32> {
    // We expect this to all be inlined, as though it was written without the
    // combinator and without the closure, using just a plain match.

    // CHECK-LABEL: fn ezmap
    // CHECK: [[INNER:_.+]] = copy ((_1 as Some).0: i32);
    // CHECK: [[SUCC:_.+]] = Add({{copy|move}} [[INNER]], const 1_i32);
    // CHECK: _0 = Option::<i32>::Some({{copy|move}} [[SUCC]]);
    map(x, |n| n + 1)
}

// EMIT_MIR simple_option_map.map_via_question_mark.PreCodegen.after.mir
pub fn map_via_question_mark(x: Option<i32>) -> Option<i32> {
    // FIXME(#138544): Ideally this would optimize out the `ControlFlow` local.

    // CHECK-LABEL: fn map_via_question_mark
    // CHECK: [[INNER:_.+]] = copy ((_1 as Some).0: i32);
    // CHECK: [[TEMP1:_.+]] = ControlFlow::<Option<Infallible>, i32>::Continue(copy [[INNER]]);
    // CHECK: [[TEMP2:_.+]] = copy (([[TEMP1]] as Continue).0: i32);
    // CHECK: [[SUCC:_.+]] = Add({{copy|move}} [[TEMP2]], const 1_i32);
    // CHECK: _0 = Option::<i32>::Some({{copy|move}} [[SUCC]]);
    Some(x? + 1)
}

fn main() {
    assert_eq!(None, ezmap(None));
    assert_eq!(None, map_via_question_mark(None));
}