]> git.lizzy.rs Git - rust.git/blob - tests/ui/closures/2229_closure_analysis/issue-88476.rs
Rollup merge of #105784 - yanns:update_stdarch, r=Amanieu
[rust.git] / tests / ui / closures / 2229_closure_analysis / issue-88476.rs
1 // edition:2021
2
3 #![feature(rustc_attrs)]
4
5 // Test that we can't move out of struct that impls `Drop`.
6
7
8 use std::rc::Rc;
9
10 // Test that we restrict precision when moving not-`Copy` types, if any of the parent paths
11 // implement `Drop`. This is to ensure that we don't move out of a type that implements Drop.
12 pub fn test1() {
13     struct Foo(Rc<i32>);
14
15     impl Drop for Foo {
16         fn drop(self: &mut Foo) {}
17     }
18
19     let f = Foo(Rc::new(1));
20     let x = #[rustc_capture_analysis] move || {
21     //~^ ERROR: attributes on expressions are experimental
22     //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
23     //~| ERROR: First Pass analysis includes:
24     //~| ERROR: Min Capture analysis includes:
25         println!("{:?}", f.0);
26         //~^ NOTE: Capturing f[(0, 0)] -> ImmBorrow
27         //~| NOTE: Min Capture f[] -> ByValue
28     };
29
30     x();
31 }
32
33 // Test that we don't restrict precision when moving `Copy` types(i.e. when copying),
34 // even if any of the parent paths implement `Drop`.
35 fn test2() {
36     struct Character {
37         hp: u32,
38         name: String,
39     }
40
41     impl Drop for Character {
42         fn drop(&mut self) {}
43     }
44
45     let character = Character { hp: 100, name: format!("A") };
46
47     let c = #[rustc_capture_analysis] move || {
48     //~^ ERROR: attributes on expressions are experimental
49     //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
50     //~| ERROR: First Pass analysis includes:
51     //~| ERROR: Min Capture analysis includes:
52         println!("{}", character.hp)
53         //~^ NOTE: Capturing character[(0, 0)] -> ImmBorrow
54         //~| NOTE: Min Capture character[(0, 0)] -> ByValue
55     };
56
57     c();
58
59     println!("{}", character.name);
60 }
61
62 fn main() {}