summaryrefslogtreecommitdiffstats
path: root/tests/ui/closures/2229_closure_analysis/destructure_patterns.rs
blob: 6c65a7bf87b96c4aa6c4884175315f7092acb7d5 (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
// edition:2021

#![feature(rustc_attrs)]

// Test to ensure Index projections are handled properly during capture analysis
// The array should be moved in entirety, even though only some elements are used.
fn arrays() {
    let arr: [String; 5] = [format!("A"), format!("B"), format!("C"), format!("D"), format!("E")];

    let c = #[rustc_capture_analysis]
    //~^ ERROR: attributes on expressions are experimental
    //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
    || {
    //~^ ERROR: First Pass analysis includes:
    //~| ERROR: Min Capture analysis includes:
        let [a, b, .., e] = arr;
        //~^ NOTE: Capturing arr[Index] -> ByValue
        //~| NOTE: Capturing arr[Index] -> ByValue
        //~| NOTE: Capturing arr[Index] -> ByValue
        //~| NOTE: Min Capture arr[] -> ByValue
        assert_eq!(a, "A");
        assert_eq!(b, "B");
        assert_eq!(e, "E");
    };

    c();
}

struct Point {
    x: i32,
    y: i32,
    id: String,
}

fn structs() {
    let mut p = Point { x: 10, y: 10, id: String::new() };

    let c = #[rustc_capture_analysis]
    //~^ ERROR: attributes on expressions are experimental
    //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
    || {
    //~^ ERROR: First Pass analysis includes:
    //~| ERROR: Min Capture analysis includes:
        let Point { x: ref mut x, y: _, id: moved_id } = p;
        //~^ NOTE: Capturing p[(0, 0)] -> MutBorrow
        //~| NOTE: Capturing p[(2, 0)] -> ByValue
        //~| NOTE: Min Capture p[(0, 0)] -> MutBorrow
        //~| NOTE: Min Capture p[(2, 0)] -> ByValue

        println!("{}, {}", x, moved_id);
    };
    c();
}

fn tuples() {
    let mut t = (10, String::new(), (String::new(), 42));

    let c = #[rustc_capture_analysis]
    //~^ ERROR: attributes on expressions are experimental
    //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
    || {
    //~^ ERROR: First Pass analysis includes:
    //~| ERROR: Min Capture analysis includes:
        let (ref mut x, ref ref_str, (moved_s, _)) = t;
        //~^ NOTE: Capturing t[(0, 0)] -> MutBorrow
        //~| NOTE: Capturing t[(1, 0)] -> ImmBorrow
        //~| NOTE: Capturing t[(2, 0),(0, 0)] -> ByValue
        //~| NOTE: Min Capture t[(0, 0)] -> MutBorrow
        //~| NOTE: Min Capture t[(1, 0)] -> ImmBorrow
        //~| NOTE: Min Capture t[(2, 0),(0, 0)] -> ByValue

        println!("{}, {} {}", x, ref_str, moved_s);
    };
    c();
}

fn main() {}