2021-06-27 01:22:46 -05:00
|
|
|
// edition:2021
|
|
|
|
|
2020-12-14 02:09:17 -06:00
|
|
|
// Test that move closures drop derefs with `capture_disjoint_fields` enabled.
|
|
|
|
|
|
|
|
#![feature(rustc_attrs)]
|
|
|
|
|
2021-02-11 18:36:05 -06:00
|
|
|
fn simple_move_closure() {
|
|
|
|
struct S(String);
|
|
|
|
struct T(S);
|
|
|
|
|
|
|
|
let t = T(S("s".into()));
|
|
|
|
let mut c = #[rustc_capture_analysis]
|
|
|
|
//~^ ERROR: attributes on expressions are experimental
|
|
|
|
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
|
2024-01-10 00:39:02 -06:00
|
|
|
//~| NOTE: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
2021-02-11 18:36:05 -06:00
|
|
|
move || {
|
|
|
|
//~^ ERROR: First Pass analysis includes:
|
|
|
|
//~| ERROR: Min Capture analysis includes:
|
|
|
|
t.0.0 = "new S".into();
|
2021-07-08 23:25:41 -05:00
|
|
|
//~^ NOTE: Capturing t[(0, 0),(0, 0)] -> MutBorrow
|
2021-02-11 18:36:05 -06:00
|
|
|
//~| NOTE: Min Capture t[(0, 0),(0, 0)] -> ByValue
|
|
|
|
};
|
|
|
|
c();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test move closure use reborrows when using references
|
2020-12-14 02:09:17 -06:00
|
|
|
fn simple_ref() {
|
|
|
|
let mut s = 10;
|
|
|
|
let ref_s = &mut s;
|
|
|
|
|
|
|
|
let mut c = #[rustc_capture_analysis]
|
|
|
|
//~^ ERROR: attributes on expressions are experimental
|
|
|
|
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
|
2024-01-10 00:39:02 -06:00
|
|
|
//~| NOTE: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
2020-12-14 02:09:17 -06:00
|
|
|
move || {
|
|
|
|
//~^ ERROR: First Pass analysis includes:
|
|
|
|
//~| ERROR: Min Capture analysis includes:
|
|
|
|
*ref_s += 10;
|
2021-07-27 01:24:46 -05:00
|
|
|
//~^ NOTE: Capturing ref_s[Deref] -> MutBorrow
|
2021-08-29 15:28:58 -05:00
|
|
|
//~| NOTE: Min Capture ref_s[] -> ByValue
|
2020-12-14 02:09:17 -06:00
|
|
|
};
|
|
|
|
c();
|
|
|
|
}
|
|
|
|
|
2021-02-11 18:36:05 -06:00
|
|
|
// Test move closure use reborrows when using references
|
|
|
|
fn struct_contains_ref_to_another_struct_1() {
|
2020-12-14 02:09:17 -06:00
|
|
|
struct S(String);
|
|
|
|
struct T<'a>(&'a mut S);
|
|
|
|
|
|
|
|
let mut s = S("s".into());
|
|
|
|
let t = T(&mut s);
|
|
|
|
|
|
|
|
let mut c = #[rustc_capture_analysis]
|
|
|
|
//~^ ERROR: attributes on expressions are experimental
|
|
|
|
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
|
2024-01-10 00:39:02 -06:00
|
|
|
//~| NOTE: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
2020-12-14 02:09:17 -06:00
|
|
|
move || {
|
|
|
|
//~^ ERROR: First Pass analysis includes:
|
|
|
|
//~| ERROR: Min Capture analysis includes:
|
|
|
|
t.0.0 = "new s".into();
|
2021-07-27 01:24:46 -05:00
|
|
|
//~^ NOTE: Capturing t[(0, 0),Deref,(0, 0)] -> MutBorrow
|
2021-08-29 15:28:58 -05:00
|
|
|
//~| NOTE: Min Capture t[(0, 0)] -> ByValue
|
2020-12-14 02:09:17 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
c();
|
|
|
|
}
|
|
|
|
|
2021-02-11 18:36:05 -06:00
|
|
|
// Test that we can use reborrows to read data of Copy types
|
|
|
|
// i.e. without truncating derefs
|
|
|
|
fn struct_contains_ref_to_another_struct_2() {
|
|
|
|
struct S(i32);
|
|
|
|
struct T<'a>(&'a S);
|
|
|
|
|
|
|
|
let s = S(0);
|
|
|
|
let t = T(&s);
|
|
|
|
|
|
|
|
let mut c = #[rustc_capture_analysis]
|
|
|
|
//~^ ERROR: attributes on expressions are experimental
|
|
|
|
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
|
2024-01-10 00:39:02 -06:00
|
|
|
//~| NOTE: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
2021-02-11 18:36:05 -06:00
|
|
|
move || {
|
|
|
|
//~^ ERROR: First Pass analysis includes:
|
|
|
|
//~| ERROR: Min Capture analysis includes:
|
|
|
|
let _t = t.0.0;
|
2021-07-09 02:55:03 -05:00
|
|
|
//~^ NOTE: Capturing t[(0, 0),Deref,(0, 0)] -> ImmBorrow
|
2021-08-29 15:28:58 -05:00
|
|
|
//~| NOTE: Min Capture t[(0, 0)] -> ByValue
|
2021-02-11 18:36:05 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
c();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that we can use truncate to move out of !Copy types
|
|
|
|
fn struct_contains_ref_to_another_struct_3() {
|
2020-12-14 02:09:17 -06:00
|
|
|
struct S(String);
|
2021-02-11 18:36:05 -06:00
|
|
|
struct T<'a>(&'a S);
|
|
|
|
|
|
|
|
let s = S("s".into());
|
|
|
|
let t = T(&s);
|
2020-12-14 02:09:17 -06:00
|
|
|
|
|
|
|
let mut c = #[rustc_capture_analysis]
|
|
|
|
//~^ ERROR: attributes on expressions are experimental
|
|
|
|
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
|
2024-01-10 00:39:02 -06:00
|
|
|
//~| NOTE: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
2020-12-14 02:09:17 -06:00
|
|
|
move || {
|
|
|
|
//~^ ERROR: First Pass analysis includes:
|
|
|
|
//~| ERROR: Min Capture analysis includes:
|
2021-02-11 18:36:05 -06:00
|
|
|
let _t = t.0.0;
|
2021-07-09 02:55:03 -05:00
|
|
|
//~^ NOTE: Capturing t[(0, 0),Deref,(0, 0)] -> ByValue
|
2021-02-11 18:36:05 -06:00
|
|
|
//~| NOTE: Min Capture t[(0, 0)] -> ByValue
|
|
|
|
};
|
|
|
|
|
|
|
|
c();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that derefs of box are truncated in move closures
|
|
|
|
fn truncate_box_derefs() {
|
|
|
|
struct S(i32);
|
|
|
|
|
|
|
|
|
2021-06-18 15:35:10 -05:00
|
|
|
// Content within the box is moved within the closure
|
|
|
|
let b = Box::new(S(10));
|
2021-02-11 18:36:05 -06:00
|
|
|
let c = #[rustc_capture_analysis]
|
|
|
|
//~^ ERROR: attributes on expressions are experimental
|
|
|
|
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
|
2024-01-10 00:39:02 -06:00
|
|
|
//~| NOTE: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
2021-02-11 18:36:05 -06:00
|
|
|
move || {
|
|
|
|
//~^ ERROR: First Pass analysis includes:
|
|
|
|
//~| ERROR: Min Capture analysis includes:
|
|
|
|
let _t = b.0;
|
2021-07-08 23:25:41 -05:00
|
|
|
//~^ NOTE: Capturing b[Deref,(0, 0)] -> ImmBorrow
|
2021-02-11 18:36:05 -06:00
|
|
|
//~| NOTE: Min Capture b[] -> ByValue
|
2020-12-14 02:09:17 -06:00
|
|
|
};
|
2021-02-11 18:36:05 -06:00
|
|
|
|
2020-12-14 02:09:17 -06:00
|
|
|
c();
|
2021-06-18 15:35:10 -05:00
|
|
|
|
|
|
|
// Content within the box is used by a shared ref and the box is the root variable
|
|
|
|
let b = Box::new(S(10));
|
|
|
|
|
|
|
|
let c = #[rustc_capture_analysis]
|
|
|
|
//~^ ERROR: attributes on expressions are experimental
|
|
|
|
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
|
2024-01-10 00:39:02 -06:00
|
|
|
//~| NOTE: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
2021-06-18 15:35:10 -05:00
|
|
|
move || {
|
|
|
|
//~^ ERROR: First Pass analysis includes:
|
|
|
|
//~| ERROR: Min Capture analysis includes:
|
|
|
|
println!("{}", b.0);
|
2021-07-08 23:25:41 -05:00
|
|
|
//~^ NOTE: Capturing b[Deref,(0, 0)] -> ImmBorrow
|
2021-06-18 15:35:10 -05:00
|
|
|
//~| NOTE: Min Capture b[] -> ByValue
|
|
|
|
};
|
|
|
|
|
|
|
|
c();
|
|
|
|
|
|
|
|
// Content within the box is used by a shared ref and the box is not the root variable
|
|
|
|
let b = Box::new(S(10));
|
|
|
|
let t = (0, b);
|
|
|
|
|
|
|
|
let c = #[rustc_capture_analysis]
|
|
|
|
//~^ ERROR: attributes on expressions are experimental
|
|
|
|
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
|
2024-01-10 00:39:02 -06:00
|
|
|
//~| NOTE: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
2021-06-18 15:35:10 -05:00
|
|
|
move || {
|
|
|
|
//~^ ERROR: First Pass analysis includes:
|
|
|
|
//~| ERROR: Min Capture analysis includes:
|
|
|
|
println!("{}", t.1.0);
|
2021-07-08 23:25:41 -05:00
|
|
|
//~^ NOTE: Capturing t[(1, 0),Deref,(0, 0)] -> ImmBorrow
|
2021-06-18 15:35:10 -05:00
|
|
|
//~| NOTE: Min Capture t[(1, 0)] -> ByValue
|
|
|
|
};
|
2020-12-14 02:09:17 -06:00
|
|
|
}
|
|
|
|
|
2021-07-09 03:34:11 -05:00
|
|
|
struct Foo { x: i32 }
|
|
|
|
|
|
|
|
// Ensure that even in move closures, if the data is not owned by the root variable
|
|
|
|
// then we don't truncate the derefs or a ByValue capture, rather do a reborrow
|
|
|
|
fn box_mut_1() {
|
|
|
|
let mut foo = Foo { x: 0 } ;
|
|
|
|
|
|
|
|
let p_foo = &mut foo;
|
|
|
|
let box_p_foo = Box::new(p_foo);
|
|
|
|
|
|
|
|
let c = #[rustc_capture_analysis] move || box_p_foo.x += 10;
|
|
|
|
//~^ ERROR: attributes on expressions are experimental
|
|
|
|
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
|
2024-01-10 00:39:02 -06:00
|
|
|
//~| NOTE: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
2021-07-09 03:34:11 -05:00
|
|
|
//~| First Pass analysis includes:
|
2021-07-27 01:24:46 -05:00
|
|
|
//~| NOTE: Capturing box_p_foo[Deref,Deref,(0, 0)] -> MutBorrow
|
2021-07-09 03:34:11 -05:00
|
|
|
//~| Min Capture analysis includes:
|
2021-08-29 15:28:58 -05:00
|
|
|
//~| NOTE: Min Capture box_p_foo[] -> ByValue
|
2021-07-09 03:34:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that even in move closures, if the data is not owned by the root variable
|
|
|
|
// then we don't truncate the derefs or a ByValue capture, rather do a reborrow
|
|
|
|
fn box_mut_2() {
|
|
|
|
let foo = Foo { x: 0 } ;
|
|
|
|
|
|
|
|
let mut box_foo = Box::new(foo);
|
|
|
|
let p_foo = &mut box_foo;
|
|
|
|
|
|
|
|
let c = #[rustc_capture_analysis] move || p_foo.x += 10;
|
|
|
|
//~^ ERROR: attributes on expressions are experimental
|
|
|
|
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
|
2024-01-10 00:39:02 -06:00
|
|
|
//~| NOTE: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
2021-07-09 03:34:11 -05:00
|
|
|
//~| First Pass analysis includes:
|
2021-07-27 01:24:46 -05:00
|
|
|
//~| NOTE: Capturing p_foo[Deref,Deref,(0, 0)] -> MutBorrow
|
2021-07-09 03:34:11 -05:00
|
|
|
//~| Min Capture analysis includes:
|
2021-08-29 15:28:58 -05:00
|
|
|
//~| NOTE: Min Capture p_foo[] -> ByValue
|
2021-07-09 03:34:11 -05:00
|
|
|
}
|
|
|
|
|
2021-07-13 23:27:39 -05:00
|
|
|
// Test that move closures can take ownership of Copy type
|
|
|
|
fn returned_closure_owns_copy_type_data() -> impl Fn() -> i32 {
|
|
|
|
let x = 10;
|
|
|
|
|
|
|
|
let c = #[rustc_capture_analysis] move || x;
|
|
|
|
//~^ ERROR: attributes on expressions are experimental
|
|
|
|
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
|
2024-01-10 00:39:02 -06:00
|
|
|
//~| NOTE: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
2021-07-13 23:27:39 -05:00
|
|
|
//~| First Pass analysis includes:
|
|
|
|
//~| NOTE: Capturing x[] -> ImmBorrow
|
|
|
|
//~| Min Capture analysis includes:
|
|
|
|
//~| NOTE: Min Capture x[] -> ByValue
|
|
|
|
|
|
|
|
c
|
|
|
|
}
|
|
|
|
|
2020-12-14 02:09:17 -06:00
|
|
|
fn main() {
|
2021-02-11 18:36:05 -06:00
|
|
|
simple_move_closure();
|
2020-12-14 02:09:17 -06:00
|
|
|
simple_ref();
|
2021-02-11 18:36:05 -06:00
|
|
|
struct_contains_ref_to_another_struct_1();
|
|
|
|
struct_contains_ref_to_another_struct_2();
|
|
|
|
struct_contains_ref_to_another_struct_3();
|
|
|
|
truncate_box_derefs();
|
2021-07-09 03:34:11 -05:00
|
|
|
box_mut_2();
|
|
|
|
box_mut_1();
|
2020-12-14 02:09:17 -06:00
|
|
|
}
|