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
{{ message }}
Repository navigation
Confusing error message with unaligned access to packed(n > 1) fields #147528
A-diagnosticsArea: Messages for errors, warnings, and lintsA-repr-packedArea: the naughtiest reprT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
#[repr(C, packed(4))]pubstructData{oxide_magic:u32,header_version:u32,monotonic_counter:u64,slot_select:u32,}fnmain(){let d = Data{oxide_magic:1,header_version:1,monotonic_counter:4,slot_select:15,};println!("{}", d.monotonic_counter);}
Current output
error[E0793]: reference to packed field is unaligned
--> src/main.rs:16:20
|
16 | println!("{}", d.monotonic_counter);| ^^^^^^^^^^^^^^^^^^^
|
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
= note: 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)
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0793`.
error: could not compile `playground` (bin "playground") due to 1 previous er
Desired output
Something along the lines of
= note: this struct is aligned to 4 bytes, but the variable requires 8-byte alignment
= note: many modern architectures penalize unaligned field accesses
= ...etc
Rationale and extra context
It's confusing to see packed structs are only aligned by one byte when I have specified packed(4)
A-diagnosticsArea: Messages for errors, warnings, and lintsA-repr-packedArea: the naughtiest reprT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
Activity
21aslade commentedon Oct 15, 2025
I'd like to work on this! (My first issue, seems simple enough.)
@rustbot claim
Rollup merge of rust-lang#147743 - 21aslade:packed-diagnostic, r=Ralf…
Rollup merge of rust-lang#147743 - 21aslade:packed-diagnostic, r=Ralf…
Rollup merge of rust-lang#147743 - 21aslade:packed-diagnostic, r=Ralf…
Rollup merge of rust-lang#147743 - 21aslade:packed-diagnostic, r=Ralf…
Unrolled build for #147743
Rollup merge of #147743 - 21aslade:packed-diagnostic, r=RalfJung
Rollup merge of #147743 - 21aslade:packed-diagnostic, r=RalfJung
Rollup merge of rust-lang#147743 - 21aslade:packed-diagnostic, r=Ralf…
Rollup merge of #147743 - 21aslade:packed-diagnostic, r=RalfJung
Rollup merge of #147743 - 21aslade:packed-diagnostic, r=RalfJung
Rollup merge of #147743 - 21aslade:packed-diagnostic, r=RalfJung