Skip to content

Commit 36f51c9

Browse files
committed
Add test for copy type in move closure
1 parent 10b536f commit 36f51c9

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/test/ui/closures/2229_closure_analysis/move_closure.rs

+15
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,21 @@ fn box_mut_2() {
195195
//~| NOTE: Min Capture p_foo[Deref,Deref,(0, 0)] -> UniqueImmBorrow
196196
}
197197

198+
// Test that move closures can take ownership of Copy type
199+
fn returned_closure_owns_copy_type_data() -> impl Fn() -> i32 {
200+
let x = 10;
201+
202+
let c = #[rustc_capture_analysis] move || x;
203+
//~^ ERROR: attributes on expressions are experimental
204+
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
205+
//~| First Pass analysis includes:
206+
//~| NOTE: Capturing x[] -> ImmBorrow
207+
//~| Min Capture analysis includes:
208+
//~| NOTE: Min Capture x[] -> ByValue
209+
210+
c
211+
}
212+
198213
fn main() {
199214
simple_move_closure();
200215
simple_ref();

src/test/ui/closures/2229_closure_analysis/move_closure.stderr

+34-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,39 @@ LL | let c = #[rustc_capture_analysis] move || p_foo.x += 10;
8888
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
8989
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable
9090

91+
error[E0658]: attributes on expressions are experimental
92+
--> $DIR/move_closure.rs:202:13
93+
|
94+
LL | let c = #[rustc_capture_analysis] move || x;
95+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
96+
|
97+
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
98+
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable
99+
100+
error: First Pass analysis includes:
101+
--> $DIR/move_closure.rs:202:39
102+
|
103+
LL | let c = #[rustc_capture_analysis] move || x;
104+
| ^^^^^^^^^
105+
|
106+
note: Capturing x[] -> ImmBorrow
107+
--> $DIR/move_closure.rs:202:47
108+
|
109+
LL | let c = #[rustc_capture_analysis] move || x;
110+
| ^
111+
112+
error: Min Capture analysis includes:
113+
--> $DIR/move_closure.rs:202:39
114+
|
115+
LL | let c = #[rustc_capture_analysis] move || x;
116+
| ^^^^^^^^^
117+
|
118+
note: Min Capture x[] -> ByValue
119+
--> $DIR/move_closure.rs:202:47
120+
|
121+
LL | let c = #[rustc_capture_analysis] move || x;
122+
| ^
123+
91124
error: First Pass analysis includes:
92125
--> $DIR/move_closure.rs:15:5
93126
|
@@ -424,6 +457,6 @@ note: Min Capture p_foo[Deref,Deref,(0, 0)] -> UniqueImmBorrow
424457
LL | let c = #[rustc_capture_analysis] move || p_foo.x += 10;
425458
| ^^^^^^^
426459

427-
error: aborting due to 30 previous errors
460+
error: aborting due to 33 previous errors
428461

429462
For more information about this error, try `rustc --explain E0658`.

src/test/ui/closures/2229_closure_analysis/run_pass/move_closure.rs

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
// Test that move closures compile properly with `capture_disjoint_fields` enabled.
55

6+
#![allow(unused)]
7+
68
fn simple_ref() {
79
let mut s = 10;
810
let ref_s = &mut s;
@@ -92,6 +94,15 @@ fn data_moved_but_not_fn_once() {
9294
c();
9395
}
9496

97+
// Test that move closures can take ownership of Copy type
98+
fn returned_closure_owns_copy_type_data() -> impl Fn() -> i32 {
99+
let x = 10;
100+
101+
let c = move || x;
102+
103+
c
104+
}
105+
95106
fn main() {
96107
simple_ref();
97108
struct_contains_ref_to_another_struct();
@@ -100,4 +111,6 @@ fn main() {
100111

101112
disjoint_via_ref();
102113
data_moved_but_not_fn_once();
114+
115+
returned_closure_owns_copy_type_data();
103116
}

0 commit comments

Comments
 (0)