summaryrefslogtreecommitdiffstats
path: root/tests/ui/closures/2229_closure_analysis/run_pass/box.rs
blob: 73aca288faa88b1267dc7b79f77ced534275ba23 (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
// edition:2021
// run-pass

// Test precise capture when using boxes

struct MetaData { x: String, name: String }
struct Data { m: MetaData }
struct BoxedData(Box<Data>);
struct EvenMoreBoxedData(Box<BoxedData>);

// Mutate disjoint paths, one inside one outside the closure
fn box_1() {
    let m = MetaData { x: format!("x"), name: format!("name") };
    let d = Data { m };
    let b = BoxedData(Box::new(d));
    let mut e = EvenMoreBoxedData(Box::new(b));

    let mut c = || {
        e.0.0.m.x = format!("not-x");
    };

    e.0.0.m.name = format!("not-name");
    c();
}

// Mutate a path inside the closure and read a disjoint path outside the closure
fn box_2() {
    let m = MetaData { x: format!("x"), name: format!("name") };
    let d = Data { m };
    let b = BoxedData(Box::new(d));
    let mut e = EvenMoreBoxedData(Box::new(b));

    let mut c = || {
        e.0.0.m.x = format!("not-x");
    };

    println!("{}", e.0.0.m.name);
    c();
}

// Read a path inside the closure and mutate a disjoint path outside the closure
fn box_3() {
    let m = MetaData { x: format!("x"), name: format!("name") };
    let d = Data { m };
    let b = BoxedData(Box::new(d));
    let mut e = EvenMoreBoxedData(Box::new(b));

    let c = || {
        println!("{}", e.0.0.m.name);
    };

    e.0.0.m.x = format!("not-x");
    c();
}

// Read disjoint paths, one inside the closure and one outside the closure.
fn box_4() {
    let m = MetaData { x: format!("x"), name: format!("name") };
    let d = Data { m };
    let b = BoxedData(Box::new(d));
    let e = EvenMoreBoxedData(Box::new(b));

    let c = || {
        println!("{}", e.0.0.m.name);
    };

    println!("{}", e.0.0.m.x);
    c();
}

// Read the same path, once inside the closure and once outside the closure.
fn box_5() {
    let m = MetaData { x: format!("x"), name: format!("name") };
    let d = Data { m };
    let b = BoxedData(Box::new(d));
    let e = EvenMoreBoxedData(Box::new(b));

    let c = || {
        println!("{}", e.0.0.m.name);
    };

    println!("{}", e.0.0.m.name);
    c();
}

fn main() {
    box_1();
    box_2();
    box_3();
    box_4();
    box_5();
}