]> git.lizzy.rs Git - rust.git/blob - tests/ui/closures/2229_closure_analysis/destructure_patterns.rs
Move /src/test to /tests
[rust.git] / tests / ui / closures / 2229_closure_analysis / destructure_patterns.rs
1 // edition:2021
2
3 #![feature(rustc_attrs)]
4
5 // Test to ensure Index projections are handled properly during capture analysis
6 // The array should be moved in entirety, even though only some elements are used.
7 fn arrays() {
8     let arr: [String; 5] = [format!("A"), format!("B"), format!("C"), format!("D"), format!("E")];
9
10     let c = #[rustc_capture_analysis]
11     //~^ ERROR: attributes on expressions are experimental
12     //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
13     || {
14     //~^ ERROR: First Pass analysis includes:
15     //~| ERROR: Min Capture analysis includes:
16         let [a, b, .., e] = arr;
17         //~^ NOTE: Capturing arr[Index] -> ByValue
18         //~| NOTE: Capturing arr[Index] -> ByValue
19         //~| NOTE: Capturing arr[Index] -> ByValue
20         //~| NOTE: Min Capture arr[] -> ByValue
21         assert_eq!(a, "A");
22         assert_eq!(b, "B");
23         assert_eq!(e, "E");
24     };
25
26     c();
27 }
28
29 struct Point {
30     x: i32,
31     y: i32,
32     id: String,
33 }
34
35 fn structs() {
36     let mut p = Point { x: 10, y: 10, id: String::new() };
37
38     let c = #[rustc_capture_analysis]
39     //~^ ERROR: attributes on expressions are experimental
40     //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
41     || {
42     //~^ ERROR: First Pass analysis includes:
43     //~| ERROR: Min Capture analysis includes:
44         let Point { x: ref mut x, y: _, id: moved_id } = p;
45         //~^ NOTE: Capturing p[(0, 0)] -> MutBorrow
46         //~| NOTE: Capturing p[(2, 0)] -> ByValue
47         //~| NOTE: Min Capture p[(0, 0)] -> MutBorrow
48         //~| NOTE: Min Capture p[(2, 0)] -> ByValue
49
50         println!("{}, {}", x, moved_id);
51     };
52     c();
53 }
54
55 fn tuples() {
56     let mut t = (10, String::new(), (String::new(), 42));
57
58     let c = #[rustc_capture_analysis]
59     //~^ ERROR: attributes on expressions are experimental
60     //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
61     || {
62     //~^ ERROR: First Pass analysis includes:
63     //~| ERROR: Min Capture analysis includes:
64         let (ref mut x, ref ref_str, (moved_s, _)) = t;
65         //~^ NOTE: Capturing t[(0, 0)] -> MutBorrow
66         //~| NOTE: Capturing t[(1, 0)] -> ImmBorrow
67         //~| NOTE: Capturing t[(2, 0),(0, 0)] -> ByValue
68         //~| NOTE: Min Capture t[(0, 0)] -> MutBorrow
69         //~| NOTE: Min Capture t[(1, 0)] -> ImmBorrow
70         //~| NOTE: Min Capture t[(2, 0),(0, 0)] -> ByValue
71
72         println!("{}, {} {}", x, ref_str, moved_s);
73     };
74     c();
75 }
76
77 fn main() {}