Repository: rust
Issue: ICE error triggered in Rust code involving conditional type and generic constant expressions
Labels: I-ICE,T-compiler,C-bug,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
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
trait Ret {
    type R;
}
struct Cond<const PRED: bool, U, V>(std::marker::PhantomData<U>, std::marker::PhantomData<V>);
impl<U, V> Ret for Cond<true, U, V> {
    type R = U;
}
impl<U, V> Ret for Cond<false, U, V> {
    type R = V;
}
struct RobinHashTable<
    const MAX_LENGTH: usize,
    CellIdx = <Cond<{ MAX_LENGTH < 65535 }, u16, u32> as Ret>::R,
> {
    _idx: CellIdx,
}
impl<CellIdx> RobinHashTable<MAX_LENGTH, CellIdx> {
    fn new() -> Self {
        Self {
            _idx: CellIdx { MAX_LENGTH },
        }
    }
}
impl<CellIdx> HashMapBase<CellIdx> {
    fn new() -> Self {
        Self {
            _idx: CellIdx { 0 },
        }
    }
}
impl<CellIdx> HashMapBase<CellIdx> for RobinHashTable<MAX_LENGTH, CellIdx> {
    fn hash<H: Hash + Hasher>(&self, 

    ) -> H {
        self._idx.hash()
    }
    fn eq(&self, other: &Self) -> bool {
        self._idx.eq(other._idx)
    }
}
impl<CellIdx> HashMapBase<CellIdx> for RobinHashTable<MAX_LENGTH, CellIdx> {
    fn hash<H: Hash + Hasher>(&self, other: &Self) -> H {
        self._idx.hash(other._idx)
    }
    fn eq(&self, other: &Self) -> bool {
        self._idx.eq(other._idx)
    }
}
#[test]
fn test_size_of_robin_hash_table() {
    use std::mem::size_of;
    println!("{}", size_of::<RobinHashTable<1024>>());
    println!("{}", size_of::<RobinHashTable<65536>>());
}
```


### 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.75.0-nightly (960754090 2023-10-06)
binary: rustc
commit-hash: 960754090acc9cdd2a5a57586f244c0fc712d26c
commit-date: 2023-10-06
host: x86_64-unknown-linux-gnu
release: 1.75.0-nightly
LLVM version: 17.0.2
```

### Error output

