[ICE]: Error when using `generic_const_expr` inside type in combination with `map(...)`
Open
Nobody has claimed this yet.
A-closures
C-bug
F-generic_const_exprs
I-ICE
P-low
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I am trying to implement a custom Matrix type for a cryptographic scheme. I would like to use arrays for the (known to be very small, ca. 2x2 and similar) matrices and use generic_const_exprs to handle and propagate the sizes. I encountered the following error when using
let xs = (0..2*msg_len).map(|_| Matrix::<Num, {2*K}, K>::default()).collect();
in line 35 (see variant A). For some reason, the same code works when using square matrices (variant B) or an explicit loop (variant C).
Adding an explicit type to collect::<...>() also does not help.
Code
#![feature(generic_const_exprs)]
#[derive(Debug, Default, Clone, Copy)]
pub struct Num(i64);
// #[derive(Debug)]
pub struct Matrix<T, const R: usize, const C: usize> {
pub entries: [[T; C]; R],
}
impl<T, const R: usize, const C: usize> Matrix<T, R, C>
where
T: Default + Copy,
{
pub fn default() -> Matrix<T, R, C> {
let entries = [[T::default(); C]; R];
Matrix { entries }
}
}
pub struct SecretKey<const K: usize>
where
[[Num; 2 * K]; K]: Sized,
{
pub xs: Vec<Matrix<Num, { 2 * K }, K>>, // variants A and C
// pub xs: Vec<Matrix<Num, K, K>>, // variant B
}
fn gen_mac<const K: usize>(msg_len: usize) -> SecretKey<K>
where
[[Num; 2 * K]; K]: Sized,
{
// variant (A): crashes rustc
let xs = (0..2*msg_len).map(|_| Matrix::<Num, {2*K}, K>::default()).collect();
// variant (B): works fine
// let xs = (0..2*msg_len).map(|_| Matrix::<Num, K, K>::default()).collect();
// variant (C): works fine
// let mut xs = Vec::with_capacity(2 * msg_len);
// for _ in 0..2 * msg_len {
// let tmp = Matrix::<Num, { 2 * K }, K>::default();
// xs.push(tmp);
// }
SecretKey { xs }
}
fn main() {
let msg_len = 128;
let sk: SecretKey<2> = gen_mac(msg_len);
println!("{:?}", sk.xs[0].entries[0][0].0);
}
Meta
rustc --version --verbose:
rustc 1.95.0-nightly (e96bb7e44 2026-01-27)
binary: rustc
commit-hash: e96bb7e44fbcc23c1e6009e8d0ee8ab208668fb4
commit-date: 2026-01-27
host: aarch64-apple-darwin
release: 1.95.0-nightly
LLVM version: 21.1.8
Error output
thread 'rustc' (163522) panicked at /rustc-dev/e96bb7e44fbcc23c1e6009e8d0ee8ab208668fb4/compiler/rustc_type_ir/src/binder.rs:784:9:
type parameter `<closure_kind>/#1` (<closure_kind>/#1/1) out of range when instantiating, args=[2_usize]
[...]
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 `/Users/sven/Scratch/rust-generic-const-expr-mwe/rustc-ice-2026-01-29T11_02_50-23713.txt` to your bug report
note: rustc 1.95.0-nightly (e96bb7e44 2026-01-27) running on aarch64-apple-darwin
note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked -C incremental=[REDACTED]
note: some of the compiler flags provided by cargo are hidden
query stack during panic:
#0 [items_of_instance] collecting items used by `gen_mac::<ValTree(Leaf(0x0000000000000002): usize)>`
#1 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack
warning: `rust-generic-const-expr-mwe` (bin "rust-generic-const-expr-mwe") generated 1 warning
error: could not compile `rust-generic-const-expr-mwe` (bin "rust-generic-const-expr-mwe"); 1 warning emitted
Caused by:
process didn't exit successfully: `/Users/sven/.rustup/toolchains/nightly-aarch64-apple-darwin/bin/rustc --crate-name rust_generic_const_expr_mwe --edition=2024 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=225 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values())' -C metadata=650d964325e10461 -C extra-filename=-0b8cdc7be243b639 --out-dir /Users/sven/Scratch/rust-generic-const-expr-mwe/target/debug/deps -C incremental=/Users/sven/Scratch/rust-generic-const-expr-mwe/target/debug/incremental -L dependency=/Users/sven/Scratch/rust-generic-const-expr-mwe/target/debug/deps` (exit status: 101)
Backtrace
thread 'rustc' (163522) panicked at /rustc-dev/e96bb7e44fbcc23c1e6009e8d0ee8ab208668fb4/compiler/rustc_type_ir/src/binder.rs:784:9:
type parameter `<closure_kind>/#1` (<closure_kind>/#1/1) out of range when instantiating, args=[2_usize]
stack backtrace:
0: 0x10ef9aa64 - <<std[4bc8c175d103129a]::sys::backtrace::BacktraceLock>::print::DisplayBacktrace as core[a9260f7fe7be6471]::fmt::Display>::fmt
1: 0x10c1bac88 - core[a9260f7fe7be6471]::fmt::write
2: 0x10efb1d78 - <std[4bc8c175d103129a]::sys::stdio::unix::Stderr as std[4bc8c175d103129a]::io::Write>::write_fmt
3: 0x10ef71430 - std[4bc8c175d103129a]::panicking::default_hook::{closure#0}
4: 0x10ef8d164 - std[4bc8c175d103129a]::panicking::default_hook
5: 0x10ce1efcc - std[4bc8c175d103129a]::panicking::update_hook::<alloc[ee9c6ec37ce30a84]::boxed::Box<rustc_driver_impl[fe8b8d4615f4f17e]::install_ice_hook::{closure#1}>>::{closure#0}
6: 0x10ef8d4c8 - std[4bc8c175d103129a]::panicking::panic_with_hook
7: 0x10ef714d8 - std[4bc8c175d103129a]::panicking::panic_handler::{closure#0}
8: 0x10ef65edc - std[4bc8c175d103129a]::sys::backtrace::__rust_end_short_backtrace::<std[4bc8c175d103129a]::panicking::panic_handler::{closure#0}, !>
9: 0x10ef72af0 - __rustc[3e792ca608042bd3]::rust_begin_unwind
10: 0x111ce77d0 - core[a9260f7fe7be6471]::panicking::panic_fmt
11: 0x111dc313c - <rustc_type_ir[5b4184059516088b]::binder::ArgFolder<rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt>>::type_param_out_of_range
12: 0x10dfe2f54 - <rustc_type_ir[5b4184059516088b]::binder::ArgFolder<rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt> as rustc_type_ir[5b4184059516088b]::fold::TypeFolder<rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt>>::fold_ty
13: 0x10dfb6e20 - <&rustc_middle[5ebf0ea49052770a]::ty::list::RawList<(), rustc_middle[5ebf0ea49052770a]::ty::generic_args::GenericArg> as rustc_type_ir[5b4184059516088b]::fold::TypeFoldable<rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt>>::fold_with::<rustc_type_ir[5b4184059516088b]::binder::ArgFolder<rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt>>
14: 0x10dfb5e34 - <rustc_middle[5ebf0ea49052770a]::ty::consts::Const as rustc_type_ir[5b4184059516088b]::fold::TypeSuperFoldable<rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt>>::super_fold_with::<rustc_type_ir[5b4184059516088b]::binder::ArgFolder<rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt>>
15: 0x10dfb6cac - <&rustc_middle[5ebf0ea49052770a]::ty::list::RawList<(), rustc_middle[5ebf0ea49052770a]::ty::generic_args::GenericArg> as rustc_type_ir[5b4184059516088b]::fold::TypeFoldable<rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt>>::fold_with::<rustc_type_ir[5b4184059516088b]::binder::ArgFolder<rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt>>
16: 0x10dfb9480 - <rustc_middle[5ebf0ea49052770a]::ty::Ty as rustc_type_ir[5b4184059516088b]::fold::TypeSuperFoldable<rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt>>::super_fold_with::<rustc_type_ir[5b4184059516088b]::binder::ArgFolder<rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt>>
17: 0x10dfb6c54 - <&rustc_middle[5ebf0ea49052770a]::ty::list::RawList<(), rustc_middle[5ebf0ea49052770a]::ty::generic_args::GenericArg> as rustc_type_ir[5b4184059516088b]::fold::TypeFoldable<rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt>>::fold_with::<rustc_type_ir[5b4184059516088b]::binder::ArgFolder<rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt>>
18: 0x10dfb93b8 - <rustc_middle[5ebf0ea49052770a]::ty::Ty as rustc_type_ir[5b4184059516088b]::fold::TypeSuperFoldable<rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt>>::super_fold_with::<rustc_type_ir[5b4184059516088b]::binder::ArgFolder<rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt>>
19: 0x10dfd886c - <rustc_monomorphize[1a1fb678af60a318]::collector::check_normalization_error::NormalizationChecker as rustc_type_ir[5b4184059516088b]::visit::TypeVisitor<rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt>>::visit_ty
20: 0x10dfb5474 - <rustc_middle[5ebf0ea49052770a]::mir::syntax::ConstOperand as rustc_type_ir[5b4184059516088b]::visit::TypeVisitable<rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt>>::visit_with::<rustc_monomorphize[1a1fb678af60a318]::collector::check_normalization_error::NormalizationChecker>
21: 0x10dfb4584 - <rustc_middle[5ebf0ea49052770a]::mir::Body as rustc_type_ir[5b4184059516088b]::visit::TypeVisitable<rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt>>::visit_with::<rustc_monomorphize[1a1fb678af60a318]::collector::check_normalization_error::NormalizationChecker>
22: 0x10dfd1460 - rustc_monomorphize[1a1fb678af60a318]::collector::items_of_instance
23: 0x10e3a77f8 - rustc_query_impl[57b348b6a4ba5473]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[57b348b6a4ba5473]::query_impl::items_of_instance::dynamic_query::{closure#2}::{closure#0}, rustc_middle[5ebf0ea49052770a]::query::erase::Erased<[u8; 32usize]>>
24: 0x10e6b8578 - <rustc_query_impl[57b348b6a4ba5473]::query_impl::items_of_instance::dynamic_query::{closure#2} as core[a9260f7fe7be6471]::ops::function::FnOnce<(rustc_middle[5ebf0ea49052770a]::ty::context::TyCtxt, (rustc_middle[5ebf0ea49052770a]::ty::instance::Instance, rustc_middle[5ebf0ea49052770a]::mir::mono::CollectionMode))>>::call_once
25: 0x10e462d3c - rustc_query_system[66ccc6c5d9ba3a25]::query::plumbing::try_execute_query::<rustc_query_impl[57b348b6a4ba5473]::DynamicConfig<rustc_query_system[66ccc6c5d9ba3a25]::query::caches::DefaultCache<(rustc_middle[5ebf0ea49052770a]::ty::instance::Instance, rustc_middle[5ebf0ea49052770a]::mir::mono::CollectionMode), rustc_middle[5ebf0ea49052770a]::query::erase::Erased<[u8; 32usize]>>, false, false, false>, rustc_query_impl[57b348b6a4ba5473]::plumbing::QueryCtxt, true>
26: 0x10e5486ac - rustc_query_impl[57b348b6a4ba5473]::query_impl::items_of_instance::get_query_incr::__rust_end_short_backtrace
27: 0x10dfbbec8 - rustc_monomorphize[1a1fb678af60a318]::collector::collect_items_rec::{closure#0}
28: 0x10dfcf99c - rustc_monomorphize[1a1fb678af60a318]::collector::collect_items_rec
29: 0x10dfd0c80 - rustc_monomorphize[1a1fb678af60a318]::collector::collect_items_rec
30: 0x10dfbbb70 - rustc_monomorphize[1a1fb678af60a318]::collector::collect_crate_mono_items::{closure#1}::{closure#0}
31: 0x10dfc4eb0 - rustc_monomorphize[1a1fb678af60a318]::partitioning::collect_and_partition_mono_items
32: 0x10e3aacb0 - rustc_query_impl[57b348b6a4ba5473]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[57b348b6a4ba5473]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2}::{closure#0}, rustc_middle[5ebf0ea49052770a]::query::erase::Erased<[u8; 24usize]>>
33: 0x10e3f56dc - rustc_query_system[66ccc6c5d9ba3a25]::query::plumbing::try_execute_query::<rustc_query_impl[57b348b6a4ba5473]::DynamicConfig<rustc_query_system[66ccc6c5d9ba3a25]::query::caches::SingleCache<rustc_middle[5ebf0ea49052770a]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[57b348b6a4ba5473]::plumbing::QueryCtxt, true>
34: 0x10e5650d4 - rustc_query_impl[57b348b6a4ba5473]::query_impl::collect_and_partition_mono_items::get_query_incr::__rust_end_short_backtrace
35: 0x10c9ce954 - rustc_codegen_ssa[88bfac25357d781c]::base::codegen_crate::<rustc_codegen_llvm[fb4d14e7ca1c4fd4]::LlvmCodegenBackend>
36: 0x10cac8930 - <rustc_codegen_llvm[fb4d14e7ca1c4fd4]::LlvmCodegenBackend as rustc_codegen_ssa[88bfac25357d781c]::traits::backend::CodegenBackend>::codegen_crate
37: 0x10d75341c - <rustc_interface[77ca761cc9ffec09]::queries::Linker>::codegen_and_build_linker
38: 0x10cdd1834 - rustc_interface[77ca761cc9ffec09]::passes::create_and_enter_global_ctxt::<core[a9260f7fe7be6471]::option::Option<rustc_interface[77ca761cc9ffec09]::queries::Linker>, rustc_driver_impl[fe8b8d4615f4f17e]::run_compiler::{closure#0}::{closure#2}>
39: 0x10ce22ffc - rustc_interface[77ca761cc9ffec09]::interface::run_compiler::<(), rustc_driver_impl[fe8b8d4615f4f17e]::run_compiler::{closure#0}>::{closure#1}
40: 0x10ce121c8 - std[4bc8c175d103129a]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[77ca761cc9ffec09]::util::run_in_thread_with_globals<rustc_interface[77ca761cc9ffec09]::util::run_in_thread_pool_with_globals<rustc_interface[77ca761cc9ffec09]::interface::run_compiler<(), rustc_driver_impl[fe8b8d4615f4f17e]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
41: 0x10ce26834 - <std[4bc8c175d103129a]::thread::lifecycle::spawn_unchecked<rustc_interface[77ca761cc9ffec09]::util::run_in_thread_with_globals<rustc_interface[77ca761cc9ffec09]::util::run_in_thread_pool_with_globals<rustc_interface[77ca761cc9ffec09]::interface::run_compiler<(), rustc_driver_impl[fe8b8d4615f4f17e]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[a9260f7fe7be6471]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
42: 0x10ef980bc - <std[4bc8c175d103129a]::sys::thread::unix::Thread>::new::thread_start
43: 0x19c951c08 - __pthread_cond_wait
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.