Repository: rust
Issue: `-Znext-solver`: ICE with closure capturing iterated variable in 2021 edition
Labels: I-ICE,E-needs-test,A-closures,T-compiler,C-bug,WG-trait-system-refactor

### Code

Using `rustc --edition=2021 -Znext-solver=globally foo.rs`.

```Rust
fn main() {
    for item in &[1, 2, 3] {
        let _ = || *item;
    }
}
```

Manually minimized from original code in [`objc2`](https://github.com/madsmtm/objc2), specifically [here](https://github.com/madsmtm/objc2/blob/3b507d6eda90535a217441fd69f1cad9ad180397/crates/objc2/src/__macro_helpers/declare_class.rs#L388-L399).


### 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.77.0-nightly (30dfb9e04 2024-01-14)
binary: rustc
commit-hash: 30dfb9e046aeb878db04332c74de76e52fb7db10
commit-date: 2024-01-14
host: aarch64-apple-darwin
release: 1.77.0-nightly
LLVM version: 17.0.6
```

### Error output

```
thread 'rustc' panicked at compiler/rustc_mir_build/src/build/expr/as_place.rs:253:31:
called `Option::unwrap()` on a `None` value
```

<!--
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>

```
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic
   3: core::option::unwrap_failed
   4: <rustc_mir_build::build::Builder>::expr_into_dest
   5: <rustc_mir_build::build::Builder>::in_scope::<<rustc_mir_build::build::Builder>::expr_into_dest::{closure#0}::{closure#0}, ()>
   6: <rustc_mir_build::build::Builder>::expr_into_dest::{closure#0}
   7: <rustc_mir_build::build::Builder>::expr_into_dest
   8: rustc_mir_build::build::mir_build::{closure#0}
   9: rustc_mir_build::build::mir_built
      [... omitted 2 frames ...]
  10: <rustc_mir_build::check_unsafety::UnsafetyVisitor>::visit_inner_body
  11: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_expr
  12: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_expr
  13: rustc_middle::thir::visit::walk_stmt::<rustc_mir_build::check_unsafety::UnsafetyVisitor>
  14: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_block
  15: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_expr
  16: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_expr
  17: rustc_middle::thir::visit::walk_expr::<rustc_mir_build::check_unsafety::UnsafetyVisitor>
  18: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_expr
  19: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_expr
  20: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_block
  21: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_expr
  22: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_expr
  23: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_expr
  24: rustc_middle::thir::visit::walk_expr::<rustc_mir_build::check_unsafety::UnsafetyVisitor>
  25: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_expr
  26: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_expr
  27: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_expr
  28: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_expr
  29: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_expr
  30: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_expr
  31: rustc_mir_build::check_unsafety::check_unsafety
      [... omitted 2 frames ...]
  32: <rustc_middle::hir::map::Map>::par_body_owners::<rustc_interface::passes::analysis::{closure#1}::{closure#0}>::{closure#0}
  33: <rustc_session::session::Session>::time::<(), rustc_interface::passes::analysis::{closure#1}>
  34: rustc_interface::passes::analysis
      [... omitted 2 frames ...]
  35: <rustc_middle::ty::context::GlobalCtxt>::enter::<rustc_driver_impl::run_compiler::{closure#0}::{closure#0}::{closure#3}, core::result::Result<(), rustc_span::ErrorGuaranteed>>
  36: <rustc_interface::interface::Compiler>::enter::<rustc_driver_impl::run_compiler::{closure#0}::{closure#0}, core::result::Result<core::option::Option<rustc_interface::queries::Linker>, rustc_span::ErrorGuaranteed>>
  37: rustc_span::set_source_map::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#0}::{closure#0}>
  38: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>
  39: rustc_span::create_session_globals_then::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
```

</p>
</details>