You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// run-pass#![allow(dead_code)]#[repr(packed)]pubstructGood{data:&'staticu32,data2:[&'staticu32;2],aligned:[u8;32],}// kill this test when that turns to a hard error#[allow(unaligned_references)]fnmain(){let good = Good{data:&0,data2:[&0,&0],aligned:[0;32]};let _ = &good.data;// oklet _ = &good.data2[0];// oklet _ = &good.data;let _ = &good.data2[0];let _ = &*good.data;// ok, behind a pointerlet _ = &good.aligned;// ok, has align 1let _ = &good.aligned[2];// ok, has align 1}
error: Undefined Behavior: type validation failed: encountered an unaligned reference (required 8 byte alignment but found 2)
--> src/main.rs:15:13
|
15 | let _ = &good.data; // ok
| ^^^^^^^^^^ type validation failed: encountered an unaligned reference (required 8 byte alignment but found 2)
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
This might be intentional. The original file is a test which seems to be related to #27060 (which has moved to #82523), which tracks the deprecation of unsound references to repr(packed) fields. Given the presence of #[allow(unaligned_references)], this appears to be a regression test to ensure that this code doesn't break before unaligned_references becomes a hard error.
Indeed this is not a bug. You allowed the unaligned_references lint which would have told you that there is a soundness issue in this code. :)
warning: reference to packed field is unaligned
[--> src/main.rs:16:13
](https://play.rust-lang.org/#) |
16 | let _ = &good.data;
| ^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, [see issue #82523 <https://github.com/rust-lang/rust/issues/82523>](https://github.com/rust-lang/rust/issues/82523)
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
I tried this code:
The text was updated successfully, but these errors were encountered: