]> git.lizzy.rs Git - rust.git/blob - tests/ui/closures/2229_closure_analysis/nested-closure.rs
Auto merge of #107843 - bjorn3:sync_cg_clif-2023-02-09, r=bjorn3
[rust.git] / tests / ui / closures / 2229_closure_analysis / nested-closure.rs
1 // edition:2021
2
3 #![feature(rustc_attrs)]
4
5 struct Point {
6     x: i32,
7     y: i32,
8 }
9
10 // This testcase ensures that nested closures are handles properly
11 // - The nested closure is analyzed first.
12 // - The capture kind of the nested closure is accounted for by the enclosing closure
13 // - Any captured path by the nested closure that starts off a local variable in the enclosing
14 // closure is not listed as a capture of the enclosing closure.
15
16 fn main() {
17     let mut p = Point { x: 5, y: 20 };
18
19     let mut c1 = #[rustc_capture_analysis]
20         //~^ ERROR: attributes on expressions are experimental
21         //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
22     || {
23     //~^ ERROR: First Pass analysis includes:
24     //~| ERROR: Min Capture analysis includes:
25         println!("{}", p.x);
26         //~^ NOTE: Capturing p[(0, 0)] -> ImmBorrow
27         //~| NOTE: Min Capture p[(0, 0)] -> ImmBorrow
28         let incr = 10;
29         let mut c2 = #[rustc_capture_analysis]
30         //~^ ERROR: attributes on expressions are experimental
31         //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
32         || p.y += incr;
33         //~^ ERROR: First Pass analysis includes:
34         //~| ERROR: Min Capture analysis includes:
35         //~| NOTE: Capturing p[(1, 0)] -> MutBorrow
36         //~| NOTE: Capturing incr[] -> ImmBorrow
37         //~| NOTE: Min Capture p[(1, 0)] -> MutBorrow
38         //~| NOTE: Min Capture incr[] -> ImmBorrow
39         //~| NOTE: Capturing p[(1, 0)] -> MutBorrow
40         //~| NOTE: Min Capture p[(1, 0)] -> MutBorrow
41         c2();
42         println!("{}", p.y);
43         //~^ NOTE: Capturing p[(1, 0)] -> ImmBorrow
44     };
45
46     c1();
47
48     let px = &p.x;
49
50     println!("{}", px);
51
52     c1();
53 }