```
error: expected identifier, found `0`
  --> ./test/1.rs:29:29
   |
29 |             _idx: CellIdx { 0 },
   |                   -------   ^ expected identifier
   |                   |
   |                   while parsing this struct

error[E0412]: cannot find type `MAX_LENGTH` in this scope
  --> ./test/1.rs:19:30
   |
19 | impl<CellIdx> RobinHashTable<MAX_LENGTH, CellIdx> {
   |                              ^^^^^^^^^^ not found in this scope
   |
help: you might be missing a type parameter
   |
19 | impl<CellIdx, MAX_LENGTH> RobinHashTable<MAX_LENGTH, CellIdx> {
   |             ++++++++++++

error[E0574]: expected struct, variant or union type, found type parameter `CellIdx`
  --> ./test/1.rs:22:19
   |
19 | impl<CellIdx> RobinHashTable<MAX_LENGTH, CellIdx> {
   |      ------- found this type parameter
...
22 |             _idx: CellIdx { MAX_LENGTH },
   |                   ^^^^^^^ not a struct, variant or union type

error[E0425]: cannot find value `MAX_LENGTH` in this scope
  --> ./test/1.rs:22:29
   |
22 |             _idx: CellIdx { MAX_LENGTH },
   |                             ^^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `HashMapBase` in this scope
  --> ./test/1.rs:26:15
   |
26 | impl<CellIdx> HashMapBase<CellIdx> {
   |               ^^^^^^^^^^^ not found in this scope

error[E0574]: expected struct, variant or union type, found type parameter `CellIdx`
  --> ./test/1.rs:29:19
   |
26 | impl<CellIdx> HashMapBase<CellIdx> {
   |      ------- found this type parameter
...
29 |             _idx: CellIdx { 0 },
   |                   ^^^^^^^ not a struct, variant or union type

error[E0405]: cannot find trait `HashMapBase` in this scope
  --> ./test/1.rs:33:15
   |
33 | impl<CellIdx> HashMapBase<CellIdx> for RobinHashTable<MAX_LENGTH, CellIdx> {
   |               ^^^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `MAX_LENGTH` in this scope
  --> ./test/1.rs:33:55
   |
33 | impl<CellIdx> HashMapBase<CellIdx> for RobinHashTable<MAX_LENGTH, CellIdx> {
   |                                                       ^^^^^^^^^^ not found in this scope
   |
help: you might be missing a type parameter
   |
33 | impl<CellIdx, MAX_LENGTH> HashMapBase<CellIdx> for RobinHashTable<MAX_LENGTH, CellIdx> {
   |             ++++++++++++

error[E0404]: expected trait, found derive macro `Hash`
  --> ./test/1.rs:34:16
   |
34 |     fn hash<H: Hash + Hasher>(&self, 
   |                ^^^^ not a trait
   |
help: consider importing this trait instead
   |
3  + use std::hash::Hash;
   |

error[E0405]: cannot find trait `Hasher` in this scope
  --> ./test/1.rs:34:23
   |
34 |     fn hash<H: Hash + Hasher>(&self, 
   |                       ^^^^^^ not found in this scope
   |
help: consider importing this trait
   |
3  + use std::hash::Hasher;
   |

error[E0405]: cannot find trait `HashMapBase` in this scope
  --> ./test/1.rs:43:15
   |
43 | impl<CellIdx> HashMapBase<CellIdx> for RobinHashTable<MAX_LENGTH, CellIdx> {
   |               ^^^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `MAX_LENGTH` in this scope
  --> ./test/1.rs:43:55
   |
43 | impl<CellIdx> HashMapBase<CellIdx> for RobinHashTable<MAX_LENGTH, CellIdx> {
   |                                                       ^^^^^^^^^^ not found in this scope
   |
help: you might be missing a type parameter
   |
43 | impl<CellIdx, MAX_LENGTH> HashMapBase<CellIdx> for RobinHashTable<MAX_LENGTH, CellIdx> {
   |             ++++++++++++

error[E0404]: expected trait, found derive macro `Hash`
  --> ./test/1.rs:44:16
   |
44 |     fn hash<H: Hash + Hasher>(&self, other: &Self) -> H {
   |                ^^^^ not a trait
   |
help: consider importing this trait instead
   |
3  + use std::hash::Hash;
   |

error[E0405]: cannot find trait `Hasher` in this scope
  --> ./test/1.rs:44:23
   |
44 |     fn hash<H: Hash + Hasher>(&self, other: &Self) -> H {
   |                       ^^^^^^ not found in this scope
   |
help: consider importing this trait
   |
3  + use std::hash::Hasher;
   |

error[E0601]: `main` function not found in crate `1`
  --> ./test/1.rs:56:2
   |
56 | }
   |  ^ consider adding a `main` function to `./test/1.rs`

error[E0747]: unresolved item provided when a constant was expected
  --> ./test/1.rs:19:30
   |
19 | impl<CellIdx> RobinHashTable<MAX_LENGTH, CellIdx> {
   |                              ^^^^^^^^^^
   |
help: if this generic argument was intended as a const parameter, surround it with braces
   |
19 | impl<CellIdx> RobinHashTable<{ MAX_LENGTH }, CellIdx> {
   |                              +            +

error: internal compiler error: compiler/rustc_middle/src/ty/generic_args.rs:910:9: type parameter `CellIdx/#1` (CellIdx/#1/1) out of range when substituting, args=[{const error}: usize]
```

<!--
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 /rustc/960754090acc9cdd2a5a57586f244c0fc712d26c/compiler/rustc_errors/src/lib.rs:1656:9:
Box<dyn Any>
stack backtrace:
   0:     0x7fbb2c01687c - std::backtrace_rs::backtrace::libunwind::trace::hb61da1a1684055c4
                               at /rustc/960754090acc9cdd2a5a57586f244c0fc712d26c/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7fbb2c01687c - std::backtrace_rs::backtrace::trace_unsynchronized::hfba1eca2979dce84
                               at /rustc/960754090acc9cdd2a5a57586f244c0fc712d26c/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7fbb2c01687c - std::sys_common::backtrace::_print_fmt::h8f5555fce463d91d
                               at /rustc/960754090acc9cdd2a5a57586f244c0fc712d26c/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x7fbb2c01687c - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h85a4773f8b2e5329
                               at /rustc/960754090acc9cdd2a5a57586f244c0fc712d26c/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7fbb2c07d230 - core::fmt::rt::Argument::fmt::hcbdbafc573dfcac0
                               at /rustc/960754090acc9cdd2a5a57586f244c0fc712d26c/library/core/src/fmt/rt.rs:142:9
   5:     0x7fbb2c07d230 - core::fmt::write::hfe29e619fd6dae8f
                               at /rustc/960754090acc9cdd2a5a57586f244c0fc712d26c/library/core/src/fmt/mod.rs:1117:17
   6:     0x7fbb2c00988f - std::io::Write::write_fmt::h954d21bd4916c1a7
                               at /rustc/960754090acc9cdd2a5a57586f244c0fc712d26c/library/std/src/io/mod.rs:1763:15
   7:     0x7fbb2c016664 - std::sys_common::backtrace::_print::h49b5ea37c9310a62
                               at /rustc/960754090acc9cdd2a5a57586f244c0fc712d26c/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7fbb2c016664 - std::sys_common::backtrace::print::h53fdbe2d04672199
                               at /rustc/960754090acc9cdd2a5a57586f244c0fc712d26c/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7fbb2c019477 - std::panicking::default_hook::{{closure}}::h8b406ee6bba8d916
  10:     0x7fbb2c0191ba - std::panicking::default_hook::hce42dbd3c47cd760
                               at /rustc/960754090acc9cdd2a5a57586f244c0fc712d26c/library/std/src/panicking.rs:292:9
  11:     0x7fbb2b10c025 - std[4ac157f6623d00f2]::panicking::update_hook::<alloc[294f366240cefd1b]::boxed::Box<rustc_driver_impl[bd09ca8c49181cfd]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7fbb2c019c18 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hdde241eca81a2601
                               at /rustc/960754090acc9cdd2a5a57586f244c0fc712d26c/library/alloc/src/boxed.rs:2021:9
  13:     0x7fbb2c019c18 - std::panicking::rust_panic_with_hook::h10be2dd5a87447cb
                               at /rustc/960754090acc9cdd2a5a57586f244c0fc712d26c/library/std/src/panicking.rs:735:13
  14:     0x7fbb2b4c4084 - std[4ac157f6623d00f2]::panicking::begin_panic::<rustc_errors[adbb6fc482faea87]::ExplicitBug>::{closure#0}
  15:     0x7fbb2b4b6106 - std[4ac157f6623d00f2]::sys_common::backtrace::__rust_end_short_backtrace::<std[4ac157f6623d00f2]::panicking::begin_panic<rustc_errors[adbb6fc482faea87]::ExplicitBug>::{closure#0}, !>
  16:     0x7fbb2b4a51e6 - std[4ac157f6623d00f2]::panicking::begin_panic::<rustc_errors[adbb6fc482faea87]::ExplicitBug>
  17:     0x7fbb2b49d2f4 - <rustc_errors[adbb6fc482faea87]::HandlerInner>::bug::<alloc[294f366240cefd1b]::string::String>
  18:     0x7fbb2b49d0b4 - <rustc_errors[adbb6fc482faea87]::Handler>::bug::<alloc[294f366240cefd1b]::string::String>
  19:     0x7fbb2b540bfd - rustc_middle[2434184eb823d08a]::util::bug::opt_span_bug_fmt::<rustc_span[f9e4ea17963693be]::span_encoding::Span>::{closure#0}
  20:     0x7fbb2b526f0a - rustc_middle[2434184eb823d08a]::ty::context::tls::with_opt::<rustc_middle[2434184eb823d08a]::util::bug::opt_span_bug_fmt<rustc_span[f9e4ea17963693be]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  21:     0x7fbb2b526de8 - rustc_middle[2434184eb823d08a]::ty::context::tls::with_context_opt::<rustc_middle[2434184eb823d08a]::ty::context::tls::with_opt<rustc_middle[2434184eb823d08a]::util::bug::opt_span_bug_fmt<rustc_span[f9e4ea17963693be]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  22:     0x7fbb29576470 - rustc_middle[2434184eb823d08a]::util::bug::bug_fmt
  23:     0x7fbb2b535716 - <rustc_middle[2434184eb823d08a]::ty::generic_args::ArgFolder>::type_param_out_of_range
  24:     0x7fbb29427d23 - <rustc_middle[2434184eb823d08a]::ty::generic_args::ArgFolder as rustc_type_ir[d79ce18dcaf65c70]::fold::TypeFolder<rustc_middle[2434184eb823d08a]::ty::context::TyCtxt>>::fold_ty
  25:     0x7fbb29459e41 - <&rustc_middle[2434184eb823d08a]::ty::list::List<rustc_middle[2434184eb823d08a]::ty::generic_args::GenericArg> as rustc_type_ir[d79ce18dcaf65c70]::fold::TypeFoldable<rustc_middle[2434184eb823d08a]::ty::context::TyCtxt>>::try_fold_with::<rustc_middle[2434184eb823d08a]::ty::generic_args::ArgFolder>
  26:     0x7fbb29429958 - <rustc_middle[2434184eb823d08a]::ty::generic_args::ArgFolder as rustc_type_ir[d79ce18dcaf65c70]::fold::TypeFolder<rustc_middle[2434184eb823d08a]::ty::context::TyCtxt>>::fold_const
  27:     0x7fbb29426492 - <rustc_middle[2434184eb823d08a]::ty::generic_args::ArgFolder as rustc_type_ir[d79ce18dcaf65c70]::fold::TypeFolder<rustc_middle[2434184eb823d08a]::ty::context::TyCtxt>>::fold_ty
  28:     0x7fbb29421cbf - <rustc_middle[2434184eb823d08a]::ty::generic_args::ArgFolder as rustc_type_ir[d79ce18dcaf65c70]::fold::TypeFolder<rustc_middle[2434184eb823d08a]::ty::context::TyCtxt>>::fold_ty
  29:     0x7fbb29e17ac7 - <dyn rustc_hir_analysis[a9631556c65800fa]::astconv::AstConv>::res_to_ty
  30:     0x7fbb29e11691 - <dyn rustc_hir_analysis[a9631556c65800fa]::astconv::AstConv>::ast_ty_to_ty_inner::{closure#0}
  31:     0x7fbb29b611c8 - rustc_hir_analysis[a9631556c65800fa]::collect::type_of::type_of
  32:     0x7fbb29737925 - rustc_query_impl[de4d3dc93f970998]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[de4d3dc93f970998]::query_impl::type_of::dynamic_query::{closure#2}::{closure#0}, rustc_middle[2434184eb823d08a]::query::erase::Erased<[u8; 8usize]>>
  33:     0x7fbb29788621 - rustc_query_system[2fcca6536f793a9d]::query::plumbing::try_execute_query::<rustc_query_impl[de4d3dc93f970998]::DynamicConfig<rustc_query_system[2fcca6536f793a9d]::query::caches::DefaultCache<rustc_span[f9e4ea17963693be]::def_id::DefId, rustc_middle[2434184eb823d08a]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[de4d3dc93f970998]::plumbing::QueryCtxt, false>
  34:     0x7fbb2abc817f - rustc_query_impl[de4d3dc93f970998]::query_impl::type_of::get_query_non_incr::__rust_end_short_backtrace
  35:     0x7fbb2a4e5c19 - <rustc_hir_analysis[a9631556c65800fa]::collect::CollectItemTypesVisitor as rustc_hir[efad412a3ca7eda7]::intravisit::Visitor>::visit_item
  36:     0x7fbb2a4e4cf8 - rustc_hir_analysis[a9631556c65800fa]::collect::collect_mod_item_types
  37:     0x7fbb2a606977 - rustc_query_impl[de4d3dc93f970998]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[de4d3dc93f970998]::query_impl::collect_mod_item_types::dynamic_query::{closure#2}::{closure#0}, rustc_middle[2434184eb823d08a]::query::erase::Erased<[u8; 0usize]>>
  38:     0x7fbb2a6066b4 - rustc_query_system[2fcca6536f793a9d]::query::plumbing::try_execute_query::<rustc_query_impl[de4d3dc93f970998]::DynamicConfig<rustc_query_system[2fcca6536f793a9d]::query::caches::DefaultCache<rustc_span[f9e4ea17963693be]::def_id::LocalModDefId, rustc_middle[2434184eb823d08a]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[de4d3dc93f970998]::plumbing::QueryCtxt, false>
  39:     0x7fbb2abd35d7 - rustc_query_impl[de4d3dc93f970998]::query_impl::collect_mod_item_types::get_query_non_incr::__rust_end_short_backtrace
  40:     0x7fbb2a7933a6 - rustc_hir_analysis[a9631556c65800fa]::check_crate
  41:     0x7fbb2a78959a - rustc_interface[f4e43bc9ecbe2dc8]::passes::analysis
  42:     0x7fbb2a9cd527 - rustc_query_impl[de4d3dc93f970998]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[de4d3dc93f970998]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[2434184eb823d08a]::query::erase::Erased<[u8; 1usize]>>
  43:     0x7fbb2a9cd116 - rustc_query_system[2fcca6536f793a9d]::query::plumbing::try_execute_query::<rustc_query_impl[de4d3dc93f970998]::DynamicConfig<rustc_query_system[2fcca6536f793a9d]::query::caches::SingleCache<rustc_middle[2434184eb823d08a]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[de4d3dc93f970998]::plumbing::QueryCtxt, false>
  44:     0x7fbb2a9ccecd - rustc_query_impl[de4d3dc93f970998]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  45:     0x7fbb2a8e731d - std[4ac157f6623d00f2]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[f4e43bc9ecbe2dc8]::util::run_in_thread_with_globals<rustc_interface[f4e43bc9ecbe2dc8]::interface::run_compiler<core[abb4237e6a0cf505]::result::Result<(), rustc_span[f9e4ea17963693be]::ErrorGuaranteed>, rustc_driver_impl[bd09ca8c49181cfd]::run_compiler::{closure#1}>::{closure#0}, core[abb4237e6a0cf505]::result::Result<(), rustc_span[f9e4ea17963693be]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[abb4237e6a0cf505]::result::Result<(), rustc_span[f9e4ea17963693be]::ErrorGuaranteed>>
  46:     0x7fbb2a8e65d3 - <<std[4ac157f6623d00f2]::thread::Builder>::spawn_unchecked_<rustc_interface[f4e43bc9ecbe2dc8]::util::run_in_thread_with_globals<rustc_interface[f4e43bc9ecbe2dc8]::interface::run_compiler<core[abb4237e6a0cf505]::result::Result<(), rustc_span[f9e4ea17963693be]::ErrorGuaranteed>, rustc_driver_impl[bd09ca8c49181cfd]::run_compiler::{closure#1}>::{closure#0}, core[abb4237e6a0cf505]::result::Result<(), rustc_span[f9e4ea17963693be]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[abb4237e6a0cf505]::result::Result<(), rustc_span[f9e4ea17963693be]::ErrorGuaranteed>>::{closure#1} as core[abb4237e6a0cf505]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  47:     0x7fbb2c024b95 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hb5c04f04e18e9df2
                               at /rustc/960754090acc9cdd2a5a57586f244c0fc712d26c/library/alloc/src/boxed.rs:2007:9
  48:     0x7fbb2c024b95 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hd8aeab4522891881
                               at /rustc/960754090acc9cdd2a5a57586f244c0fc712d26c/library/alloc/src/boxed.rs:2007:9
  49:     0x7fbb2c024b95 - std::sys::unix::thread::Thread::new::thread_start::h8aff1b302695caf4
                               at /rustc/960754090acc9cdd2a5a57586f244c0fc712d26c/library/std/src/sys/unix/thread.rs:108:17
  50:     0x7fbb27ff46db - start_thread
                               at /build/glibc-CVJwZb/glibc-2.27/nptl/pthread_create.c:463
  51:     0x7fbb27d1d61f - clone
                               at /build/glibc-CVJwZb/glibc-2.27/misc/../sysdeps/unix/sysv/linux/x86_64/clone.S:95
  52:                0x0 - <unknown>

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 attach the file at `/home/Ghy/LLM/rustc-ice-2023-10-07T16:07:07.328537474Z-17218.txt` to your bug report

query stack during panic:
#0 [type_of] computing type of `<impl at ./test/1.rs:19:1: 19:50>`
#1 [collect_mod_item_types] collecting item types in top-level module
#2 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 17 previous errors

Some errors have detailed explanations: E0404, E0405, E0412, E0425, E0574, E0601, E0747.
For more information about an error, try `rustc --explain E0404`.
```

</p>
</details>