3 #![feature(rustc_attrs)]
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() {
9 struct Foo { x: u8, y: u8 }
11 let mut foo
= Foo { x: 0, y: 0 }
;
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>
17 //~^ ERROR: First Pass analysis includes:
18 //~| ERROR: Min Capture analysis includes:
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
28 println
!("({}, {})", z1
, z2
);
34 // `String`, `u16` are not aligned at a one byte boundary and are thus affected by repr(packed).
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() {
40 struct Foo { x: String, y: u16 }
42 let mut foo
= Foo { x: String::new(), y: 0 }
;
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>
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
59 println
!("({}, {})", z1
, z2
);
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
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() {
75 struct Foo { x: String }
77 let mut foo
= Foo { x: String::new() }
;
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>
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
90 //~^ NOTE: Capturing foo[(0, 0)] -> ByValue
91 //~| NOTE: foo[] captured as ByValue here
98 test_truncation_when_ref_and_move();
99 test_alignment_affected();
100 test_alignment_not_affected();