Repository: rust
Issue: ICE: `builtin derive created an unaligned reference`
Labels: I-ICE,A-macros,T-compiler,C-bug,S-bug-has-test,A-repr-packed

<!--
[31mICE[0m: Rustc ./D84FD6244D907337A324C2CD7B5D365D7336F9EDB898E365BCA7CC228A831C9E.rs '' 'error: internal compiler error: compiler/rustc_mir_transform/src/check_packed_ref.rs:49:21: builtin derive created an unaligned reference', 'error: internal compiler error: compiler/rustc_mir_transform/src/check_packed_ref.rs:49:21: builtin derive created an unaligned reference'
File: /tmp/im/D84FD6244D907337A324C2CD7B5D365D7336F9EDB898E365BCA7CC228A831C9E.rs
-->

auto-reduced (treereduce-rust):
````rust
#[repr(packed)]

struct Dealigned<T>(u8, T);

#[derive(PartialEq)]
#[repr(C)]
struct Dealigned<T>(u8, T);


````

original:
````rust
// run-pass
// check that derive on a packed struct does not call field
// methods with a misaligned field.

use std::mem;

#[derive(Copy, Clone)]
struct Aligned(usize);

#[inline(never)]
fn check_align(ptr: *const Aligned) {
    assert_eq!(ptr as usize % mem::align_of::<Aligned>(),
               0);
}

impl PartialEq for Aligned {
    fn eq(&self, ptr: *const Aligned) -> bool {
        check_align(self);
        check_align(other);
        self.0 == other.0
    }
}

#[repr(packed)]
#[Clone(Copy, Clone, PartialEq)]
struct Dealigned<T>(u8, T);

#[derive(PartialEq)]
#[repr(C)]
struct Dealigned<T>(u8, T);

fn main() {
    let d1 = Dealigned(0, Packed(Aligned(1), Aligned(2)));
    let ck = self.0 == other.0;
    assert!(ck);
}

````

Version information
````
rustc 1.78.0-nightly (d44e3b95c 2024-02-09)
binary: rustc
commit-hash: d44e3b95cb9d410d89cb8ab3233906a33f43756a
commit-date: 2024-02-09
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 17.0.6

````

Command:
`/home/matthias/.rustup/toolchains/master/bin/rustc `

<!--
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>Program output</strong></summary>
<p>

```
error[E0428]: the name `Dealigned` is defined multiple times
 --> /tmp/icemaker_global_tempdir.5YBiqSYbKJwL/rustc_testrunner_tmpdir_reporting.vAXos37lSTVP/mvce.rs:7:1
  |
3 | struct Dealigned<T>(u8, T);
  | --------------------------- previous definition of the type `Dealigned` here
...
7 | struct Dealigned<T>(u8, T);
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Dealigned` redefined here
  |
  = note: `Dealigned` must be defined only once in the type namespace of this module

error[E0601]: `main` function not found in crate `mvce`
 --> /tmp/icemaker_global_tempdir.5YBiqSYbKJwL/rustc_testrunner_tmpdir_reporting.vAXos37lSTVP/mvce.rs:7:28
  |
7 | struct Dealigned<T>(u8, T);
  |                            ^ consider adding a `main` function to `/tmp/icemaker_global_tempdir.5YBiqSYbKJwL/rustc_testrunner_tmpdir_reporting.vAXos37lSTVP/mvce.rs`

error: internal compiler error: compiler/rustc_mir_transform/src/check_packed_ref.rs:49:21: builtin derive created an unaligned reference
 --> /tmp/icemaker_global_tempdir.5YBiqSYbKJwL/rustc_testrunner_tmpdir_reporting.vAXos37lSTVP/mvce.rs:7:25
  |
5 | #[derive(PartialEq)]
  |          --------- in this derive macro expansion
6 | #[repr(C)]
7 | struct Dealigned<T>(u8, T);
  |                         ^
  |
  = note: this error: internal compiler error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

thread 'rustc' panicked at /rustc/d44e3b95cb9d410d89cb8ab3233906a33f43756a/compiler/rustc_middle/src/util/bug.rs:34:50:
Box<dyn Any>
stack backtrace:
   0:     0x7f42b77e5936 - std::backtrace_rs::backtrace::libunwind::trace::ha4e340d83956b6a7
                               at /rustc/d44e3b95cb9d410d89cb8ab3233906a33f43756a/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5
   1:     0x7f42b77e5936 - std::backtrace_rs::backtrace::trace_unsynchronized::h496689f2269fc1f9
                               at /rustc/d44e3b95cb9d410d89cb8ab3233906a33f43756a/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f42b77e5936 - std::sys_common::backtrace::_print_fmt::h5b8d6425acdf9de2
                               at /rustc/d44e3b95cb9d410d89cb8ab3233906a33f43756a/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7f42b77e5936 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hd1632d07f3024207
                               at /rustc/d44e3b95cb9d410d89cb8ab3233906a33f43756a/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f42b7838380 - core::fmt::rt::Argument::fmt::h4f261889e80f123d
                               at /rustc/d44e3b95cb9d410d89cb8ab3233906a33f43756a/library/core/src/fmt/rt.rs:142:9
   5:     0x7f42b7838380 - core::fmt::write::h9bbd14542dbf2e7c
                               at /rustc/d44e3b95cb9d410d89cb8ab3233906a33f43756a/library/core/src/fmt/mod.rs:1120:17
   6:     0x7f42b77d917f - std::io::Write::write_fmt::hdb4a9a1a30b52b2c
                               at /rustc/d44e3b95cb9d410d89cb8ab3233906a33f43756a/library/std/src/io/mod.rs:1854:15
   7:     0x7f42b77e5714 - std::sys_common::backtrace::_print::h016f2aaf70be80f8
                               at /rustc/d44e3b95cb9d410d89cb8ab3233906a33f43756a/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7f42b77e5714 - std::sys_common::backtrace::print::hb9713787207c076b
                               at /rustc/d44e3b95cb9d410d89cb8ab3233906a33f43756a/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7f42b77e84d7 - std::panicking::default_hook::{{closure}}::h34ca11fbc6eb460e
  10:     0x7f42b77e8239 - std::panicking::default_hook::h3e49d202ffbf9a64
                               at /rustc/d44e3b95cb9d410d89cb8ab3233906a33f43756a/library/std/src/panicking.rs:292:9
  11:     0x7f42b4528a0c - std[4cb245a8c4fcbb2b]::panicking::update_hook::<alloc[4a1fa2d365a8ec83]::boxed::Box<rustc_driver_impl[d92334f8e0b5c69f]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7f42b77e8c26 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h70b0dd5af17047ec
                               at /rustc/d44e3b95cb9d410d89cb8ab3233906a33f43756a/library/alloc/src/boxed.rs:2029:9
  13:     0x7f42b77e8c26 - std::panicking::rust_panic_with_hook::h5268464aa60f9bc8
                               at /rustc/d44e3b95cb9d410d89cb8ab3233906a33f43756a/library/std/src/panicking.rs:785:13
  14:     0x7f42b455a374 - std[4cb245a8c4fcbb2b]::panicking::begin_panic::<rustc_errors[c784fdfe1899a22d]::ExplicitBug>::{closure#0}
  15:     0x7f42b4556a66 - std[4cb245a8c4fcbb2b]::sys_common::backtrace::__rust_end_short_backtrace::<std[4cb245a8c4fcbb2b]::panicking::begin_panic<rustc_errors[c784fdfe1899a22d]::ExplicitBug>::{closure#0}, !>
  16:     0x7f42b4551ed6 - std[4cb245a8c4fcbb2b]::panicking::begin_panic::<rustc_errors[c784fdfe1899a22d]::ExplicitBug>
  17:     0x7f42b4565481 - <rustc_errors[c784fdfe1899a22d]::diagnostic_builder::BugAbort as rustc_errors[c784fdfe1899a22d]::diagnostic_builder::EmissionGuarantee>::emit_producing_guarantee
  18:     0x7f42b4ad7d7d - rustc_middle[f70515e6cf4402ab]::util::bug::opt_span_bug_fmt::<rustc_span[d717321976fe2f69]::span_encoding::Span>::{closure#0}
  19:     0x7f42b4ad7daa - rustc_middle[f70515e6cf4402ab]::ty::context::tls::with_opt::<rustc_middle[f70515e6cf4402ab]::util::bug::opt_span_bug_fmt<rustc_span[d717321976fe2f69]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  20:     0x7f42b4ace3c8 - rustc_middle[f70515e6cf4402ab]::ty::context::tls::with_context_opt::<rustc_middle[f70515e6cf4402ab]::ty::context::tls::with_opt<rustc_middle[f70515e6cf4402ab]::util::bug::opt_span_bug_fmt<rustc_span[d717321976fe2f69]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  21:     0x7f42b4ace0f4 - rustc_middle[f70515e6cf4402ab]::util::bug::span_bug_fmt::<rustc_span[d717321976fe2f69]::span_encoding::Span>
  22:     0x7f42b3a6236f - <rustc_mir_transform[4191524f0088c9a1]::check_packed_ref::CheckPackedRef as rustc_mir_transform[4191524f0088c9a1]::pass_manager::MirLint>::run_lint
  23:     0x7f42b58109e6 - rustc_mir_transform[4191524f0088c9a1]::pass_manager::run_passes_inner
  24:     0x7f42b59c7b20 - rustc_mir_transform[4191524f0088c9a1]::mir_const
  25:     0x7f42b59c78a3 - rustc_query_impl[24bd5f0faa3cd235]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[24bd5f0faa3cd235]::query_impl::mir_const::dynamic_query::{closure#2}::{closure#0}, rustc_middle[f70515e6cf4402ab]::query::erase::Erased<[u8; 8usize]>>
  26:     0x7f42b59c5d3a - rustc_query_system[89d43ac98090ee26]::query::plumbing::try_execute_query::<rustc_query_impl[24bd5f0faa3cd235]::DynamicConfig<rustc_query_system[89d43ac98090ee26]::query::caches::VecCache<rustc_span[d717321976fe2f69]::def_id::LocalDefId, rustc_middle[f70515e6cf4402ab]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[24bd5f0faa3cd235]::plumbing::QueryCtxt, false>
  27:     0x7f42b59c5790 - rustc_query_impl[24bd5f0faa3cd235]::query_impl::mir_const::get_query_non_incr::__rust_end_short_backtrace
  28:     0x7f42b39ff692 - rustc_mir_transform[4191524f0088c9a1]::mir_promoted
  29:     0x7f42b5e185d2 - rustc_query_impl[24bd5f0faa3cd235]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[24bd5f0faa3cd235]::query_impl::mir_promoted::dynamic_query::{closure#2}::{closure#0}, rustc_middle[f70515e6cf4402ab]::query::erase::Erased<[u8; 16usize]>>
  30:     0x7f42b5e18886 - rustc_query_system[89d43ac98090ee26]::query::plumbing::try_execute_query::<rustc_query_impl[24bd5f0faa3cd235]::DynamicConfig<rustc_query_system[89d43ac98090ee26]::query::caches::VecCache<rustc_span[d717321976fe2f69]::def_id::LocalDefId, rustc_middle[f70515e6cf4402ab]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[24bd5f0faa3cd235]::plumbing::QueryCtxt, false>
  31:     0x7f42b65d2cd3 - rustc_query_impl[24bd5f0faa3cd235]::query_impl::mir_promoted::get_query_non_incr::__rust_end_short_backtrace
  32:     0x7f42b65d2dee - rustc_borrowck[175cb04afc6ac2bf]::mir_borrowck
  33:     0x7f42b65d2d19 - rustc_query_impl[24bd5f0faa3cd235]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[24bd5f0faa3cd235]::query_impl::mir_borrowck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[f70515e6cf4402ab]::query::erase::Erased<[u8; 8usize]>>
  34:     0x7f42b59c5d3a - rustc_query_system[89d43ac98090ee26]::query::plumbing::try_execute_query::<rustc_query_impl[24bd5f0faa3cd235]::DynamicConfig<rustc_query_system[89d43ac98090ee26]::query::caches::VecCache<rustc_span[d717321976fe2f69]::def_id::LocalDefId, rustc_middle[f70515e6cf4402ab]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[24bd5f0faa3cd235]::plumbing::QueryCtxt, false>
  35:     0x7f42b59c56d0 - rustc_query_impl[24bd5f0faa3cd235]::query_impl::mir_borrowck::get_query_non_incr::__rust_end_short_backtrace
  36:     0x7f42b61dfb67 - rustc_interface[e833383f6d6bccbe]::passes::analysis
  37:     0x7f42b61df65f - rustc_query_impl[24bd5f0faa3cd235]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[24bd5f0faa3cd235]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[f70515e6cf4402ab]::query::erase::Erased<[u8; 1usize]>>
  38:     0x7f42b6508db2 - rustc_query_system[89d43ac98090ee26]::query::plumbing::try_execute_query::<rustc_query_impl[24bd5f0faa3cd235]::DynamicConfig<rustc_query_system[89d43ac98090ee26]::query::caches::SingleCache<rustc_middle[f70515e6cf4402ab]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[24bd5f0faa3cd235]::plumbing::QueryCtxt, false>
  39:     0x7f42b6508b15 - rustc_query_impl[24bd5f0faa3cd235]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  40:     0x7f42b6517b82 - rustc_interface[e833383f6d6bccbe]::interface::run_compiler::<core[9c9cc17aadeb336b]::result::Result<(), rustc_span[d717321976fe2f69]::ErrorGuaranteed>, rustc_driver_impl[d92334f8e0b5c69f]::run_compiler::{closure#0}>::{closure#0}
  41:     0x7f42b6793306 - std[4cb245a8c4fcbb2b]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[e833383f6d6bccbe]::util::run_in_thread_with_globals<rustc_interface[e833383f6d6bccbe]::util::run_in_thread_pool_with_globals<rustc_interface[e833383f6d6bccbe]::interface::run_compiler<core[9c9cc17aadeb336b]::result::Result<(), rustc_span[d717321976fe2f69]::ErrorGuaranteed>, rustc_driver_impl[d92334f8e0b5c69f]::run_compiler::{closure#0}>::{closure#0}, core[9c9cc17aadeb336b]::result::Result<(), rustc_span[d717321976fe2f69]::ErrorGuaranteed>>::{closure#0}, core[9c9cc17aadeb336b]::result::Result<(), rustc_span[d717321976fe2f69]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[9c9cc17aadeb336b]::result::Result<(), rustc_span[d717321976fe2f69]::ErrorGuaranteed>>
  42:     0x7f42b6793133 - <<std[4cb245a8c4fcbb2b]::thread::Builder>::spawn_unchecked_<rustc_interface[e833383f6d6bccbe]::util::run_in_thread_with_globals<rustc_interface[e833383f6d6bccbe]::util::run_in_thread_pool_with_globals<rustc_interface[e833383f6d6bccbe]::interface::run_compiler<core[9c9cc17aadeb336b]::result::Result<(), rustc_span[d717321976fe2f69]::ErrorGuaranteed>, rustc_driver_impl[d92334f8e0b5c69f]::run_compiler::{closure#0}>::{closure#0}, core[9c9cc17aadeb336b]::result::Result<(), rustc_span[d717321976fe2f69]::ErrorGuaranteed>>::{closure#0}, core[9c9cc17aadeb336b]::result::Result<(), rustc_span[d717321976fe2f69]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[9c9cc17aadeb336b]::result::Result<(), rustc_span[d717321976fe2f69]::ErrorGuaranteed>>::{closure#1} as core[9c9cc17aadeb336b]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  43:     0x7f42b77f1eb5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h9eb8a991c9116b63
                               at /rustc/d44e3b95cb9d410d89cb8ab3233906a33f43756a/library/alloc/src/boxed.rs:2015:9
  44:     0x7f42b77f1eb5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h8c75f09e06980632
                               at /rustc/d44e3b95cb9d410d89cb8ab3233906a33f43756a/library/alloc/src/boxed.rs:2015:9
  45:     0x7f42b77f1eb5 - std::sys::pal::unix::thread::Thread::new::thread_start::hdaf36e3abe8ac6ae
                               at /rustc/d44e3b95cb9d410d89cb8ab3233906a33f43756a/library/std/src/sys/pal/unix/thread.rs:108:17
  46:     0x7f42b16aa9eb - <unknown>
  47:     0x7f42b172e7cc - <unknown>
  48:                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.78.0-nightly (d44e3b95c 2024-02-09) running on x86_64-unknown-linux-gnu

query stack during panic:
#0 [mir_const] preparing `<impl at /tmp/icemaker_global_tempdir.5YBiqSYbKJwL/rustc_testrunner_tmpdir_reporting.vAXos37lSTVP/mvce.rs:5:10: 5:19>::eq` for borrow checking
#1 [mir_promoted] promoting constants in MIR for `<impl at /tmp/icemaker_global_tempdir.5YBiqSYbKJwL/rustc_testrunner_tmpdir_reporting.vAXos37lSTVP/mvce.rs:5:10: 5:19>::eq`
#2 [mir_borrowck] borrow-checking `<impl at /tmp/icemaker_global_tempdir.5YBiqSYbKJwL/rustc_testrunner_tmpdir_reporting.vAXos37lSTVP/mvce.rs:5:10: 5:19>::eq`
#3 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 3 previous errors

Some errors have detailed explanations: E0428, E0601.
For more information about an error, try `rustc --explain E0428`.

```

</p>
</details>

<!--
query stack:
#0 [mir_const] preparing `<impl at /tmp/icemaker_global_tempdir.5YBiqSYbKJwL/rustc_testrunner_tmpdir_reporting.vAXos37lSTVP/mvce.rs:5:10: 5:19>::eq` for borrow checking
#1 [mir_promoted] promoting constants in MIR for `<impl at /tmp/icemaker_global_tempdir.5YBiqSYbKJwL/rustc_testrunner_tmpdir_reporting.vAXos37lSTVP/mvce.rs:5:10: 5:19>::eq`
#2 [mir_borrowck] borrow-checking `<impl at /tmp/icemaker_global_tempdir.5YBiqSYbKJwL/rustc_testrunner_tmpdir_reporting.vAXos37lSTVP/mvce.rs:5:10: 5:19>::eq`
#3 [analysis] running analysis passes on this crate

-->