Repository: rust
Issue: ICE: `called Option::unwrap() on a None value in rustc_middle/src/mir/tcx.rs`
Labels: I-ICE,T-compiler,A-MIR,C-bug,F-generic_const_exprs,S-has-mcve,S-bug-has-test,requires-incomplete-features

<!--
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

(hand-reduced)

```Rust
#![feature(generic_const_exprs)]
#![allow(incomplete_features)]

trait Foo {
    type Output;
}

impl Foo for [u8; 3] {
    type Output = [u8; 3];
}

static A: <[u8; N] as Foo>::Output = [1, 2, 3];

fn main() {
    || {
        let _ = A[1];
    };
}
```

<details><summary><strong>(original)</strong></summary>
<p>

```Rust
//@ build-pass

#![feature(generic_const_exprs)]
#![allow(incomplete_features)]

trait Foo {
    type Output;

    fn foo() -> Self::Output;
}

impl Foo for [u8; 3] {
    type Output = [u8; 1 + 2];

    fn foo() -> [u8; 3] {
        [1u8; 3]
    }
}

fn bug<const N: usize>()
where
    [u8; N]: Foo,
    <[u8; N] as Foo>::Output: AsRef<[u8]>,
{
    <[u8; N]>::foo().as_ref();
}

fn main() {
    bug::<3>();
}


//@ build-pass
#![feature(coroutines)]

static A: <[u8; N] as Foo>::Output = [1, 2, 3, 4, 5];

fn main() {
    #[coroutine] static || {
        let u = A[{yield; 1}];
    };
    #[coroutine] static || {
        match A {
            i if { yield; true } => (),
            _ => (),
        }
    };
}
```

</p>
</details>


### 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.80.0-nightly (6f3df08aa 2024-05-30)
binary: rustc
commit-hash: 6f3df08aadf71e8d4bf7e49f5dc10dfa6f254cb4
commit-date: 2024-05-30
host: x86_64-apple-darwin
release: 1.80.0-nightly
LLVM version: 18.1.6
```

### Error output

Command: `rustc`

```
error[E0425]: cannot find value `N` in this scope
  --> r_tcx_1F6082.rs:12:17
   |
12 | static A: <[u8; N] as Foo>::Output = [1, 2, 3];
   |                 ^ not found in this scope
```

<!--
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>

```
thread 'rustc' panicked at compiler/rustc_middle/src/mir/tcx.rs:88:58:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic
   3: core::option::unwrap_failed
   4: <rustc_middle::mir::tcx::PlaceTy>::projection_ty
   5: <rustc_mir_build::build::matches::MatchPair>::new
   6: <rustc_mir_build::build::matches::FlatPat>::new
   7: <rustc_mir_build::build::Builder>::place_into_pattern
   8: <rustc_mir_build::build::Builder>::ast_block_stmts
   9: <rustc_mir_build::build::Builder>::expr_into_dest
  10: <rustc_mir_build::build::Builder>::expr_into_dest::{closure#0}
  11: <rustc_mir_build::build::Builder>::expr_into_dest
  12: rustc_mir_build::build::construct_fn
  13: rustc_mir_build::build::mir_build
  14: rustc_mir_transform::mir_built
      [... omitted 1 frame ...]
  15: <rustc_mir_build::check_unsafety::UnsafetyVisitor>::visit_inner_body
  16: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_expr
  17: <rustc_mir_build::check_unsafety::UnsafetyVisitor as rustc_middle::thir::visit::Visitor>::visit_expr
  18: rustc_middle::thir::visit::walk_block::<rustc_mir_build::check_unsafety::UnsafetyVisitor>
  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_expr
  21: rustc_mir_build::check_unsafety::check_unsafety
      [... omitted 1 frame ...]
  22: <rustc_middle::hir::map::Map>::par_body_owners::<rustc_interface::passes::run_required_analyses::{closure#1}::{closure#0}>::{closure#0}
  23: rustc_interface::passes::run_required_analyses
  24: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  25: <rustc_interface::queries::QueryResult<&rustc_middle::ty::context::GlobalCtxt>>::enter::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}::{closure#1}::{closure#3}>
  26: rustc_interface::interface::run_compiler::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

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 `/Volumes/T7/workspace/240530_100chaos_tree_combine_typ/icefiles/rustc-ice-2024-05-31T07_58_41-11292.txt` to your bug report

query stack during panic:
#0 [mir_built] building MIR for `main::{closure#0}`
#1 [check_unsafety] unsafety-checking `main`
#2 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0425`.
```

</p>
</details>

### Note
- ICE location: `rustc_middle/src/mir/tcx.rs L88`
  https://github.com/rust-lang/rust/blob/6f3df08aadf71e8d4bf7e49f5dc10dfa6f254cb4/compiler/rustc_middle/src/mir/tcx.rs#L80-L89