Repository: rust
Issue: ICE: `panic in a destructor during cleanup`
Labels: I-ICE,T-compiler,C-bug,F-type_alias_impl_trait,S-bug-has-test

<!--
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
#![feature(type_alias_impl_trait)]

trait Trait {
    type Gat<'lt>;
}

fn dyn_hoops<T: Trait>(_: T) -> *const dyn for<'b> Iterator<Item = impl Sized + Captures<'a>> {
    loop {}
}

mod typeck {
    use super::*;
    type Opaque = impl Sized;
    fn define() -> Option<Opaque> {
        let _: Opaque = dyn_hoops::<u8>(0);
        None
    }
}

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.83.0-nightly (f559d6188 2024-10-05)
binary: rustc
commit-hash: f559d6188828b738ce7e7c2e4d99bf03111336d6
commit-date: 2024-10-05
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0
```

### Error output

```
<output>
```

<!--
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[E0261]: use of undeclared lifetime name `'a`
 --> a.rs:7:90
  |
7 | fn dyn_hoops<T: Trait>(_: T) -> *const dyn for<'b> Iterator<Item = impl Sized + Captures<'a>> {
  |                                                                                          ^^ undeclared lifetime
  |
  = note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
help: consider making the bound lifetime-generic with a new `'a` lifetime
  |
7 | fn dyn_hoops<T: Trait>(_: T) -> *const dyn for<'b> Iterator<Item = impl Sized + for<'a> Captures<'a>> {
  |                                                                                 +++++++
help: consider making the bound lifetime-generic with a new `'a` lifetime
  |
7 | fn dyn_hoops<T: Trait>(_: T) -> *const dyn for<'a, 'b> Iterator<Item = impl Sized + Captures<'a>> {
  |                                                +++
help: consider introducing lifetime `'a` here
  |
7 | fn dyn_hoops<'a, T: Trait>(_: T) -> *const dyn for<'b> Iterator<Item = impl Sized + Captures<'a>> {
  |              +++

error[E0405]: cannot find trait `Captures` in this scope
 --> a.rs:7:81
  |
7 | fn dyn_hoops<T: Trait>(_: T) -> *const dyn for<'b> Iterator<Item = impl Sized + Captures<'a>> {
  |                                                                                 ^^^^^^^^ not found in this scope

thread 'rustc' panicked at core/src/panicking.rs:229:5:
panic in a destructor during cleanup

error: the compiler unexpectedly panicked. this is a bug.

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 make sure that you have updated to the latest nightly

note: please attach the file at `/tmp/im/rustc-ice-2024-10-05T15_12_11-2721517.txt` to your bug report

query stack during panic:
#0 [typeck] type-checking `typeck::define`
#1 [type_of_opaque] computing type of opaque `typeck::Opaque::{opaque#0}`
#2 [type_of] computing type of `typeck::Opaque::{opaque#0}`
#3 [check_well_formed] checking that `typeck::Opaque::{opaque#0}` is well-formed
#4 [check_mod_type_wf] checking that types are well-formed in module `typeck`
#5 [analysis] running analysis passes on this crate
end of query stack
thread caused non-unwinding panic. aborting.
```

</p>
</details>