]> git.proxmox.com Git - rustc.git/blob - src/test/ui/closures/2229_closure_analysis/move_closure.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / src / test / ui / closures / 2229_closure_analysis / move_closure.rs
1 // Test that move closures drop derefs with `capture_disjoint_fields` enabled.
2
3 #![feature(capture_disjoint_fields)]
4 //~^ WARNING: the feature `capture_disjoint_fields` is incomplete
5 //~| NOTE: `#[warn(incomplete_features)]` on by default
6 //~| NOTE: see issue #53488 <https://github.com/rust-lang/rust/issues/53488>
7 #![feature(rustc_attrs)]
8
9 fn simple_move_closure() {
10 struct S(String);
11 struct T(S);
12
13 let t = T(S("s".into()));
14 let mut c = #[rustc_capture_analysis]
15 //~^ ERROR: attributes on expressions are experimental
16 //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
17 move || {
18 //~^ ERROR: First Pass analysis includes:
19 //~| ERROR: Min Capture analysis includes:
20 t.0.0 = "new S".into();
21 //~^ NOTE: Capturing t[(0, 0),(0, 0)] -> ByValue
22 //~| NOTE: Min Capture t[(0, 0),(0, 0)] -> ByValue
23 };
24 c();
25 }
26
27 // Test move closure use reborrows when using references
28 fn simple_ref() {
29 let mut s = 10;
30 let ref_s = &mut s;
31
32 let mut c = #[rustc_capture_analysis]
33 //~^ ERROR: attributes on expressions are experimental
34 //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
35 move || {
36 //~^ ERROR: First Pass analysis includes:
37 //~| ERROR: Min Capture analysis includes:
38 *ref_s += 10;
39 //~^ NOTE: Capturing ref_s[Deref] -> UniqueImmBorrow
40 //~| NOTE: Min Capture ref_s[Deref] -> UniqueImmBorrow
41 };
42 c();
43 }
44
45 // Test move closure use reborrows when using references
46 fn struct_contains_ref_to_another_struct_1() {
47 struct S(String);
48 struct T<'a>(&'a mut S);
49
50 let mut s = S("s".into());
51 let t = T(&mut s);
52
53 let mut c = #[rustc_capture_analysis]
54 //~^ ERROR: attributes on expressions are experimental
55 //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
56 move || {
57 //~^ ERROR: First Pass analysis includes:
58 //~| ERROR: Min Capture analysis includes:
59 t.0.0 = "new s".into();
60 //~^ NOTE: Capturing t[(0, 0),Deref,(0, 0)] -> UniqueImmBorrow
61 //~| NOTE: Min Capture t[(0, 0),Deref,(0, 0)] -> UniqueImmBorrow
62 };
63
64 c();
65 }
66
67 // Test that we can use reborrows to read data of Copy types
68 // i.e. without truncating derefs
69 fn struct_contains_ref_to_another_struct_2() {
70 struct S(i32);
71 struct T<'a>(&'a S);
72
73 let s = S(0);
74 let t = T(&s);
75
76 let mut c = #[rustc_capture_analysis]
77 //~^ ERROR: attributes on expressions are experimental
78 //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
79 move || {
80 //~^ ERROR: First Pass analysis includes:
81 //~| ERROR: Min Capture analysis includes:
82 let _t = t.0.0;
83 //~^ NOTE: Capturing t[(0, 0),Deref,(0, 0)] -> ImmBorrow
84 //~| NOTE: Min Capture t[(0, 0),Deref,(0, 0)] -> ImmBorrow
85 };
86
87 c();
88 }
89
90 // Test that we can use truncate to move out of !Copy types
91 fn struct_contains_ref_to_another_struct_3() {
92 struct S(String);
93 struct T<'a>(&'a S);
94
95 let s = S("s".into());
96 let t = T(&s);
97
98 let mut c = #[rustc_capture_analysis]
99 //~^ ERROR: attributes on expressions are experimental
100 //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
101 move || {
102 //~^ ERROR: First Pass analysis includes:
103 //~| ERROR: Min Capture analysis includes:
104 let _t = t.0.0;
105 //~^ NOTE: Capturing t[(0, 0),Deref,(0, 0)] -> ImmBorrow
106 //~| NOTE: Capturing t[(0, 0)] -> ByValue
107 //~| NOTE: Min Capture t[(0, 0)] -> ByValue
108 };
109
110 c();
111 }
112
113 // Test that derefs of box are truncated in move closures
114 fn truncate_box_derefs() {
115 struct S(i32);
116
117 let b = Box::new(S(10));
118
119 let c = #[rustc_capture_analysis]
120 //~^ ERROR: attributes on expressions are experimental
121 //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
122 move || {
123 //~^ ERROR: First Pass analysis includes:
124 //~| ERROR: Min Capture analysis includes:
125 let _t = b.0;
126 //~^ NOTE: Capturing b[Deref,(0, 0)] -> ByValue
127 //~| NOTE: Capturing b[] -> ByValue
128 //~| NOTE: Min Capture b[] -> ByValue
129 };
130
131 c();
132 }
133
134 fn main() {
135 simple_move_closure();
136 simple_ref();
137 struct_contains_ref_to_another_struct_1();
138 struct_contains_ref_to_another_struct_2();
139 struct_contains_ref_to_another_struct_3();
140 truncate_box_derefs();
141 }