Repository: rust
Issue: `rustc` never finishes after emitting error diagnostic
Labels: T-compiler,C-bug,I-hang

This issue was identified in Fuchsia. We're were unable to move this issue out of tree for reproduction.

Adding a relatively innocuous `HashMap<u32, String>` field to an [existing struct](https://fuchsia.googlesource.com/fuchsia/+/refs/heads/main/src/starnix/kernel/task/kernel.rs#60) causes the compiler to hang forever:

```rust
pub struct Kernel {
    pub new_map: HashMap<u32, String>,
    /// The Zircon job object that holds the processes running in this kernel.
    pub job: zx::Job,
```

Right before it hangs, it prints:

```
error[E0412]: cannot find type `HashMap` in this scope
  --> ../../src/starnix/kernel/task/kernel.rs:61:18
   |
61 |     pub new_map: HashMap<u32, String>,
   |                  ^^^^^^^ not found in this scope
   |
help: consider importing this struct
   |
5  + use std::collections::HashMap;
   |
```

The trailing pipe seems to suggest that some additional diagnostic information is intended to be printed. Adding `use std::collections::HashMap;` to the file fixes the error and causes the compilation to finish normally.

By attaching to a debug build with gdb, we were able to capture [a full call stack](https://gist.github.com/djkoloski/a3b0634b02c25d3b5e24bba8b02a35ad). We noticed that the call stack contained many repetitions of this particular set of frames:

```
#92 rustc_trait_selection::traits::select::{impl#2}::evaluate_candidate::{closure#0} () at compiler/rustc_trait_selection/src/traits/select/mod.rs:1307
#93 rustc_trait_selection::traits::select::SelectionContext::evaluate_candidate (self=0x7f5461f66208, stack=0x7f5461eeda70, candidate=0x7f5461ee9270) at compiler/rustc_trait_selection/src/traits/select/mod.rs:1296
#94 0x00007f546b026c16 in rustc_trait_selection::traits::select::SelectionContext::evaluate_stack (self=0x7f5461f66208, stack=0x7f5461eeda70) at compiler/rustc_trait_selection/src/traits/select/mod.rs:1270
#95 0x00007f546ae96301 in rustc_trait_selection::traits::select::{impl#2}::evaluate_trait_predicate_recursively::{closure#0}::{closure#1} (this=0x7f5461f66208) at compiler/rustc_trait_selection/src/traits/select/mod.rs:1092
#96 rustc_trait_selection::traits::select::{impl#2}::in_task::{closure#0}<rustc_trait_selection::traits::select::{impl#2}::evaluate_trait_predicate_recursively::{closure#0}::{closure_env#1}, core::result::Result<rustc_middle::traits::select::EvaluationResult, rustc_middle::traits::select::OverflowError>> () at compiler/rustc_trait_selection/src/traits/select/mod.rs:1440
#97 rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::DepsType>::with_anon_task<rustc_middle::dep_graph::DepsType, rustc_middle::ty::context::TyCtxt, rustc_trait_selection::traits::select::{impl#2}::in_task::{closure_env#0}<rustc_trait_selection::traits::select::{impl#2}::evaluate_trait_predicate_recursively::{closure#0}::{closure_env#1}, core::result::Result<rustc_middle::traits::select::EvaluationResult, rustc_middle::traits::select::OverflowError>>, core::result::Result<rustc_middle::traits::select::EvaluationResult, rustc_middle::traits::select::OverflowError>> (self=0x7f5461f6a4f8, cx=..., dep_kind=..., op=...) at compiler/rustc_query_system/src/dep_graph/graph.rs:303
#98 0x00007f546b042687 in rustc_trait_selection::traits::select::SelectionContext::in_task<rustc_trait_selection::traits::select::{impl#2}::evaluate_trait_predicate_recursively::{closure#0}::{closure_env#1}, core::result::Result<rustc_middle::traits::select::EvaluationResult, rustc_middle::traits::select::OverflowError>> (self=0x7f5461f66208, op=...) at compiler/rustc_trait_selection/src/traits/select/mod.rs:1440
#99 rustc_trait_selection::traits::select::{impl#2}::evaluate_trait_predicate_recursively::{closure#0} () at compiler/rustc_trait_selection/src/traits/select/mod.rs:1091
#100 rustc_trait_selection::traits::select::SelectionContext::evaluate_trait_predicate_recursively (self=<optimized out>, previous_stack=..., obligation=...) at compiler/rustc_trait_selection/src/traits/select/mod.rs:1046
#101 0x00007f546b040468 in rustc_trait_selection::traits::select::{impl#2}::evaluate_predicate_recursively::{closure#0}::{closure#0} () at compiler/rustc_trait_selection/src/traits/select/mod.rs:686
#102 0x00007f546b03fd09 in stacker::maybe_grow<core::result::Result<rustc_middle::traits::select::EvaluationResult, rustc_middle::traits::select::OverflowError>, rustc_trait_selection::traits::select::{impl#2}::evaluate_predicate_recursively::{closure#0}::{closure_env#0}> (red_zone=102400, stack_size=1048576, callback=...) at /usr/local/google/home/dkoloski/.cargo/registry/src/index.crates.io-6f17d22bba15001f/stacker-0.1.15/src/lib.rs:55
#103 rustc_data_structures::stack::ensure_sufficient_stack<core::result::Result<rustc_middle::traits::select::EvaluationResult, rustc_middle::traits::select::OverflowError>, rustc_trait_selection::traits::select::{impl#2}::evaluate_predicate_recursively::{closure#0}::{closure_env#0}> (f=...) at compiler/rustc_data_structures/src/stack.rs:17
#104 rustc_trait_selection::traits::select::{impl#2}::evaluate_predicate_recursively::{closure#0} () at compiler/rustc_trait_selection/src/traits/select/mod.rs:679
#105 rustc_trait_selection::traits::select::SelectionContext::evaluate_predicate_recursively (self=<optimized out>, previous_stack=..., obligation=...) at compiler/rustc_trait_selection/src/traits/select/mod.rs:658
#106 0x00007f546b03f4bc in rustc_trait_selection::traits::select::SelectionContext::evaluate_predicates_recursively<alloc::vec::into_iter::IntoIter<rustc_infer::traits::Obligation<rustc_middle::ty::Predicate>, alloc::alloc::Global>> (self=0x7f5461f66208, stack=..., predicates=...) at compiler/rustc_trait_selection/src/traits/select/mod.rs:646
#107 0x00007f546ae35d6f in rustc_trait_selection::traits::select::{impl#2}::evaluate_candidate::{closure#0}::{closure#0} (this=0x7f5461f66208) at compiler/rustc_trait_selection/src/traits/select/mod.rs:1312
#108 rustc_trait_selection::traits::select::{impl#2}::evaluation_probe::{closure#0}<rustc_trait_selection::traits::select::{impl#2}::evaluate_candidate::{closure#0}::{closure_env#0}> (snapshot=0x7f5461eee2f0) at compiler/rustc_trait_selection/src/traits/select/mod.rs:612
#109 rustc_infer::infer::InferCtxt::probe<core::result::Result<rustc_middle::traits::select::EvaluationResult, rustc_middle::traits::select::OverflowError>, rustc_trait_selection::traits::select::{impl#2}::evaluation_probe::{closure_env#0}<rustc_trait_selection::traits::select::{impl#2}::evaluate_candidate::{closure#0}::{closure_env#0}>> (self=0x7f5461f662d8, f=...) at compiler/rustc_infer/src/infer/mod.rs:885
#110 0x00007f546b044753 in rustc_trait_selection::traits::select::SelectionContext::evaluation_probe<rustc_trait_selection::traits::select::{impl#2}::evaluate_candidate::{closure#0}::{closure_env#0}> (self=0x7f5461f66208, op=...) at compiler/rustc_trait_selection/src/traits/select/mod.rs:610
```

In our case, the total number of frames reached about 2500. It looks like this may be hitting some kind of exponential computation edge case.

Potentially related: #116914

### Meta

`rustc --version --verbose`:
```
rustc 1.75.0-nightly (9d1e4b787 2023-10-11)
binary: rustc
commit-hash: 9d1e4b7870f0aecb9f53e71f3cca3529b21d677a
commit-date: 2023-10-11
host: x86_64-unknown-linux-gnu
release: 1.75.0-nightly
LLVM version: 17.0.2
```

This is a custom-built toolchain following our [toolchain build instructions](https://fuchsia.dev/fuchsia-src/development/build/rust_toolchain?hl=en).