Repository: rust-lang/rust (Rust)

Issue:
ICE: `failed to get layout for `{type error}`: ReferencesError(ErrorGuaranteed(()))`
<!--
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
`rustc treereduce.out  --crate-type lib  -Cdebuginfo=2`
```Rust
#![recursion_limit = "10"]
macro_rules! link {
    ($outer:ident, $inner:ident) => {
        struct $outer($inner);
        impl $outer {
            fn new() -> $outer {
                $outer($inner::new())
            }
        }
        impl std::ops::Deref for $outer {
            type Target = $inner;
            fn deref(&self) -> &$inner {
                &self.0
            }
        }
    };
}

struct Bottom;

impl Bottom {
    fn new() -> Bottom {
        Bottom
    }
}


link!(A, B);
link!(B, C);
link!(C, D);
link!(D, E);
link!(E, F);
link!(F, G);
link!(G, H);
link!(H, I);
link!(I, J);
link!(J, K);
link!(K, Bottom);
```


### 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.73.0-nightly (8131b9774 2023-08-02)
binary: rustc
commit-hash: 8131b9774ebcb6c162fcac71545a13543ec369e7
commit-date: 2023-08-02
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5
```

### Error output

```
warning: associated function `new` is never used
  --> treereduce.out:22:8
   |
