summaryrefslogtreecommitdiffstats
path: root/tests/ui/closures/2229_closure_analysis/migrations/no_migrations.rs
blob: 8b75e226ab59bcf2ee64ca663b38d7913c570dac (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
// run-pass

// Set of test cases that don't need migrations

#![deny(rust_2021_incompatible_closure_captures)]

// Copy types as copied by the closure instead of being moved into the closure
// Therefore their drop order isn't tied to the closure and won't be requiring any
// migrations.
fn test1_only_copy_types() {
    let t = (0i32, 0i32);

    let c = || {
        let _t = t.0;
    };

    c();
}

// Same as test1 but using a move closure
fn test2_only_copy_types_move_closure() {
    let t = (0i32, 0i32);

    let c = move || {
        println!("{}", t.0);
    };

    c();
}

// Don't need to migrate if captured by ref
fn test3_only_copy_types_move_closure() {
    let t = (String::new(), String::new());

    let c = || {
        println!("{}", t.0);
    };

    c();
}

// Test migration analysis in case of Insignificant Drop + Non Drop aggregates.
// Note in this test the closure captures a non Drop type and therefore the variable
// is only captured by ref.
fn test4_insignificant_drop_non_drop_aggregate() {
    let t = (String::new(), 0i32);

    let c = || {
        let _t = t.1;
    };

    c();
}

struct Foo(i32);
impl Drop for Foo {
    fn drop(&mut self) {
        println!("{:?} dropped", self.0);
    }
}

// Test migration analysis in case of Significant Drop + Non Drop aggregates.
// Note in this test the closure captures a non Drop type and therefore the variable
// is only captured by ref.
fn test5_significant_drop_non_drop_aggregate() {
    let t = (Foo(0), 0i32);

    let c = || {
        let _t = t.1;
    };

    c();
}

fn main() {
    test1_only_copy_types();
    test2_only_copy_types_move_closure();
    test3_only_copy_types_move_closure();
    test4_insignificant_drop_non_drop_aggregate();
    test5_significant_drop_non_drop_aggregate();
}