Repository: rust
Issue: #[derive(Debug)] on #[repr(packed)] enum causes internal compiler error
Labels: I-ICE,A-macros,T-compiler,C-bug,E-needs-bisection,A-repr-packed

<!--
Thank you for finding an Internal Compiler Error! 🧊  If possible, try to provide
a minimal verifiable example. You can read "Rust Bug Minimization Patterns" for
how to create smaller examples.
http://blog.pnkfx.org/blog/2019/11/18/rust-bug-minimization-patterns/
-->

### Code

```Rust
#[derive(Debug)]
#[repr(packed)]
enum COption<T> {
    None,
    Some(T),
}

fn main() {
}
```


### Meta
<!--
If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions.
-->

`rustc --version --verbose`:
```
rustc 1.82.0 (f6e511eec 2024-10-15) (Fedora 1.82.0-1.fc40)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-unknown-linux-gnu
release: 1.82.0
LLVM version: 18.1.8
```

### Error output

```
error: internal compiler error: compiler/rustc_mir_transform/src/check_packed_ref.rs:49:21: builtin derive created an unaligned reference
 --> src/main.rs:5:10
  |
1 | #[derive(Debug)]
  |          ----- in this derive macro expansion
...
5 |     Some(T),
  |          ^
  |
  = note: this error: internal compiler error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)

thread 'rustc' panicked at compiler/rustc_mir_transform/src/check_packed_ref.rs:49:21:
Box<dyn Any>
stack backtrace:
   0:     0x7fd20cb85b78 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h1ceeeb1b55e290c2
   1:     0x7fd20c582b9b - core::fmt::write::hb3a48913f78b0dae
   2:     0x7fd20cb78ba3 - <unknown>
   3:     0x7fd20cb887ea - <unknown>
   4:     0x7fd20cb8842c - std::panicking::default_hook::h49089136d7ad7532
   5:     0x7fd20a1425ad - <unknown>
   6:     0x7fd20cb891e6 - std::panicking::rust_panic_with_hook::ha43241025fb228f0
   7:     0x7fd20a18c221 - <unknown>
   8:     0x7fd20a17c246 - <unknown>
   9:     0x7fd20a17bfc6 - <unknown>
  10:     0x7fd20a19a8e1 - <rustc_errors[9a66caa3f8a65bb2]::diagnostic::BugAbort as rustc_errors[9a66caa3f8a65bb2]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  11:     0x7fd20a92c36d - <unknown>
  12:     0x7fd20a96b188 - <unknown>
  13:     0x7fd20a96bada - <unknown>
  14:     0x7fd20a9570db - <unknown>
  15:     0x7fd20a94dd57 - <unknown>
  16:     0x7fd20be2d539 - <unknown>
  17:     0x7fd20bf150eb - rustc_mir_transform[ec94f2ac466ce048]::mir_built
  18:     0x7fd20c1c6467 - <unknown>
  19:     0x7fd20ac4e640 - <unknown>
  20:     0x7fd20ac3cb28 - <unknown>
  21:     0x7fd20ad4ed98 - <unknown>
  22:     0x7fd20bd63e17 - rustc_mir_build[b87ccf4b6ee18353]::check_unsafety::check_unsafety
  23:     0x7fd20c1b87f7 - <unknown>
  24:     0x7fd20ac4dd90 - <unknown>
  25:     0x7fd20ac3714e - <unknown>
  26:     0x7fd20ad5d45f - <unknown>
  27:     0x7fd20baa8cbb - <unknown>
  28:     0x7fd20c8bd509 - rustc_interface[73b4cfe22fadb0e6]::passes::analysis
  29:     0x7fd20ca728b7 - <unknown>
  30:     0x7fd20ac4d70f - <unknown>
  31:     0x7fd20ac06129 - <unknown>
  32:     0x7fd20ad49194 - <unknown>
  33:     0x7fd20c7b0ee4 - <unknown>
  34:     0x7fd20c7ad945 - <unknown>
  35:     0x7fd20c7b3dd0 - <unknown>
  36:     0x7fd20cb952ab - <unknown>
  37:     0x7fd2094a66d7 - start_thread
  38:     0x7fd20952a60c - __clone3
  39:                0x0 - <unknown>

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.82.0 (f6e511eec 2024-10-15) (Fedora 1.82.0-1.fc40) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [mir_built] building MIR for `<impl at src/main.rs:1:10: 1:15>::fmt`
#1 [check_unsafety] unsafety-checking `<impl at src/main.rs:1:10: 1:15>::fmt`
end of query stack
```

<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary><strong>Backtrace</strong></summary>
<p>

```
error: internal compiler error: compiler/rustc_mir_transform/src/check_packed_ref.rs:49:21: builtin derive created an unaligned reference
 --> src/main.rs:5:10
  |
1 | #[derive(Debug)]
  |          ----- in this derive macro expansion
...
5 |     Some(T),
  |          ^
  |
  = note: this error: internal compiler error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)

thread 'rustc' panicked at compiler/rustc_mir_transform/src/check_packed_ref.rs:49:21:
Box<dyn Any>
stack backtrace:
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.82.0 (f6e511eec 2024-10-15) (Fedora 1.82.0-1.fc40) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [mir_built] building MIR for `<impl at src/main.rs:1:10: 1:15>::fmt`
#1 [check_unsafety] unsafety-checking `<impl at src/main.rs:1:10: 1:15>::fmt`
#2 [analysis] running analysis passes on this crate
end of query stack
```

</p>
</details>