-
Notifications
You must be signed in to change notification settings - Fork 14k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-repr-packedArea: the naughtiest reprArea: the naughtiest reprT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Code
#[repr(C, packed(4))]
pub struct Data {
oxide_magic: u32,
header_version: u32,
monotonic_counter: u64,
slot_select: u32,
}
fn main() {
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 erDesired 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
= ...etcRationale and extra context
It's confusing to see packed structs are only aligned by one byte when I have specified packed(4)
Rust Version
rustc 1.90.0 (1159e78c4 2025-09-14)
binary: rustc
commit-hash: 1159e78c4747b02ef996e55082b704c09b970588
commit-date: 2025-09-14
host: aarch64-apple-darwin
release: 1.90.0
LLVM version: 20.1.8Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-repr-packedArea: the naughtiest reprArea: the naughtiest reprT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.