]> git.lizzy.rs Git - rust.git/blob - tests/ui/closures/2229_closure_analysis/migrations/insignificant_drop_attr_migrations.fixed
Rollup merge of #106717 - klensy:typo, r=lcnr
[rust.git] / tests / ui / closures / 2229_closure_analysis / migrations / insignificant_drop_attr_migrations.fixed
1 // run-rustfix
2
3 #![deny(rust_2021_incompatible_closure_captures)]
4 //~^ NOTE: the lint level is defined here
5 #![feature(rustc_attrs)]
6 #![allow(unused)]
7
8 use std::sync::Mutex;
9
10     #[rustc_insignificant_dtor]
11 struct InsignificantDropPoint {
12     x: i32,
13     y: Mutex<i32>,
14 }
15
16 impl Drop for InsignificantDropPoint {
17     fn drop(&mut self) {}
18 }
19
20 struct SigDrop;
21
22 impl Drop for SigDrop {
23     fn drop(&mut self) {}
24 }
25
26 #[rustc_insignificant_dtor]
27 struct GenericStruct<T>(T, T);
28
29 impl<T> Drop for GenericStruct<T> {
30     fn drop(&mut self) {}
31 }
32
33 struct Wrapper<T>(GenericStruct<T>, i32);
34
35 // `SigDrop` implements drop and therefore needs to be migrated.
36 fn significant_drop_needs_migration() {
37     let t = (SigDrop {}, SigDrop {});
38
39     let c = || {
40         let _ = &t;
41         //~^ ERROR: drop order
42         //~| NOTE: for more information, see
43         //~| HELP: add a dummy let to cause `t` to be fully captured
44         let _t = t.0;
45         //~^ NOTE: in Rust 2018, this closure captures all of `t`, but in Rust 2021, it will only capture `t.0`
46     };
47
48     c();
49 }
50 //~^ NOTE: in Rust 2018, `t` is dropped here, but in Rust 2021, only `t.0` will be dropped here as part of the closure
51
52 // Even if a type implements an insignificant drop, if it's
53 // elements have a significant drop then the overall type is
54 // consdered to have an significant drop. Since the elements
55 // of `GenericStruct` implement drop, migration is required.
56 fn generic_struct_with_significant_drop_needs_migration() {
57     let t = Wrapper(GenericStruct(SigDrop {}, SigDrop {}), 5);
58
59     // move is used to force i32 to be copied instead of being a ref
60     let c = move || {
61         let _ = &t;
62         //~^ ERROR: drop order
63         //~| NOTE: for more information, see
64         //~| HELP: add a dummy let to cause `t` to be fully captured
65         let _t = t.1;
66         //~^ NOTE: in Rust 2018, this closure captures all of `t`, but in Rust 2021, it will only capture `t.1`
67     };
68
69     c();
70 }
71 //~^ NOTE: in Rust 2018, `t` is dropped here, but in Rust 2021, only `t.1` will be dropped here as part of the closure
72
73 fn main() {
74     significant_drop_needs_migration();
75     generic_struct_with_significant_drop_needs_migration();
76 }