]> git.proxmox.com Git - rustc.git/blame - src/test/ui/closures/2229_closure_analysis/diagnostics/repr_packed.rs
Merge tag 'debian/1.52.1+dfsg1-1_exp2' into proxmox/buster
[rustc.git] / src / test / ui / closures / 2229_closure_analysis / diagnostics / repr_packed.rs
CommitLineData
6a06907d
XL
1// check-pass
2
3#![feature(capture_disjoint_fields)]
4//~^ WARNING: the feature `capture_disjoint_fields` is incomplete
5
6// Given how the closure desugaring is implemented (at least at the time of writing this test),
7// we don't need to truncate the captured path to a reference into a packed-struct if the field
8// being referenced will be moved into the closure, since it's safe to move out a field from a
9// packed-struct.
10//
11// However to avoid surprises for the user, or issues when the closure is
12// inlined we will truncate the capture to access just the struct regardless of if the field
13// might get moved into the closure.
14//
15// It is possible for someone to try writing the code that relies on the desugaring to access a ref
16// into a packed-struct without explicity using unsafe. Here we test that the compiler warns the
17// user that such an access is still unsafe.
18fn test_missing_unsafe_warning_on_repr_packed() {
19 #[repr(packed)]
20 struct Foo { x: String }
21
22 let foo = Foo { x: String::new() };
23
24 let c = || {
25 println!("{}", foo.x);
26 //~^ WARNING: borrow of packed field is unsafe and requires unsafe function or block
27 //~| WARNING: this was previously accepted by the compiler but is being phased out
28 let _z = foo.x;
29 };
30
31 c();
32}
33
34fn main() {
35 test_missing_unsafe_warning_on_repr_packed();
36}