Skip to content

Confusing error message with unaligned access to packed(n > 1) fields #147528

Closed

Description

@mkeeter

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 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)

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.8

Activity

  1. added
    A-diagnostics
    T-compiler
    on Oct 9, 2025
  2. 21aslade commented on Oct 15, 2025

    @21aslade
    Contributor

    I'd like to work on this! (My first issue, seems simple enough.)

    @rustbot claim

  3. added
    A-repr-packed
    on Oct 16, 2025
  4. added 3 commits that reference this issue on Nov 8, 2025
    7354146
    9e1d2c6
    025ebb0
  5. added a commit that references this issue on Nov 9, 2025
    068e784
  6. added a commit that references this issue on Nov 9, 2025
    4ddaa5f
  7. added a commit that references this issue on Nov 10, 2025
    4a1fcf2
  8. added 3 commits that reference this issue on Nov 10, 2025
    698abb4
    221d79a
    76ed1d9
  9. added a commit that references this issue on Dec 29, 2025
    0d9f104
  10. added a commit that references this issue on Mar 27, 2026
    14b3233
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsA-repr-packedT-compiler

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions