]> git.proxmox.com Git - rustc.git/blob - src/test/ui/closures/2229_closure_analysis/migrations/insignificant_drop_attr_migrations.fixed
New upstream version 1.55.0+dfsg1
[rustc.git] / src / test / 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 struct InsignificantDropPoint {
9 x: i32,
10 y: i32,
11 }
12
13 impl Drop for InsignificantDropPoint {
14 #[rustc_insignificant_dtor]
15 fn drop(&mut self) {}
16 }
17
18 struct SigDrop;
19
20 impl Drop for SigDrop {
21 fn drop(&mut self) {}
22 }
23
24 struct GenericStruct<T>(T, T);
25
26 struct Wrapper<T>(GenericStruct<T>, i32);
27
28 impl<T> Drop for GenericStruct<T> {
29 #[rustc_insignificant_dtor]
30 fn drop(&mut self) {}
31 }
32
33 // `SigDrop` implements drop and therefore needs to be migrated.
34 fn significant_drop_needs_migration() {
35 let t = (SigDrop {}, SigDrop {});
36
37 let c = || { let _ = &t;
38 //~^ ERROR: drop order
39 //~| NOTE: for more information, see
40 //~| HELP: add a dummy let to cause `t` to be fully captured
41 let _t = t.0;
42 //~^ NOTE: in Rust 2018, closure captures all of `t`, but in Rust 2021, it only captures `t.0`
43 };
44
45 c();
46 }
47 //~^ NOTE: in Rust 2018, `t` would be dropped here, but in Rust 2021, only `t.0` would be dropped here alongside the closure
48
49 // Even if a type implements an insignificant drop, if it's
50 // elements have a significant drop then the overall type is
51 // consdered to have an significant drop. Since the elements
52 // of `GenericStruct` implement drop, migration is required.
53 fn generic_struct_with_significant_drop_needs_migration() {
54 let t = Wrapper(GenericStruct(SigDrop {}, SigDrop {}), 5);
55
56 // move is used to force i32 to be copied instead of being a ref
57 let c = move || { let _ = &t;
58 //~^ ERROR: drop order
59 //~| NOTE: for more information, see
60 //~| HELP: add a dummy let to cause `t` to be fully captured
61 let _t = t.1;
62 //~^ NOTE: in Rust 2018, closure captures all of `t`, but in Rust 2021, it only captures `t.1`
63 };
64
65 c();
66 }
67 //~^ NOTE: in Rust 2018, `t` would be dropped here, but in Rust 2021, only `t.1` would be dropped here alongside the closure
68
69 fn main() {
70 significant_drop_needs_migration();
71 generic_struct_with_significant_drop_needs_migration();
72 }