Repository: rust
Issue: Compiler crash: type parameter `D/#3` (D/#3/3) out of range when substituting (project with feature generic_const_exprs)
Labels: I-ICE,T-compiler,C-bug,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/
-->

When I add the default generic parameter then the error about that part goes away, but the crash remains.

### Code

This is a very broken intermediate state where I try to replace a field `data: Box<[T; X * Y]>` with a generic field that has `AsRef<[T; X * Y]>`.

<https://github.com/panzi/rust-matrix/commit/64fecf7b75eeea7b50f01a5779a3c08d942474cf>

### 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 (31bc7e2c4 2023-10-30)
binary: rustc
commit-hash: 31bc7e2c47e82798a392c770611975a6883132c8
commit-date: 2023-10-30
host: x86_64-unknown-linux-gnu
release: 1.75.0-nightly
LLVM version: 17.0.3
```

### Error output

```
error: generic parameters with a default must be trailing
  --> src/bycolumn.rs:11:57
   |
11 | pub struct IntoByColumn<const X: usize, const Y: usize, T: Number=f64, D: AsRef<[T; X * Y]>>
   |                                                         ^

error: internal compiler error: compiler/rustc_middle/src/ty/generic_args.rs:905:9: type parameter `D/#3` (D/#3/3) out of range when substituting, args=[X/#1: usize, Y/#2: usize, T/#3]
```

<!--
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: generic parameters with a default must be trailing
  --> src/bycolumn.rs:11:57
   |
11 | pub struct IntoByColumn<const X: usize, const Y: usize, T: Number=f64, D: AsRef<[T; X * Y]>>
   |                                                         ^

error: internal compiler error: compiler/rustc_middle/src/ty/generic_args.rs:905:9: type parameter `D/#3` (D/#3/3) out of range when substituting, args=[X/#1: usize, Y/#2: usize, T/#3]

thread 'rustc' panicked at /rustc/31bc7e2c47e82798a392c770611975a6883132c8/compiler/rustc_errors/src/lib.rs:1661:9:
Box<dyn Any>
stack backtrace:
   0:     0x7f837f03acfc - std::backtrace_rs::backtrace::libunwind::trace::h974857262e179b17
                               at /rustc/31bc7e2c47e82798a392c770611975a6883132c8/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5
   1:     0x7f837f03acfc - std::backtrace_rs::backtrace::trace_unsynchronized::h6adce2053c056ac9
                               at /rustc/31bc7e2c47e82798a392c770611975a6883132c8/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f837f03acfc - std::sys_common::backtrace::_print_fmt::hcf2018a5ad956761
                               at /rustc/31bc7e2c47e82798a392c770611975a6883132c8/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x7f837f03acfc - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h3d39e48ce4d7e36f
                               at /rustc/31bc7e2c47e82798a392c770611975a6883132c8/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f837f09cf50 - core::fmt::rt::Argument::fmt::hef0ae15422a7f61d
                               at /rustc/31bc7e2c47e82798a392c770611975a6883132c8/library/core/src/fmt/rt.rs:142:9
   5:     0x7f837f09cf50 - core::fmt::write::hf465a4ba34489409
                               at /rustc/31bc7e2c47e82798a392c770611975a6883132c8/library/core/src/fmt/mod.rs:1117:17
   6:     0x7f837f02ec1f - std::io::Write::write_fmt::hf34ad5986fa99e89
                               at /rustc/31bc7e2c47e82798a392c770611975a6883132c8/library/std/src/io/mod.rs:1763:15
   7:     0x7f837f03aae4 - std::sys_common::backtrace::_print::h5aa7f1d99916c125
                               at /rustc/31bc7e2c47e82798a392c770611975a6883132c8/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7f837f03aae4 - std::sys_common::backtrace::print::h036d85b5c81889eb
                               at /rustc/31bc7e2c47e82798a392c770611975a6883132c8/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7f837f03d777 - std::panicking::default_hook::{{closure}}::h9b311bcc8d2b2a40
  10:     0x7f837f03d4df - std::panicking::default_hook::hd7a03fd029ce4ee2
                               at /rustc/31bc7e2c47e82798a392c770611975a6883132c8/library/std/src/panicking.rs:292:9
  11:     0x7f8381bb16a0 - std[d7fc13cc5242579e]::panicking::update_hook::<alloc[46fb3a2f92666e24]::boxed::Box<rustc_driver_impl[88c66c37425cba72]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7f837f03deb8 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h079168172b9ee18d
                               at /rustc/31bc7e2c47e82798a392c770611975a6883132c8/library/alloc/src/boxed.rs:2021:9
  13:     0x7f837f03deb8 - std::panicking::rust_panic_with_hook::h9ec5267af19ebd61
                               at /rustc/31bc7e2c47e82798a392c770611975a6883132c8/library/std/src/panicking.rs:783:13
  14:     0x7f8381f79614 - std[d7fc13cc5242579e]::panicking::begin_panic::<rustc_errors[b15781973272acfb]::ExplicitBug>::{closure#0}
  15:     0x7f8381f70df6 - std[d7fc13cc5242579e]::sys_common::backtrace::__rust_end_short_backtrace::<std[d7fc13cc5242579e]::panicking::begin_panic<rustc_errors[b15781973272acfb]::ExplicitBug>::{closure#0}, !>
  16:     0x7f8381f6b936 - std[d7fc13cc5242579e]::panicking::begin_panic::<rustc_errors[b15781973272acfb]::ExplicitBug>
  17:     0x7f8381f53154 - <rustc_errors[b15781973272acfb]::HandlerInner>::bug::<alloc[46fb3a2f92666e24]::string::String>
  18:     0x7f8381f52f14 - <rustc_errors[b15781973272acfb]::Handler>::bug::<alloc[46fb3a2f92666e24]::string::String>
  19:     0x7f8381ff322d - rustc_middle[604fc891fd74f2d8]::util::bug::opt_span_bug_fmt::<rustc_span[88faddf4715d2d43]::span_encoding::Span>::{closure#0}
  20:     0x7f8381fdb68a - rustc_middle[604fc891fd74f2d8]::ty::context::tls::with_opt::<rustc_middle[604fc891fd74f2d8]::util::bug::opt_span_bug_fmt<rustc_span[88faddf4715d2d43]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  21:     0x7f8381fdb508 - rustc_middle[604fc891fd74f2d8]::ty::context::tls::with_context_opt::<rustc_middle[604fc891fd74f2d8]::ty::context::tls::with_opt<rustc_middle[604fc891fd74f2d8]::util::bug::opt_span_bug_fmt<rustc_span[88faddf4715d2d43]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  22:     0x7f838009ae30 - rustc_middle[604fc891fd74f2d8]::util::bug::bug_fmt
  23:     0x7f8381fe9a06 - <rustc_middle[604fc891fd74f2d8]::ty::generic_args::ArgFolder>::type_param_out_of_range
  24:     0x7f8382cd8bdd - <rustc_middle[604fc891fd74f2d8]::ty::generic_args::ArgFolder as rustc_type_ir[d22dfb366bb74f41]::fold::TypeFolder<rustc_middle[604fc891fd74f2d8]::ty::context::TyCtxt>>::fold_ty
  25:     0x7f8382d7b858 - <&rustc_middle[604fc891fd74f2d8]::ty::list::List<rustc_middle[604fc891fd74f2d8]::ty::generic_args::GenericArg> as rustc_type_ir[d22dfb366bb74f41]::fold::TypeFoldable<rustc_middle[604fc891fd74f2d8]::ty::context::TyCtxt>>::try_fold_with::<rustc_middle[604fc891fd74f2d8]::ty::generic_args::ArgFolder>
  26:     0x7f8382d7c5d6 - <rustc_middle[604fc891fd74f2d8]::ty::generic_args::ArgFolder as rustc_type_ir[d22dfb366bb74f41]::fold::TypeFolder<rustc_middle[604fc891fd74f2d8]::ty::context::TyCtxt>>::fold_const
  27:     0x7f8382cd7820 - <rustc_middle[604fc891fd74f2d8]::ty::generic_args::ArgFolder as rustc_type_ir[d22dfb366bb74f41]::fold::TypeFolder<rustc_middle[604fc891fd74f2d8]::ty::context::TyCtxt>>::fold_ty
  28:     0x7f8382cd3a48 - <rustc_middle[604fc891fd74f2d8]::ty::generic_args::ArgFolder as rustc_type_ir[d22dfb366bb74f41]::fold::TypeFolder<rustc_middle[604fc891fd74f2d8]::ty::context::TyCtxt>>::fold_ty
  29:     0x7f838310197e - <<dyn rustc_hir_analysis[7dbe6ed907447fd9]::astconv::AstConv>::create_args_for_ast_path::{closure#0}::SubstsForAstPathCtxt as rustc_hir_analysis[7dbe6ed907447fd9]::astconv::CreateSubstsForGenericArgsCtxt>::inferred_kind
  30:     0x7f8380893313 - <dyn rustc_hir_analysis[7dbe6ed907447fd9]::astconv::AstConv>::res_to_ty
  31:     0x7f8382e04e5b - <dyn rustc_hir_analysis[7dbe6ed907447fd9]::astconv::AstConv>::ast_ty_to_ty_inner
  32:     0x7f8382e05399 - <dyn rustc_hir_analysis[7dbe6ed907447fd9]::astconv::AstConv>::ast_ty_to_ty_inner
  33:     0x7f83802e624b - rustc_hir_analysis[7dbe6ed907447fd9]::collect::type_of::type_of
  34:     0x7f8382d30b69 - rustc_query_impl[688c8a72794c2a04]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[688c8a72794c2a04]::query_impl::type_of::dynamic_query::{closure#2}::{closure#0}, rustc_middle[604fc891fd74f2d8]::query::erase::Erased<[u8; 8usize]>>
  35:     0x7f8382eeff29 - rustc_query_system[92afb14951a5a4cf]::query::plumbing::try_execute_query::<rustc_query_impl[688c8a72794c2a04]::DynamicConfig<rustc_query_system[92afb14951a5a4cf]::query::caches::DefaultCache<rustc_span[88faddf4715d2d43]::def_id::DefId, rustc_middle[604fc891fd74f2d8]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[688c8a72794c2a04]::plumbing::QueryCtxt, true>
  36:     0x7f8382efece9 - rustc_query_impl[688c8a72794c2a04]::query_impl::type_of::get_query_incr::__rust_end_short_backtrace
  37:     0x7f8382dddcf7 - rustc_middle[604fc891fd74f2d8]::query::plumbing::query_get_at::<rustc_query_system[92afb14951a5a4cf]::query::caches::DefaultCache<rustc_span[88faddf4715d2d43]::def_id::DefId, rustc_middle[604fc891fd74f2d8]::query::erase::Erased<[u8; 8usize]>>>
  38:     0x7f83838ddaa3 - rustc_hir_analysis[7dbe6ed907447fd9]::outlives::inferred_outlives_crate
  39:     0x7f83837da221 - rustc_query_impl[688c8a72794c2a04]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[688c8a72794c2a04]::query_impl::inferred_outlives_crate::dynamic_query::{closure#2}::{closure#0}, rustc_middle[604fc891fd74f2d8]::query::erase::Erased<[u8; 8usize]>>
  40:     0x7f8383bc1ef2 - rustc_query_system[92afb14951a5a4cf]::query::plumbing::try_execute_query::<rustc_query_impl[688c8a72794c2a04]::DynamicConfig<rustc_query_system[92afb14951a5a4cf]::query::caches::SingleCache<rustc_middle[604fc891fd74f2d8]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[688c8a72794c2a04]::plumbing::QueryCtxt, true>
  41:     0x7f8383cf35cc - rustc_query_impl[688c8a72794c2a04]::query_impl::inferred_outlives_crate::get_query_incr::__rust_end_short_backtrace
  42:     0x7f8382e3f0ca - rustc_hir_analysis[7dbe6ed907447fd9]::outlives::inferred_outlives_of
  43:     0x7f8382e3e18c - rustc_query_impl[688c8a72794c2a04]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[688c8a72794c2a04]::query_impl::inferred_outlives_of::dynamic_query::{closure#2}::{closure#0}, rustc_middle[604fc891fd74f2d8]::query::erase::Erased<[u8; 16usize]>>
  44:     0x7f838312ed5d - rustc_query_system[92afb14951a5a4cf]::query::plumbing::try_execute_query::<rustc_query_impl[688c8a72794c2a04]::DynamicConfig<rustc_query_system[92afb14951a5a4cf]::query::caches::DefaultCache<rustc_span[88faddf4715d2d43]::def_id::DefId, rustc_middle[604fc891fd74f2d8]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[688c8a72794c2a04]::plumbing::QueryCtxt, true>
  45:     0x7f838312d518 - rustc_query_impl[688c8a72794c2a04]::query_impl::inferred_outlives_of::get_query_incr::__rust_end_short_backtrace
  46:     0x7f8382f9f19c - rustc_hir_analysis[7dbe6ed907447fd9]::collect::predicates_defined_on
  47:     0x7f8382f9efd3 - rustc_query_impl[688c8a72794c2a04]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[688c8a72794c2a04]::query_impl::predicates_defined_on::dynamic_query::{closure#2}::{closure#0}, rustc_middle[604fc891fd74f2d8]::query::erase::Erased<[u8; 24usize]>>
  48:     0x7f8383258041 - rustc_query_system[92afb14951a5a4cf]::query::plumbing::try_execute_query::<rustc_query_impl[688c8a72794c2a04]::DynamicConfig<rustc_query_system[92afb14951a5a4cf]::query::caches::DefaultCache<rustc_span[88faddf4715d2d43]::def_id::DefId, rustc_middle[604fc891fd74f2d8]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[688c8a72794c2a04]::plumbing::QueryCtxt, true>
  49:     0x7f8383256b95 - rustc_query_impl[688c8a72794c2a04]::query_impl::predicates_defined_on::get_query_incr::__rust_end_short_backtrace
  50:     0x7f8382f9d629 - rustc_hir_analysis[7dbe6ed907447fd9]::collect::predicates_of::predicates_of
  51:     0x7f8382f9d513 - rustc_query_impl[688c8a72794c2a04]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[688c8a72794c2a04]::query_impl::predicates_of::dynamic_query::{closure#2}::{closure#0}, rustc_middle[604fc891fd74f2d8]::query::erase::Erased<[u8; 24usize]>>
  52:     0x7f8383258003 - rustc_query_system[92afb14951a5a4cf]::query::plumbing::try_execute_query::<rustc_query_impl[688c8a72794c2a04]::DynamicConfig<rustc_query_system[92afb14951a5a4cf]::query::caches::DefaultCache<rustc_span[88faddf4715d2d43]::def_id::DefId, rustc_middle[604fc891fd74f2d8]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[688c8a72794c2a04]::plumbing::QueryCtxt, true>
  53:     0x7f8382efe1b6 - rustc_query_impl[688c8a72794c2a04]::query_impl::predicates_of::get_query_incr::__rust_end_short_backtrace
  54:     0x7f838314c3f4 - <rustc_hir_analysis[7dbe6ed907447fd9]::collect::CollectItemTypesVisitor as rustc_hir[b161d6d6375c5bc6]::intravisit::Visitor>::visit_item
  55:     0x7f838314aea8 - rustc_hir_analysis[7dbe6ed907447fd9]::collect::collect_mod_item_types
  56:     0x7f838314ae3d - rustc_query_impl[688c8a72794c2a04]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[688c8a72794c2a04]::query_impl::collect_mod_item_types::dynamic_query::{closure#2}::{closure#0}, rustc_middle[604fc891fd74f2d8]::query::erase::Erased<[u8; 0usize]>>
  57:     0x7f83834ed629 - rustc_query_system[92afb14951a5a4cf]::query::plumbing::try_execute_query::<rustc_query_impl[688c8a72794c2a04]::DynamicConfig<rustc_query_system[92afb14951a5a4cf]::query::caches::DefaultCache<rustc_span[88faddf4715d2d43]::def_id::LocalModDefId, rustc_middle[604fc891fd74f2d8]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[688c8a72794c2a04]::plumbing::QueryCtxt, true>
  58:     0x7f8383198640 - rustc_query_impl[688c8a72794c2a04]::query_impl::collect_mod_item_types::get_query_incr::__rust_end_short_backtrace
  59:     0x7f83831988e6 - rustc_hir_analysis[7dbe6ed907447fd9]::check_crate
  60:     0x7f83834db516 - rustc_interface[90d65b0f51490bf6]::passes::analysis
  61:     0x7f83834daf35 - rustc_query_impl[688c8a72794c2a04]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[688c8a72794c2a04]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[604fc891fd74f2d8]::query::erase::Erased<[u8; 1usize]>>
  62:     0x7f83837c3844 - rustc_query_system[92afb14951a5a4cf]::query::plumbing::try_execute_query::<rustc_query_impl[688c8a72794c2a04]::DynamicConfig<rustc_query_system[92afb14951a5a4cf]::query::caches::SingleCache<rustc_middle[604fc891fd74f2d8]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[688c8a72794c2a04]::plumbing::QueryCtxt, true>
  63:     0x7f83837c3520 - rustc_query_impl[688c8a72794c2a04]::query_impl::analysis::get_query_incr::__rust_end_short_backtrace
  64:     0x7f8383a14643 - std[d7fc13cc5242579e]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[90d65b0f51490bf6]::util::run_in_thread_with_globals<rustc_interface[90d65b0f51490bf6]::interface::run_compiler<core[53bdee0566a68c19]::result::Result<(), rustc_span[88faddf4715d2d43]::ErrorGuaranteed>, rustc_driver_impl[88c66c37425cba72]::run_compiler::{closure#1}>::{closure#0}, core[53bdee0566a68c19]::result::Result<(), rustc_span[88faddf4715d2d43]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[53bdee0566a68c19]::result::Result<(), rustc_span[88faddf4715d2d43]::ErrorGuaranteed>>
  65:     0x7f8383a13909 - <<std[d7fc13cc5242579e]::thread::Builder>::spawn_unchecked_<rustc_interface[90d65b0f51490bf6]::util::run_in_thread_with_globals<rustc_interface[90d65b0f51490bf6]::interface::run_compiler<core[53bdee0566a68c19]::result::Result<(), rustc_span[88faddf4715d2d43]::ErrorGuaranteed>, rustc_driver_impl[88c66c37425cba72]::run_compiler::{closure#1}>::{closure#0}, core[53bdee0566a68c19]::result::Result<(), rustc_span[88faddf4715d2d43]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[53bdee0566a68c19]::result::Result<(), rustc_span[88faddf4715d2d43]::ErrorGuaranteed>>::{closure#1} as core[53bdee0566a68c19]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  66:     0x7f837f048c85 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::he310d06a8f297797
                               at /rustc/31bc7e2c47e82798a392c770611975a6883132c8/library/alloc/src/boxed.rs:2007:9
  67:     0x7f837f048c85 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h69350f715ad37c20
                               at /rustc/31bc7e2c47e82798a392c770611975a6883132c8/library/alloc/src/boxed.rs:2007:9
  68:     0x7f837f048c85 - std::sys::unix::thread::Thread::new::thread_start::h7310dea1d780d6a0
                               at /rustc/31bc7e2c47e82798a392c770611975a6883132c8/library/std/src/sys/unix/thread.rs:108:17
  69:     0x7f837eefd3f9 - start_thread
  70:     0x7f837ee284c3 - clone
  71:                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/panzi/src/rust/matrix/rustc-ice-2023-10-31T19_27_35-16286.txt` to your bug report

note: compiler flags: --crate-type lib -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [type_of] computing type of `bycolumn::ByColumn::matrix`
#1 [inferred_outlives_crate] computing the inferred outlives predicates for items in this crate
#2 [inferred_outlives_of] computing inferred outlives predicates of `vector::Vector`
#3 [predicates_defined_on] computing predicates of `vector::Vector`
#4 [predicates_of] computing predicates of `vector::Vector`
#5 [collect_mod_item_types] collecting item types in module `vector`
#6 [analysis] running analysis passes on this crate
end of query stack
error: could not compile `matrix` (lib) due to previous error
```

</p>
</details>