]> git.lizzy.rs Git - rust.git/blob - tests/ui/closures/2229_closure_analysis/diagnostics/repr_packed.rs
make unaligned_reference a hard error
[rust.git] / tests / ui / closures / 2229_closure_analysis / diagnostics / repr_packed.rs
1 // edition:2021
2
3 // Given how the closure desugaring is implemented (at least at the time of writing this test),
4 // we don't need to truncate the captured path to a reference into a packed-struct if the field
5 // being referenced will be moved into the closure, since it's safe to move out a field from a
6 // packed-struct.
7 //
8 // However to avoid surprises for the user, or issues when the closure is
9 // inlined we will truncate the capture to access just the struct regardless of if the field
10 // might get moved into the closure.
11 //
12 // It is possible for someone to try writing the code that relies on the desugaring to create a ref
13 // into a packed-struct. Here we test that the compiler still detects that case.
14 fn test_missing_unsafe_warning_on_repr_packed() {
15     #[repr(packed)]
16     struct Foo { x: String }
17
18     let foo = Foo { x: String::new() };
19
20     let c = || {
21         println!("{}", foo.x);
22         //~^ ERROR: reference to packed field is unaligned
23         let _z = foo.x;
24     };
25
26     c();
27 }
28
29 fn main() {
30     test_missing_unsafe_warning_on_repr_packed();
31 }