Repository: rust
Issue: generic_const_exprs: ICE when converting into type related to trait's internal generic const
Labels: I-ICE,T-compiler,C-bug,requires-nightly,glacier,F-generic_const_exprs,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

```Rust
#![feature(generic_const_exprs)]
use std::marker::PhantomData;

trait Trait {
    const CONST: usize;
}

struct A<T: Trait> {
    _marker: PhantomData<T>,
}

impl<const N: usize> Trait for [i8; N] {
    const CONST: usize = N;
}

impl<const N: usize> From<usize> for A<[i8; N]> {
    fn from(_: usize) -> Self {
        todo!()
    }
}

impl<T: Trait> From<A<[i8; T::CONST]>> for A<T> {
    fn from(_: A<[i8; T::CONST]>) -> Self {
        todo!()
    }
}

fn f<T: Trait>() -> A<T>
where
    [(); T::CONST]:,
{
    // Usage of `0` is arbitrary
    let a = A::<[i8; T::CONST]>::from(0);
    A::<T>::from(a)
}

fn main() {
    // Usage of `1` is arbitrary
    f::<[i8; 1]>();
}
```

Note: ICE does not occur if either of the two lines in `f()` is replaced with `todo!()`; i.e.
```Rust
fn f<T: Trait>() -> A<T>
where
    [(); T::CONST]:,
{
    let a: A::<[i8; T::CONST]> = todo!();
    A::<T>::from(a)
}
```
or
```Rust
fn f<T: Trait>() -> A<T>
where
    [(); T::CONST]:,
{
    let a = A::<[i8; T::CONST]>::from(0);
    todo!()
}
```

### 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.65.0-nightly (78a891d36 2022-09-06)
binary: rustc
commit-hash: 78a891d364a7358ed9eb9c93099ba2f3e6817ca6
commit-date: 2022-09-06
host: x86_64-unknown-linux-gnu
release: 1.65.0-nightly
LLVM version: 15.0.0
```

### Error output

```
thread 'rustc' panicked at 'called `Result::unwrap()` on an `Err` value: ErrorGuaranteed(())', compiler/rustc_monomorphize/src/collector.rs:959:84

error: internal compiler error: unexpected panic

note: 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: rustc 1.65.0-nightly (78a891d36 2022-09-06) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack
error: internal compiler error: no errors encountered even though `delay_span_bug` issued

error: internal compiler error: encountered ambiguity selecting `Binder(<A<[i8; 1]> as std::convert::From<A<[i8; 1]>>>, [])` during codegen, presuming due to overflow or prior type error
  |
  = note: delayed at compiler/rustc_ty_utils/src/instance.rs:211:37

thread 'rustc' panicked at 'Box<dyn Any>', compiler/rustc_errors/src/lib.rs:1494:13

note: 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: rustc 1.65.0-nightly (78a891d36 2022-09-06) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
thread panicked while panicking. aborting.
warning: `play-rust` (bin "play-rust") generated 1 warning
error: could not compile `play-rust`; 1 warning emitted

Caused by:
  process didn't exit successfully: `rustc --crate-name play_rust --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=334955660983def9 -C extra-filename=-334955660983def9 --out-dir /kaist-cp-home/sungsoo.han/WD/play-rust/target/debug/deps -C incremental=/kaist-cp-home/sungsoo.han/WD/play-rust/target/debug/incremental -L dependency=/kaist-cp-home/sungsoo.han/WD/play-rust/target/debug/deps` (signal: 6, SIGABRT: process abort signal)
