]> git.lizzy.rs Git - rust.git/blob - tests/ui/closures/2229_closure_analysis/repr_packed.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / closures / 2229_closure_analysis / repr_packed.rs
1 // edition:2021
2
3 #![feature(rustc_attrs)]
4
5 // `u8` aligned at a byte and are unaffected by repr(packed).
6 // Therefore we can precisely (and safely) capture references to both the fields.
7 fn test_alignment_not_affected() {
8     #[repr(packed)]
9     struct Foo { x: u8, y: u8 }
10
11     let mut foo = Foo { x: 0, y: 0 };
12
13     let mut c = #[rustc_capture_analysis]
14     //~^ ERROR: attributes on expressions are experimental
15     //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
16     || {
17     //~^ ERROR: First Pass analysis includes:
18     //~| ERROR: Min Capture analysis includes:
19         let z1: &u8 = &foo.x;
20         //~^ NOTE: Capturing foo[(0, 0)] -> ImmBorrow
21         //~| NOTE: Min Capture foo[(0, 0)] -> ImmBorrow
22         let z2: &mut u8 = &mut foo.y;
23         //~^ NOTE: Capturing foo[(1, 0)] -> MutBorrow
24         //~| NOTE: Min Capture foo[(1, 0)] -> MutBorrow
25
26         *z2 = 42;
27
28         println!("({}, {})", z1, z2);
29     };
30
31     c();
32 }
33
34 // `String`, `u16` are not aligned at a one byte boundary and are thus affected by repr(packed).
35 //
36 // Here we test that the closure doesn't capture a reference point to `foo.x` but
37 // rather capture `foo` entirely.
38 fn test_alignment_affected() {
39     #[repr(packed)]
40     struct Foo { x: String, y: u16 }
41
42     let mut foo = Foo { x: String::new(), y: 0 };
43
44     let mut c = #[rustc_capture_analysis]
45     //~^ ERROR: attributes on expressions are experimental
46     //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
47     || {
48     //~^ ERROR: First Pass analysis includes:
49     //~| ERROR: Min Capture analysis includes:
50         let z1: &String = &foo.x;
51         //~^ NOTE: Capturing foo[] -> ImmBorrow
52         let z2: &mut u16 = &mut foo.y;
53         //~^ NOTE: Capturing foo[] -> MutBorrow
54         //~| NOTE: Min Capture foo[] -> MutBorrow
55
56
57         *z2 = 42;
58
59         println!("({}, {})", z1, z2);
60     };
61
62     c();
63 }
64
65 // Given how the closure desugaring is implemented (at least at the time of writing this test),
66 // we don't need to truncate the captured path to a reference into a packed-struct if the field
67 // being referenced will be moved into the closure, since it's safe to move out a field from a
68 // packed-struct.
69 //
70 // However to avoid surprises for the user, or issues when the closure is
71 // inlined we will truncate the capture to access just the struct regardless of if the field
72 // might get moved into the closure.
73 fn test_truncation_when_ref_and_move() {
74     #[repr(packed)]
75     struct Foo { x: String }
76
77     let mut foo = Foo { x: String::new() };
78
79     let c = #[rustc_capture_analysis]
80     //~^ ERROR: attributes on expressions are experimental
81     //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
82     || {
83     //~^ ERROR: First Pass analysis includes:
84     //~| ERROR: Min Capture analysis includes:
85         println!("{}", foo.x);
86         //~^ NOTE: Capturing foo[] -> ImmBorrow
87         //~| NOTE: Min Capture foo[] -> ByValue
88         //~| NOTE: foo[] used here
89         let _z = foo.x;
90         //~^ NOTE: Capturing foo[(0, 0)] -> ByValue
91         //~| NOTE: foo[] captured as ByValue here
92     };
93
94     c();
95 }
96
97 fn main() {
98     test_truncation_when_ref_and_move();
99     test_alignment_affected();
100     test_alignment_not_affected();
101 }