File: nested-closure.rs

package info (click to toggle)
rustc 1.70.0%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 722,120 kB
  • sloc: xml: 147,962; javascript: 10,210; sh: 8,590; python: 8,220; ansic: 5,901; cpp: 4,635; makefile: 4,001; asm: 2,856
file content (53 lines) | stat: -rw-r--r-- 1,747 bytes parent folder | download | duplicates (5)
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
// edition:2021

#![feature(rustc_attrs)]

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

// This testcase ensures that nested closures are handles properly
// - The nested closure is analyzed first.
// - The capture kind of the nested closure is accounted for by the enclosing closure
// - Any captured path by the nested closure that starts off a local variable in the enclosing
// closure is not listed as a capture of the enclosing closure.

fn main() {
    let mut p = Point { x: 5, y: 20 };

    let mut c1 = #[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:
        println!("{}", p.x);
        //~^ NOTE: Capturing p[(0, 0)] -> ImmBorrow
        //~| NOTE: Min Capture p[(0, 0)] -> ImmBorrow
        let incr = 10;
        let mut c2 = #[rustc_capture_analysis]
        //~^ ERROR: attributes on expressions are experimental
        //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
        || p.y += incr;
        //~^ ERROR: First Pass analysis includes:
        //~| ERROR: Min Capture analysis includes:
        //~| NOTE: Capturing p[(1, 0)] -> MutBorrow
        //~| NOTE: Capturing incr[] -> ImmBorrow
        //~| NOTE: Min Capture p[(1, 0)] -> MutBorrow
        //~| NOTE: Min Capture incr[] -> ImmBorrow
        //~| NOTE: Capturing p[(1, 0)] -> MutBorrow
        //~| NOTE: Min Capture p[(1, 0)] -> MutBorrow
        c2();
        println!("{}", p.y);
        //~^ NOTE: Capturing p[(1, 0)] -> ImmBorrow
    };

    c1();

    let px = &p.x;

    println!("{}", px);

    c1();
}