```

<!--
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 for first panic</strong></summary>
<p>

```
stack backtrace:
   0: rust_begin_unwind
             at /rustc/78a891d364a7358ed9eb9c93099ba2f3e6817ca6/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/78a891d364a7358ed9eb9c93099ba2f3e6817ca6/library/core/src/panicking.rs:142:14
   2: core::result::unwrap_failed
             at /rustc/78a891d364a7358ed9eb9c93099ba2f3e6817ca6/library/core/src/result.rs:1785:5
   3: rustc_monomorphize::collector::collect_neighbours
   4: rustc_monomorphize::collector::collect_items_rec
   5: rustc_monomorphize::collector::collect_items_rec
   6: rustc_data_structures::sync::par_for_each_in::<alloc::vec::Vec<rustc_middle::mir::mono::MonoItem>, rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}::{closure#0}>
   7: <rustc_session::session::Session>::time::<(), rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}>
   8: rustc_monomorphize::collector::collect_crate_mono_items
   9: rustc_monomorphize::partitioning::collect_and_partition_mono_items
  10: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, (), (&std::collections::hash::set::HashSet<rustc_span::def_id::DefId, core::hash::BuildHasherDefault<rustc_hash::FxHasher>>, &[rustc_middle::mir::mono::CodegenUnit])>
  11: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::plumbing::QueryCtxt, rustc_query_system::query::caches::DefaultCache<(), (&std::collections::hash::set::HashSet<rustc_span::def_id::DefId, core::hash::BuildHasherDefault<rustc_hash::FxHasher>>, &[rustc_middle::mir::mono::CodegenUnit])>>
  12: rustc_query_system::query::plumbing::get_query::<rustc_query_impl::queries::collect_and_partition_mono_items, rustc_query_impl::plumbing::QueryCtxt>
  13: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::collect_and_partition_mono_items
  14: rustc_codegen_ssa::base::codegen_crate::<rustc_codegen_llvm::LlvmCodegenBackend>
  15: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
  16: <rustc_session::session::Session>::time::<alloc::boxed::Box<dyn core::any::Any>, rustc_interface::passes::start_codegen::{closure#0}>
  17: <rustc_interface::passes::QueryContext>::enter::<<rustc_interface::queries::Queries>::ongoing_codegen::{closure#0}::{closure#0}, core::result::Result<alloc::boxed::Box<dyn core::any::Any>, rustc_errors::ErrorGuaranteed>>
  18: <rustc_interface::queries::Queries>::ongoing_codegen
  19: rustc_interface::interface::create_compiler_and_run::<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>
  20: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>>
```

</p>
</details>


<details><summary><strong>Backtrace for second panic</strong></summary>
<p>

```
stack backtrace:
   0:     0x7f51daed4210 - std::backtrace_rs::backtrace::libunwind::trace::h21646be08690cc40
                               at /rustc/78a891d364a7358ed9eb9c93099ba2f3e6817ca6/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   1:     0x7f51daed4210 - std::backtrace_rs::backtrace::trace_unsynchronized::h9f1fe42f44553716
                               at /rustc/78a891d364a7358ed9eb9c93099ba2f3e6817ca6/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f51daed4210 - std::sys_common::backtrace::_print_fmt::hab0a8a42ed612520
                               at /rustc/78a891d364a7358ed9eb9c93099ba2f3e6817ca6/library/std/src/sys_common/backtrace.rs:66:5
   3:     0x7f51daed4210 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h8335f52e54d46ccf
                               at /rustc/78a891d364a7358ed9eb9c93099ba2f3e6817ca6/library/std/src/sys_common/backtrace.rs:45:22
   4:     0x7f51daf2f02e - core::fmt::write::hfcbb54030acb2744
                               at /rustc/78a891d364a7358ed9eb9c93099ba2f3e6817ca6/library/core/src/fmt/mod.rs:1202:17
   5:     0x7f51daec4cd5 - std::io::Write::write_fmt::hed351c44331cd179
                               at /rustc/78a891d364a7358ed9eb9c93099ba2f3e6817ca6/library/std/src/io/mod.rs:1679:15
   6:     0x7f51daed6e63 - std::sys_common::backtrace::_print::h40cfc30b9abc5c43
                               at /rustc/78a891d364a7358ed9eb9c93099ba2f3e6817ca6/library/std/src/sys_common/backtrace.rs:48:5
   7:     0x7f51daed6e63 - std::sys_common::backtrace::print::h9d34bfe87c2cc4a0
                               at /rustc/78a891d364a7358ed9eb9c93099ba2f3e6817ca6/library/std/src/sys_common/backtrace.rs:35:9
   8:     0x7f51daed6e63 - std::panicking::default_hook::{{closure}}::h7e3919c83aded8ae
                               at /rustc/78a891d364a7358ed9eb9c93099ba2f3e6817ca6/library/std/src/panicking.rs:295:22
   9:     0x7f51daed6b4f - std::panicking::default_hook::h7f7edfc94586dad5
                               at /rustc/78a891d364a7358ed9eb9c93099ba2f3e6817ca6/library/std/src/panicking.rs:314:9
  10:     0x7f51dd7434e1 - rustc_driver[2795d98b07d801db]::DEFAULT_HOOK::{closure#0}::{closure#0}
  11:     0x7f51daed769d - std::panicking::rust_panic_with_hook::h62ce697bbafdcf0b
                               at /rustc/78a891d364a7358ed9eb9c93099ba2f3e6817ca6/library/std/src/panicking.rs:702:17
  12:     0x7f51de889041 - std[8e0d3d70c3a8d58a]::panicking::begin_panic::<rustc_errors[bdb1f8fbd8907e61]::ExplicitBug>::{closure#0}
  13:     0x7f51de888b96 - std[8e0d3d70c3a8d58a]::sys_common::backtrace::__rust_end_short_backtrace::<std[8e0d3d70c3a8d58a]::panicking::begin_panic<rustc_errors[bdb1f8fbd8907e61]::ExplicitBug>::{closure#0}, !>
  14:     0x7f51de888b56 - std[8e0d3d70c3a8d58a]::panicking::begin_panic::<rustc_errors[bdb1f8fbd8907e61]::ExplicitBug>
  15:     0x7f51de88b776 - std[8e0d3d70c3a8d58a]::panic::panic_any::<rustc_errors[bdb1f8fbd8907e61]::ExplicitBug>
  16:     0x7f51dd299cd8 - <rustc_errors[bdb1f8fbd8907e61]::HandlerInner as core[31aaf346d0c050fe]::ops::drop::Drop>::drop
  17:     0x7f51dc53dc68 - core[31aaf346d0c050fe]::ptr::drop_in_place::<rustc_session[e3515b6a49b6e8ce]::parse::ParseSess>
  18:     0x7f51dc4fbfe8 - <alloc[812cd34b3dffb023]::rc::Rc<rustc_session[e3515b6a49b6e8ce]::session::Session> as core[31aaf346d0c050fe]::ops::drop::Drop>::drop
  19:     0x7f51dc4f8f9d - core[31aaf346d0c050fe]::ptr::drop_in_place::<rustc_interface[9bc1f0c6c5e6f2d9]::interface::Compiler>
  20:     0x7f51dc4f71e4 - rustc_interface[9bc1f0c6c5e6f2d9]::interface::create_compiler_and_run::<core[31aaf346d0c050fe]::result::Result<(), rustc_errors[bdb1f8fbd8907e61]::ErrorGuaranteed>, rustc_driver[2795d98b07d801db]::run_compiler::{closure#1}>
  21:     0x7f51dc4f4ce1 - <scoped_tls[cfe2a4079d5abda6]::ScopedKey<rustc_span[fcbe8f7b61d422ec]::SessionGlobals>>::set::<rustc_interface[9bc1f0c6c5e6f2d9]::interface::run_compiler<core[31aaf346d0c050fe]::result::Result<(), rustc_errors[bdb1f8fbd8907e61]::ErrorGuaranteed>, rustc_driver[2795d98b07d801db]::run_compiler::{closure#1}>::{closure#0}, core[31aaf346d0c050fe]::result::Result<(), rustc_errors[bdb1f8fbd8907e61]::ErrorGuaranteed>>
  22:     0x7f51dc4f49cf - std[8e0d3d70c3a8d58a]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[9bc1f0c6c5e6f2d9]::util::run_in_thread_pool_with_globals<rustc_interface[9bc1f0c6c5e6f2d9]::interface::run_compiler<core[31aaf346d0c050fe]::result::Result<(), rustc_errors[bdb1f8fbd8907e61]::ErrorGuaranteed>, rustc_driver[2795d98b07d801db]::run_compiler::{closure#1}>::{closure#0}, core[31aaf346d0c050fe]::result::Result<(), rustc_errors[bdb1f8fbd8907e61]::ErrorGuaranteed>>::{closure#0}, core[31aaf346d0c050fe]::result::Result<(), rustc_errors[bdb1f8fbd8907e61]::ErrorGuaranteed>>
  23:     0x7f51dd5b1ba9 - <<std[8e0d3d70c3a8d58a]::thread::Builder>::spawn_unchecked_<rustc_interface[9bc1f0c6c5e6f2d9]::util::run_in_thread_pool_with_globals<rustc_interface[9bc1f0c6c5e6f2d9]::interface::run_compiler<core[31aaf346d0c050fe]::result::Result<(), rustc_errors[bdb1f8fbd8907e61]::ErrorGuaranteed>, rustc_driver[2795d98b07d801db]::run_compiler::{closure#1}>::{closure#0}, core[31aaf346d0c050fe]::result::Result<(), rustc_errors[bdb1f8fbd8907e61]::ErrorGuaranteed>>::{closure#0}, core[31aaf346d0c050fe]::result::Result<(), rustc_errors[bdb1f8fbd8907e61]::ErrorGuaranteed>>::{closure#1} as core[31aaf346d0c050fe]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  24:     0x7f51daee14f3 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hdf9c758750ff5822
                               at /rustc/78a891d364a7358ed9eb9c93099ba2f3e6817ca6/library/alloc/src/boxed.rs:1940:9
  25:     0x7f51daee14f3 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h0f4b4f05b38d1a00
                               at /rustc/78a891d364a7358ed9eb9c93099ba2f3e6817ca6/library/alloc/src/boxed.rs:1940:9
  26:     0x7f51daee14f3 - std::sys::unix::thread::Thread::new::thread_start::hfdfaccc5f3b267e1
                               at /rustc/78a891d364a7358ed9eb9c93099ba2f3e6817ca6/library/std/src/sys/unix/thread.rs:108:17
  27:     0x7f51dac30b43 - start_thread
                               at ./nptl/./nptl/pthread_create.c:442:8
  28:     0x7f51dacc2a00 - clone3
                               at ./misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:81
  29:                0x0 - <unknown>
```

</p>
</details>