Repository: rust
Issue: Infinite loop in type inference
Labels: A-trait-system,T-compiler,C-bug,I-hang

I tried this code:

```rust
struct F<A, O>(fn(A) -> O);

trait Fun {
    type O;
    type A;
}

impl<A, O> Fun for F<A, O> {
    type O = O;
    type A = A;
}

trait Gen {
    type B;
}

fn a() {
    b(F(c))
}

fn b<G, B>(_: G)
where
    G: Fun<A = B>,
    <G as Fun>::O: Gen<B = B>,
{
}

fn c<T>(_: T) -> impl Gen {}
```
[Rust Explorer](https://www.rustexplorer.com/b#c3RydWN0IEY8QSwgTz4oZm4oQSkgLT4gTyk7Cgp0cmFpdCBGdW4gewogICAgdHlwZSBPOwogICAgdHlwZSBBOwp9CgppbXBsPEEsIE8+IEZ1biBmb3IgRjxBLCBPPiB7CiAgICB0eXBlIE8gPSBPOwogICAgdHlwZSBBID0gQTsKfQoKdHJhaXQgR2VuIHsKICAgIHR5cGUgQjsKfQoKZm4gYSgpIHsKICAgIGIoRihjKSkKfQoKZm4gYjxHLCBCPihfOiBHKQp3aGVyZQogICAgRzogRnVuPEEgPSBCPiwKICAgIDxHIGFzIEZ1bj46Ok86IEdlbjxCID0gQj4sCnsKfQoKZm4gYzxUPihfOiBUKSAtPiBpbXBsIEdlbiB7fQ==)

I expected to see this happen: This code should fail to compile with errors.

Instead, this happened: Rustc enters a infinite loop and uses memory until the user or OS kills it.

### Meta

`rustc --version --verbose`:
```
rustc 1.81.0-nightly (8fcd4dd08 2024-06-18)
binary: rustc
commit-hash: 8fcd4dd08e2ba3e922d917d819ba0be066bdb005
commit-date: 2024-06-18
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7
```

`cargo bisect-rustc`
```
Regression in nightly-2023-12-06
```

The following backtrace was generated by attaching to a running rustc instance and dumping the call stack as it was when the debugger was attached.
<details><summary>Backtrace</summary>
<p>

```
Thread 3 (Thread 0x7f3869fff6c0 (LWP 14473) "rustc"):
#0  0x00007f3873eee2a5 in <rustc_middle[fecc9a0d5ad25b76]::ty::context::CtxtInterners>::intern_ty () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#1  0x00007f38778b7207 in <rustc_infer[87ef6f2195437645]::infer::InferCtxt>::next_ty_var_in_universe () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#2  0x00007f387746420e in <rustc_infer[87ef6f2195437645]::infer::relate::generalize::Generalizer as rustc_type_ir[596bd8e1208b8c9]::relate::TypeRelation<rustc_middle[fecc9a0d5ad25b76]::ty::context::TyCtxt>>::tys::{closure#0} () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#3  0x00007f3877454d3d in <rustc_infer[87ef6f2195437645]::infer::relate::type_relating::TypeRelating as rustc_type_ir[596bd8e1208b8c9]::relate::TypeRelation<rustc_middle[fecc9a0d5ad25b76]::ty::context::TyCtxt>>::tys () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#4  0x00007f38740a7e71 in <rustc_trait_selection[32fdd2c471292ab4]::traits::fulfill::FulfillProcessor as rustc_data_structures[4cb81b6577327383]::obligation_forest::ObligationProcessor>::process_obligation () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#5  0x00007f3877408b1f in <rustc_data_structures[4cb81b6577327383]::obligation_forest::ObligationForest<rustc_trait_selection[32fdd2c471292ab4]::traits::fulfill::PendingPredicateObligation>>::process_obligations::<rustc_trait_selection[32fdd2c471292ab4]::traits::fulfill::FulfillProcessor> () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#6  0x00007f38743d03d0 in <rustc_hir_typeck[a8fa972dab75dcd1]::fn_ctxt::FnCtxt>::confirm_builtin_call () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#7  0x00007f387803bc7e in <rustc_hir_typeck[a8fa972dab75dcd1]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#8  0x00007f3878036f14 in <rustc_hir_typeck[a8fa972dab75dcd1]::fn_ctxt::FnCtxt>::check_block_with_expected () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#9  0x00007f387803c421 in <rustc_hir_typeck[a8fa972dab75dcd1]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#10 0x00007f38776da6ee in rustc_hir_typeck[a8fa972dab75dcd1]::check::check_fn () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#11 0x00007f38776d067e in rustc_hir_typeck[a8fa972dab75dcd1]::typeck () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#12 0x00007f38776d00fb in rustc_query_impl[606e62fa16b19c12]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[606e62fa16b19c12]::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[fecc9a0d5ad25b76]::query::erase::Erased<[u8; 8: usize]>> () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#13 0x00007f38775bceee in rustc_query_system[ea7bd5a7968c1f8]::query::plumbing::try_execute_query::<rustc_query_impl[606e62fa16b19c12]::DynamicConfig<rustc_query_system[ea7bd5a7968c1f8]::query::caches::VecCache<rustc_span[2c9242bd6e97c416]::def_id::LocalDefId, rustc_middle[fecc9a0d5ad25b76]::query::erase::Erased<[u8; 8: usize]>>, false: bool, false: bool, false: bool>, rustc_query_impl[606e62fa16b19c12]::plumbing::QueryCtxt, false: bool> () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#14 0x00007f38775bb9eb in rustc_query_impl[606e62fa16b19c12]::query_impl::typeck::get_query_non_incr::__rust_end_short_backtrace () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#15 0x00007f38775bb621 in <rustc_middle[fecc9a0d5ad25b76]::hir::map::Map>::par_body_owners::<rustc_hir_analysis[738c88335a519906]::check_crate::{closure#4}>::{closure#0} () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#16 0x00007f38775ba0c7 in rustc_hir_analysis[738c88335a519906]::check_crate () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#17 0x00007f3877e14687 in rustc_interface[98f3030eb7c197c2]::passes::analysis () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#18 0x00007f3877e141c7 in rustc_query_impl[606e62fa16b19c12]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[606e62fa16b19c12]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[fecc9a0d5ad25b76]::query::erase::Erased<[u8; 1: usize]>> () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#19 0x00007f3878238725 in rustc_query_system[ea7bd5a7968c1f8]::query::plumbing::try_execute_query::<rustc_query_impl[606e62fa16b19c12]::DynamicConfig<rustc_query_system[ea7bd5a7968c1f8]::query::caches::SingleCache<rustc_middle[fecc9a0d5ad25b76]::query::erase::Erased<[u8; 1: usize]>>, false: bool, false: bool, false: bool>, rustc_query_impl[606e62fa16b19c12]::plumbing::QueryCtxt, false: bool> () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#20 0x00007f387823848f in rustc_query_impl[606e62fa16b19c12]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#21 0x00007f38780dc512 in rustc_interface[98f3030eb7c197c2]::interface::run_compiler::<core[4db8349ac6a0c205]::result::Result<(), rustc_span[2c9242bd6e97c416]::ErrorGuaranteed>, rustc_driver_impl[df262410c7f7944d]::run_compiler::{closure#0}>::{closure#1} () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#22 0x00007f3878091d49 in std[82c32ede93c8c228]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[98f3030eb7c197c2]::util::run_in_thread_with_globals<rustc_interface[98f3030eb7c197c2]::util::run_in_thread_pool_with_globals<rustc_interface--Type <RET> for more, q to quit, c to continue without paging--
[98f3030eb7c197c2]::interface::run_compiler<core[4db8349ac6a0c205]::result::Result<(), rustc_span[2c9242bd6e97c416]::ErrorGuaranteed>, rustc_driver_impl[df262410c7f7944d]::run_compiler::{closure#0}>::{closure#1}, core[4db8349ac6a0c205]::result::Result<(), rustc_span[2c9242bd6e97c416]::ErrorGuaranteed>>::{closure#0}, core[4db8349ac6a0c205]::result::Result<(), rustc_span[2c9242bd6e97c416]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[4db8349ac6a0c205]::result::Result<(), rustc_span[2c9242bd6e97c416]::ErrorGuaranteed>> () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#23 0x00007f3878091afa in <<std[82c32ede93c8c228]::thread::Builder>::spawn_unchecked_<rustc_interface[98f3030eb7c197c2]::util::run_in_thread_with_globals<rustc_interface[98f3030eb7c197c2]::util::run_in_thread_pool_with_globals<rustc_interface[98f3030eb7c197c2]::interface::run_compiler<core[4db8349ac6a0c205]::result::Result<(), rustc_span[2c9242bd6e97c416]::ErrorGuaranteed>, rustc_driver_impl[df262410c7f7944d]::run_compiler::{closure#0}>::{closure#1}, core[4db8349ac6a0c205]::result::Result<(), rustc_span[2c9242bd6e97c416]::ErrorGuaranteed>>::{closure#0}, core[4db8349ac6a0c205]::result::Result<(), rustc_span[2c9242bd6e97c416]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[4db8349ac6a0c205]::result::Result<(), rustc_span[2c9242bd6e97c416]::ErrorGuaranteed>>::{closure#2} as core[4db8349ac6a0c205]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#24 0x00007f38797908eb in alloc::boxed::{impl#48}::call_once<(), dyn core::ops::function::FnOnce<(), Output=()>, alloc::alloc::Global> () at library/alloc/src/boxed.rs:2062
#25 alloc::boxed::{impl#48}::call_once<(), alloc::boxed::Box<dyn core::ops::function::FnOnce<(), Output=()>, alloc::alloc::Global>, alloc::alloc::Global> () at library/alloc/src/boxed.rs:2062
#26 std::sys::pal::unix::thread::{impl#2}::new::thread_start () at library/std/src/sys/pal/unix/thread.rs:108
#27 0x00007f3872aae068 in ?? () from /lib64/libc.so.6
#28 0x00007f3872b2f0bc in ?? () from /lib64/libc.so.6

Thread 2 (Thread 0x7f386a7ff6c0 (LWP 14472) "ctrl-c"):
#0  0x00007f3872b1d74e in read () from /lib64/libc.so.6
#1  0x00007f38781e2f12 in nix[891d26199dd147b8]::unistd::read () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#2  0x00007f38781e2e36 in std[82c32ede93c8c228]::sys::backtrace::__rust_begin_short_backtrace::<ctrlc[b7c9252397e4647d]::set_handler_inner<rustc_driver_impl[df262410c7f7944d]::install_ctrlc_handler::{closure#0}>::{closure#0}, ()> () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#3  0x00007f38781e2d61 in <<std[82c32ede93c8c228]::thread::Builder>::spawn_unchecked_<ctrlc[b7c9252397e4647d]::set_handler_inner<rustc_driver_impl[df262410c7f7944d]::install_ctrlc_handler::{closure#0}>::{closure#0}, ()>::{closure#2} as core[4db8349ac6a0c205]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#4  0x00007f38797908eb in alloc::boxed::{impl#48}::call_once<(), dyn core::ops::function::FnOnce<(), Output=()>, alloc::alloc::Global> () at library/alloc/src/boxed.rs:2062
#5  alloc::boxed::{impl#48}::call_once<(), alloc::boxed::Box<dyn core::ops::function::FnOnce<(), Output=()>, alloc::alloc::Global>, alloc::alloc::Global> () at library/alloc/src/boxed.rs:2062
#6  std::sys::pal::unix::thread::{impl#2}::new::thread_start () at library/std/src/sys/pal/unix/thread.rs:108
#7  0x00007f3872aae068 in ?? () from /lib64/libc.so.6
#8  0x00007f3872b2f0bc in ?? () from /lib64/libc.so.6

Thread 1 (Thread 0x7f387967f580 (LWP 14471) "rustc"):
#0  0x00007f3872aaabd6 in ?? () from /lib64/libc.so.6
#1  0x00007f3872aafa83 in ?? () from /lib64/libc.so.6
#2  0x00007f3879790a41 in std::sys::pal::unix::thread::Thread::join () at library/std/src/sys/pal/unix/thread.rs:272
#3  0x00007f38781fde6a in rustc_driver_impl[df262410c7f7944d]::run_compiler () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#4  0x00007f38781fcbb8 in rustc_driver_impl[df262410c7f7944d]::main () from /home/konnor/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/librustc_driver-802b4aa580d71496.so
#5  0x000056535a57ac07 in rustc_main[2a842e528cc0f9db]::main ()
#6  0x000056535a57abf3 in std[82c32ede93c8c228]::sys::backtrace::__rust_begin_short_backtrace::<fn(), ()> ()
#7  0x000056535a57abe9 in <std[82c32ede93c8c228]::rt::lang_start<()>::{closure#0} as core[4db8349ac6a0c205]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} ()
#8  0x00007f3879768662 in core::ops::function::impls::{impl#2}::call_once<(), (dyn core::ops::function::Fn<(), Output=i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe)> () at library/core/src/ops/function.rs:284
#9  std::panicking::try::do_call<&(dyn core::ops::function::Fn<(), Output=i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe), i32> () at library/std/src/panicking.rs:553
#10 std::panicking::try<i32, &(dyn core::ops::function::Fn<(), Output=i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe)> () at library/std/src/panicking.rs:517
#11 std::panic::catch_unwind<&(dyn core::ops::function::Fn<(), Output=i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe), i32> () at library/std/src/panic.rs:350
#12 std::rt::lang_start_internal::{closure#2} () at library/std/src/rt.rs:141
#13 std::panicking::try::do_call<std::rt::lang_start_internal::{closure_env#2}, isize> () at library/std/src/panicking.rs:553
#14 std::panicking::try<isize, std::rt::lang_start_internal::{closure_env#2}> () at library/std/src/panicking.rs:517
#15 std::panic::catch_unwind<std::rt::lang_start_internal::{closure_env#2}, isize> () at library/std/src/panic.rs:350
--Type <RET> for more, q to quit, c to continue without paging--
#16 std::rt::lang_start_internal () at library/std/src/rt.rs:141
#17 0x000056535a57ad27 in main ()
```

</p>
</details>