21 | impl Bottom {
   | ----------- associated function in this implementation
22 |     fn new() -> Bottom {
   |        ^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: associated function `new` is never used
  --> treereduce.out:6:16
   |
5  |         impl $outer {
   |         ----------- associated function in this implementation
6  |             fn new() -> $outer {
   |                ^^^
...
28 | link!(A, B);
   | ----------- in this macro invocation
   |
   = note: this warning originates in the macro `link` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: associated function `new` is never used
  --> treereduce.out:6:16
   |
5  |         impl $outer {
   |         ----------- associated function in this implementation
6  |             fn new() -> $outer {
   |                ^^^
...
29 | link!(B, C);
   | ----------- in this macro invocation
   |
   = note: this warning originates in the macro `link` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: associated function `new` is never used
  --> treereduce.out:6:16
   |
5  |         impl $outer {
   |         ----------- associated function in this implementation
6  |             fn new() -> $outer {
   |                ^^^
...
30 | link!(C, D);
   | ----------- in this macro invocation
   |
   = note: this warning originates in the macro `link` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: associated function `new` is never used
  --> treereduce.out:6:16
   |
5  |         impl $outer {
   |         ----------- associated function in this implementation
6  |             fn new() -> $outer {
   |                ^^^
...
31 | link!(D, E);
   | ----------- in this macro invocation
   |
   = note: this warning originates in the macro `link` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: associated function `new` is never used
  --> treereduce.out:6:16
   |
5  |         impl $outer {
   |         ----------- associated function in this implementation
6  |             fn new() -> $outer {
   |                ^^^
...
32 | link!(E, F);
   | ----------- in this macro invocation
   |
   = note: this warning originates in the macro `link` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: associated function `new` is never used
  --> treereduce.out:6:16
   |
5  |         impl $outer {
   |         ----------- associated function in this implementation
6  |             fn new() -> $outer {
   |                ^^^
...
33 | link!(F, G);
   | ----------- in this macro invocation
   |
   = note: this warning originates in the macro `link` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: associated function `new` is never used
  --> treereduce.out:6:16
   |
5  |         impl $outer {
   |         ----------- associated function in this implementation
6  |             fn new() -> $outer {
   |                ^^^
...
34 | link!(G, H);
   | ----------- in this macro invocation
   |
   = note: this warning originates in the macro `link` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: associated function `new` is never used
  --> treereduce.out:6:16
   |
5  |         impl $outer {
   |         ----------- associated function in this implementation
6  |             fn new() -> $outer {
   |                ^^^
...
35 | link!(H, I);
   | ----------- in this macro invocation
   |
   = note: this warning originates in the macro `link` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: associated function `new` is never used
  --> treereduce.out:6:16
   |
5  |         impl $outer {
   |         ----------- associated function in this implementation
6  |             fn new() -> $outer {
   |                ^^^
...
36 | link!(I, J);
   | ----------- in this macro invocation
   |
   = note: this warning originates in the macro `link` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: associated function `new` is never used
  --> treereduce.out:6:16
   |
5  |         impl $outer {
   |         ----------- associated function in this implementation
6  |             fn new() -> $outer {
   |                ^^^
...
37 | link!(J, K);
   | ----------- in this macro invocation
   |
   = note: this warning originates in the macro `link` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: associated function `new` is never used
  --> treereduce.out:6:16
   |
5  |         impl $outer {
   |         ----------- associated function in this implementation
6  |             fn new() -> $outer {
   |                ^^^
...
38 | link!(K, Bottom);
   | ---------------- in this macro invocation
   |
   = note: this warning originates in the macro `link` (in Nightly builds, run with -Z macro-backtrace for more info)

error: reached the recursion limit finding the struct tail for `Bottom`
  |
  = help: consider increasing the recursion limit by adding a `#![recursion_limit = "20"]`
```

<!--
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: reached the recursion limit finding the struct tail for `Bottom`
  |
  = help: consider increasing the recursion limit by adding a `#![recursion_limit = "20"]`

error: internal compiler error: compiler/rustc_codegen_llvm/src/context.rs:991:13: failed to get layout for `{type error}`: ReferencesError(ErrorGuaranteed(()))

thread 'rustc' panicked at /rustc/8131b9774ebcb6c162fcac71545a13543ec369e7/compiler/rustc_errors/src/lib.rs:989:33:
Box<dyn Any>
stack backtrace:
   0:     0x7faec41bc19c - std::backtrace_rs::backtrace::libunwind::trace::h63bb23e9dd9d0f13
                               at /rustc/8131b9774ebcb6c162fcac71545a13543ec369e7/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7faec41bc19c - std::backtrace_rs::backtrace::trace_unsynchronized::h15eabfca85a40e74
                               at /rustc/8131b9774ebcb6c162fcac71545a13543ec369e7/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7faec41bc19c - std::sys_common::backtrace::_print_fmt::hf0c7b8002dbb638c
                               at /rustc/8131b9774ebcb6c162fcac71545a13543ec369e7/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x7faec41bc19c - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hf11a95c6d431836f
                               at /rustc/8131b9774ebcb6c162fcac71545a13543ec369e7/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7faec422289c - core::fmt::rt::Argument::fmt::h15de052cc2ee80f2
                               at /rustc/8131b9774ebcb6c162fcac71545a13543ec369e7/library/core/src/fmt/rt.rs:138:9
   5:     0x7faec422289c - core::fmt::write::h297aea74590c27b8
                               at /rustc/8131b9774ebcb6c162fcac71545a13543ec369e7/library/core/src/fmt/mod.rs:1094:21
   6:     0x7faec41ae90e - std::io::Write::write_fmt::hdd9207bc895694ae
                               at /rustc/8131b9774ebcb6c162fcac71545a13543ec369e7/library/std/src/io/mod.rs:1714:15
   7:     0x7faec41bbf85 - std::sys_common::backtrace::_print::hf5af2b088c0bb2dc
                               at /rustc/8131b9774ebcb6c162fcac71545a13543ec369e7/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7faec41bbf85 - std::sys_common::backtrace::print::ha8ca06c086ae5d11
                               at /rustc/8131b9774ebcb6c162fcac71545a13543ec369e7/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7faec41bf0da - std::panicking::panic_hook_with_disk_dump::{{closure}}::h87259dc27362a87b
                               at /rustc/8131b9774ebcb6c162fcac71545a13543ec369e7/library/std/src/panicking.rs:278:22
  10:     0x7faec41bed73 - std::panicking::panic_hook_with_disk_dump::h213c181176413b01
                               at /rustc/8131b9774ebcb6c162fcac71545a13543ec369e7/library/std/src/panicking.rs:312:9
  11:     0x7faec3019669 - <rustc_driver_impl[48833e2cf35ff341]::install_ice_hook::{closure#0} as core[d518b4b6aca24807]::ops::function::FnOnce<(&core[d518b4b6aca24807]::panic::panic_info::PanicInfo,)>>::call_once::{shim:vtable#0}
  12:     0x7faec41bf980 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hf02ba24503119f1e
                               at /rustc/8131b9774ebcb6c162fcac71545a13543ec369e7/library/alloc/src/boxed.rs:2021:9
  13:     0x7faec41bf980 - std::panicking::rust_panic_with_hook::he3f35d9a4da09197
                               at /rustc/8131b9774ebcb6c162fcac71545a13543ec369e7/library/std/src/panicking.rs:733:13
  14:     0x7faec2ed5fc1 - std[bb7e30dda1331a7f]::panicking::begin_panic::<rustc_errors[d2305626044d95ac]::ExplicitBug>::{closure#0}
  15:     0x7faec2ed4ce6 - std[bb7e30dda1331a7f]::sys_common::backtrace::__rust_end_short_backtrace::<std[bb7e30dda1331a7f]::panicking::begin_panic<rustc_errors[d2305626044d95ac]::ExplicitBug>::{closure#0}, !>
  16:     0x7faec2e88856 - std[bb7e30dda1331a7f]::panicking::begin_panic::<rustc_errors[d2305626044d95ac]::ExplicitBug>
  17:     0x7faec2e7ccce - <rustc_errors[d2305626044d95ac]::HandlerInner>::span_bug::<rustc_span[313c88adb0f8cb98]::span_encoding::Span, alloc[aad66170b1e28d8f]::string::String>
  18:     0x7faec2e7cadf - <rustc_errors[d2305626044d95ac]::Handler>::span_bug::<rustc_span[313c88adb0f8cb98]::span_encoding::Span, alloc[aad66170b1e28d8f]::string::String>
  19:     0x7faec2eb691d - rustc_middle[285106c4fd623be0]::util::bug::opt_span_bug_fmt::<rustc_span[313c88adb0f8cb98]::span_encoding::Span>::{closure#0}
  20:     0x7faec2eb694a - rustc_middle[285106c4fd623be0]::ty::context::tls::with_opt::<rustc_middle[285106c4fd623be0]::util::bug::opt_span_bug_fmt<rustc_span[313c88adb0f8cb98]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  21:     0x7faec2eb6038 - rustc_middle[285106c4fd623be0]::ty::context::tls::with_context_opt::<rustc_middle[285106c4fd623be0]::ty::context::tls::with_opt<rustc_middle[285106c4fd623be0]::util::bug::opt_span_bug_fmt<rustc_span[313c88adb0f8cb98]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  22:     0x7faec1a6e0c4 - rustc_middle[285106c4fd623be0]::util::bug::span_bug_fmt::<rustc_span[313c88adb0f8cb98]::span_encoding::Span>
  23:     0x7faec2eb7f1b - <rustc_codegen_llvm[29ef7bee937c9d23]::context::CodegenCx as rustc_middle[285106c4fd623be0]::ty::layout::LayoutOfHelpers>::handle_layout_err
  24:     0x7faec2eb6d3a - <rustc_codegen_llvm[29ef7bee937c9d23]::context::CodegenCx as rustc_middle[285106c4fd623be0]::ty::layout::LayoutOf>::spanned_layout_of::{closure#0}
  25:     0x7faec171ab3c - rustc_codegen_llvm[29ef7bee937c9d23]::debuginfo::utils::fat_pointer_kind
  26:     0x7faec1709081 - rustc_codegen_llvm[29ef7bee937c9d23]::debuginfo::metadata::type_di_node
  27:     0x7faec1700a70 - <rustc_codegen_llvm[29ef7bee937c9d23]::context::CodegenCx as rustc_codegen_ssa[648ad86e70a776a4]::traits::debuginfo::DebugInfoMethods>::dbg_scope_fn
  28:     0x7faec1a4c09e - rustc_codegen_ssa[648ad86e70a776a4]::mir::codegen_mir::<rustc_codegen_llvm[29ef7bee937c9d23]::builder::Builder>
  29:     0x7faec26636c0 - rustc_codegen_llvm[29ef7bee937c9d23]::base::compile_codegen_unit::module_codegen
  30:     0x7faec26618c8 - rustc_codegen_llvm[29ef7bee937c9d23]::base::compile_codegen_unit
  31:     0x7faec1f05602 - rustc_codegen_ssa[648ad86e70a776a4]::base::codegen_crate::<rustc_codegen_llvm[29ef7bee937c9d23]::LlvmCodegenBackend>
  32:     0x7faec1f05016 - <rustc_codegen_llvm[29ef7bee937c9d23]::LlvmCodegenBackend as rustc_codegen_ssa[648ad86e70a776a4]::traits::backend::CodegenBackend>::codegen_crate
  33:     0x7faec226c652 - <rustc_session[da4329df406289b6]::session::Session>::time::<alloc[aad66170b1e28d8f]::boxed::Box<dyn core[d518b4b6aca24807]::any::Any>, rustc_interface[f068b61f52e6f5de]::passes::start_codegen::{closure#0}>
  34:     0x7faec226c19b - rustc_interface[f068b61f52e6f5de]::passes::start_codegen
  35:     0x7faec22667aa - <rustc_middle[285106c4fd623be0]::ty::context::GlobalCtxt>::enter::<<rustc_interface[f068b61f52e6f5de]::queries::Queries>::ongoing_codegen::{closure#0}, core[d518b4b6aca24807]::result::Result<alloc[aad66170b1e28d8f]::boxed::Box<dyn core[d518b4b6aca24807]::any::Any>, rustc_span[313c88adb0f8cb98]::ErrorGuaranteed>>
  36:     0x7faec22657e3 - <rustc_interface[f068b61f52e6f5de]::interface::Compiler>::enter::<rustc_driver_impl[48833e2cf35ff341]::run_compiler::{closure#1}::{closure#2}, core[d518b4b6aca24807]::result::Result<core[d518b4b6aca24807]::option::Option<rustc_interface[f068b61f52e6f5de]::queries::Linker>, rustc_span[313c88adb0f8cb98]::ErrorGuaranteed>>
  37:     0x7faec225e858 - std[bb7e30dda1331a7f]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[f068b61f52e6f5de]::util::run_in_thread_pool_with_globals<rustc_interface[f068b61f52e6f5de]::interface::run_compiler<core[d518b4b6aca24807]::result::Result<(), rustc_span[313c88adb0f8cb98]::ErrorGuaranteed>, rustc_driver_impl[48833e2cf35ff341]::run_compiler::{closure#1}>::{closure#0}, core[d518b4b6aca24807]::result::Result<(), rustc_span[313c88adb0f8cb98]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[d518b4b6aca24807]::result::Result<(), rustc_span[313c88adb0f8cb98]::ErrorGuaranteed>>
  38:     0x7faec225dfde - <<std[bb7e30dda1331a7f]::thread::Builder>::spawn_unchecked_<rustc_interface[f068b61f52e6f5de]::util::run_in_thread_pool_with_globals<rustc_interface[f068b61f52e6f5de]::interface::run_compiler<core[d518b4b6aca24807]::result::Result<(), rustc_span[313c88adb0f8cb98]::ErrorGuaranteed>, rustc_driver_impl[48833e2cf35ff341]::run_compiler::{closure#1}>::{closure#0}, core[d518b4b6aca24807]::result::Result<(), rustc_span[313c88adb0f8cb98]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[d518b4b6aca24807]::result::Result<(), rustc_span[313c88adb0f8cb98]::ErrorGuaranteed>>::{closure#1} as core[d518b4b6aca24807]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  39:     0x7faec41ca125 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h4bfeeb62bde7cabd
                               at /rustc/8131b9774ebcb6c162fcac71545a13543ec369e7/library/alloc/src/boxed.rs:2007:9
  40:     0x7faec41ca125 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h2e1d2bae8df88d22
                               at /rustc/8131b9774ebcb6c162fcac71545a13543ec369e7/library/alloc/src/boxed.rs:2007:9
  41:     0x7faec41ca125 - std::sys::unix::thread::Thread::new::thread_start::h11377e8ae9be0f28
                               at /rustc/8131b9774ebcb6c162fcac71545a13543ec369e7/library/std/src/sys/unix/thread.rs:108:17
  42:     0x7faebfc5544b - <unknown>
  43:     0x7faebfcd8e40 - <unknown>
  44:                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: please attach the file at `/tmp/im/rustc-ice-2023-08-03T21:07:26.289831244Z-1107975.txt` to your bug report

note: compiler flags: --crate-type lib -C debuginfo=2

query stack during panic:
end of query stack
error: aborting due to 2 previous errors; 12 warnings emitted
```

</p>
</details>



Discussion: nightly-2022-04-03

  commit[0] 2022-04-01: Auto merge of #94883 - cjgillot:flat-metadata, r=oli-obk
  commit[1] 2022-04-02: Auto merge of #95578 - RalfJung:miri, r=RalfJung
  commit[2] 2022-04-02: Auto merge of #95581 - Dylan-DPC:rollup-2suh5h1, r=Dylan-DPC
  commit[3] 2022-04-02: Auto merge of #95509 - nnethercote:simplify-MatcherPos-some-more, r=petrochenkov
  commit[4] 2022-04-02: Auto merge of #95571 - petrochenkov:nowrapident2, r=Aaron1011
  commit[5] 2022-04-02: Auto merge of #95537 - GuillaumeGomez:type_of-doc, r=Dylan-DPC
  commit[6] 2022-04-02: Auto merge of #95568 - GuillaumeGomez:fix-invalid-dom-generation, r=notriddle
  commit[7] 2022-04-02: Auto merge of #94911 - jackh726:gats_extended_2, r=compiler-errors
  commit[8] 2022-04-02: Auto merge of #95600 - Dylan-DPC:rollup-580y2ra, r=Dylan-DPC
Just needs `ReferencesError` added to `handle_layout_error` where that ICE occurs.



Fix (patch):
diff --git a/compiler/rustc_codegen_cranelift/src/common.rs b/compiler/rustc_codegen_cranelift/src/common.rs
index 67ea20112fe33..3081dcfa2b7a3 100644
--- a/compiler/rustc_codegen_cranelift/src/common.rs
+++ b/compiler/rustc_codegen_cranelift/src/common.rs
@@ -477,7 +477,7 @@ impl<'tcx> LayoutOfHelpers<'tcx> for RevealAllLayoutCx<'tcx> {
 
     #[inline]
     fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
-        if let layout::LayoutError::SizeOverflow(_) = err {
+        if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
             self.0.sess.span_fatal(span, err.to_string())
         } else {
             span_bug!(span, "failed to get layout for `{}`: {}", ty, err)
diff --git a/compiler/rustc_codegen_gcc/src/context.rs b/compiler/rustc_codegen_gcc/src/context.rs
index 08507e19652b4..88dcafa7370e5 100644
--- a/compiler/rustc_codegen_gcc/src/context.rs
+++ b/compiler/rustc_codegen_gcc/src/context.rs
@@ -476,7 +476,7 @@ impl<'gcc, 'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
 
     #[inline]
     fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
-        if let LayoutError::SizeOverflow(_) = err {
+        if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
             self.sess().emit_fatal(respan(span, err.into_diagnostic()))
         } else {
             span_bug!(span, "failed to get layout for `{}`: {}", ty, err)
diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs
index cb093996d1d09..3577fb2d951ba 100644
--- a/compiler/rustc_codegen_llvm/src/context.rs
+++ b/compiler/rustc_codegen_llvm/src/context.rs
@@ -985,7 +985,7 @@ impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
 
     #[inline]
     fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
-        if let LayoutError::SizeOverflow(_) = err {
+        if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
             self.sess().emit_fatal(Spanned { span, node: err.into_diagnostic() })
         } else {
             span_bug!(span, "failed to get layout for `{ty}`: {err:?}")