Skip to content

compiler unexpectedly panicked while using cloned closure #145164

@kmlupreti

Description

@kmlupreti

Code

use core::clone::Clone;
fn main() {
    let value = "hello".to_string();
    let show_str = |s: String| println!("{}", s);
    let disp_str = show_str;
    run.clone()(value, show_str);
    run(value, disp_str);
}
fn run<F, T>(value: T, f: F)
where
    F: Fn(T),
    T: Clone,
{
    f(value.clone());
    f(value);
}

Meta

rustc --version --verbose:

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7

Error output

thread 'rustc' panicked at /rustc/05f9846f893b09a1be1fc8560e33fc3c815cfecb/compiler/rustc_type_ir/src/binder.rs:734:9:
type parameter `T/#1` (T/#1/1) out of range when instantiating, args=[FnDef(DefId(0:6 ~ learn_rust[a5bc]::run), [Closure(DefId(0:5 ~ learn_rust[a5bc]::main::{closure#0}), [i8, Binder { value: extern "RustCall" fn((std::string::String,)), bound_vars: [] }, ()]), std::string::String])]
stack backtrace:
   0:     0x7f766cdd6e40 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h6d42cc84fc840290
   1:     0x7f766d62867c - core::fmt::write::h5af61a909e3ec64d
   2:     0x7f766e92ca51 - std::io::Write::write_fmt::h5a7b54aa6e4a315d
   3:     0x7f766cdd6ca2 - std::sys::backtrace::BacktraceLock::print::h555579e7396c26ac
   4:     0x7f766cdd9122 - std::panicking::default_hook::{{closure}}::h9128866118196224
   5:     0x7f766cdd8faa - std::panicking::default_hook::h52e9e7314e0255f6
   6:     0x7f766bf33449 - std[d9e466a2d75004a2]::panicking::update_hook::<alloc[54bfe2542ace865d]::boxed::Box<rustc_driver_impl[680c351c5444d7cd]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x7f766cdd9ca3 - std::panicking::rust_panic_with_hook::h541791bcc774ef34
   8:     0x7f766cdd999a - std::panicking::begin_panic_handler::{{closure}}::h6479a2f0137c7d19
   9:     0x7f766cdd7329 - std::sys::backtrace::__rust_end_short_backtrace::ha04e7c0fc61ded91
  10:     0x7f766cdd965d - rust_begin_unwind
  11:     0x7f7669a3d920 - core::panicking::panic_fmt::h5764ee7030b7a73d
  12:     0x7f766c4e0689 - <rustc_type_ir[282c50670f22d67a]::binder::ArgFolder<rustc_middle[a78c8f6c75429410]::ty::context::TyCtxt>>::type_param_out_of_range
  13:     0x7f766d64755a - <rustc_type_ir[282c50670f22d67a]::binder::ArgFolder<rustc_middle[a78c8f6c75429410]::ty::context::TyCtxt> as rustc_type_ir[282c50670f22d67a]::fold::TypeFolder<rustc_middle[a78c8f6c75429410]::ty::context::TyCtxt>>::fold_ty
  14:     0x7f766d63e965 - <rustc_middle[a78c8f6c75429410]::ty::generics::GenericPredicates>::instantiate_into
  15:     0x7f7669bee7b4 - <rustc_middle[a78c8f6c75429410]::ty::generics::GenericPredicates>::instantiate
  16:     0x7f766bcc4859 - <rustc_borrowck[7e335cccaf901e43]::MirBorrowckCtxt>::report_use_of_moved_or_uninitialized
  17:     0x7f766d65df22 - <rustc_borrowck[7e335cccaf901e43]::MirBorrowckCtxt>::consume_operand
  18:     0x7f766e67ddab - rustc_borrowck[7e335cccaf901e43]::do_mir_borrowck
  19:     0x7f766e655f6d - rustc_query_impl[abff21d8349146d]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[abff21d8349146d]::query_impl::mir_borrowck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[a78c8f6c75429410]::query::erase::Erased<[u8; 8usize]>>
  20:     0x7f766da0c37e - rustc_query_system[9464b8c501ef87f3]::query::plumbing::try_execute_query::<rustc_query_impl[abff21d8349146d]::DynamicConfig<rustc_data_structures[6e992f72641bf6c6]::vec_cache::VecCache<rustc_span[15ded62729b25549]::def_id::LocalDefId, rustc_middle[a78c8f6c75429410]::query::erase::Erased<[u8; 8usize]>, rustc_query_system[9464b8c501ef87f3]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[abff21d8349146d]::plumbing::QueryCtxt, true>
  21:     0x7f766da96f73 - rustc_query_impl[abff21d8349146d]::query_impl::mir_borrowck::get_query_incr::__rust_end_short_backtrace
  22:     0x7f766da02e1c - rustc_interface[84ab11ffe8dacd23]::passes::run_required_analyses
  23:     0x7f766e118f1e - rustc_interface[84ab11ffe8dacd23]::passes::analysis
  24:     0x7f766e118eef - rustc_query_impl[abff21d8349146d]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[abff21d8349146d]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[a78c8f6c75429410]::query::erase::Erased<[u8; 0usize]>>
  25:     0x7f766e64ef93 - rustc_query_system[9464b8c501ef87f3]::query::plumbing::try_execute_query::<rustc_query_impl[abff21d8349146d]::DynamicConfig<rustc_query_system[9464b8c501ef87f3]::query::caches::SingleCache<rustc_middle[a78c8f6c75429410]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[abff21d8349146d]::plumbing::QueryCtxt, true>
  26:     0x7f766e64e915 - rustc_query_impl[abff21d8349146d]::query_impl::analysis::get_query_incr::__rust_end_short_backtrace
  27:     0x7f766e59aea9 - rustc_interface[84ab11ffe8dacd23]::passes::create_and_enter_global_ctxt::<core[46aa9df3d3dcdeb1]::option::Option<rustc_interface[84ab11ffe8dacd23]::queries::Linker>, rustc_driver_impl[680c351c5444d7cd]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  28:     0x7f766e5ab726 - rustc_interface[84ab11ffe8dacd23]::interface::run_compiler::<(), rustc_driver_impl[680c351c5444d7cd]::run_compiler::{closure#0}>::{closure#1}
  29:     0x7f766e4e0744 - std[d9e466a2d75004a2]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[84ab11ffe8dacd23]::util::run_in_thread_with_globals<rustc_interface[84ab11ffe8dacd23]::util::run_in_thread_pool_with_globals<rustc_interface[84ab11ffe8dacd23]::interface::run_compiler<(), rustc_driver_impl[680c351c5444d7cd]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  30:     0x7f766e4e0419 - <<std[d9e466a2d75004a2]::thread::Builder>::spawn_unchecked_<rustc_interface[84ab11ffe8dacd23]::util::run_in_thread_with_globals<rustc_interface[84ab11ffe8dacd23]::util::run_in_thread_pool_with_globals<rustc_interface[84ab11ffe8dacd23]::interface::run_compiler<(), rustc_driver_impl[680c351c5444d7cd]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[46aa9df3d3dcdeb1]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  31:     0x7f766e4dfbaf - std::sys::pal::unix::thread::Thread::new::thread_start::hcc5ed016d554f327
  32:     0x7f766867d7eb - <unknown>
  33:     0x7f766870118c - <unknown>
  34:                0x0 - <unknown>

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: rustc 1.86.0 (05f9846f8 2025-03-31) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type bin -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 [mir_borrowck] borrow-checking `main`
#1 [analysis] running analysis passes on this crate
end of query stack
error: could not compile `learn-rust` (bin "learn-rust")

Caused by:
  process didn't exit successfully: `/home/kml/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name learn_rust --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=135 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values())' -C metadata=ee70aad8aebd16b1 -C extra-filename=-6d41ed70a80210d2 --out-dir /home/kml/projects/learn-rust/target/debug/deps -C incremental=/home/kml/projects/learn-rust/target/debug/incremental -L dependency=/home/kml/projects/learn-rust/target/debug/deps` (exit status: 101)
Backtrace

 Compiling learn-rust v0.1.0 (/home/kml/projects/learn-rust)

thread 'rustc' panicked at /rustc/05f9846f893b09a1be1fc8560e33fc3c815cfecb/compiler/rustc_type_ir/src/binder.rs:734:9:
type parameter `T/#1` (T/#1/1) out of range when instantiating, args=[FnDef(DefId(0:6 ~ learn_rust[a5bc]::run), [Closure(DefId(0:5 ~ learn_rust[a5bc]::main::{closure#0}), [i8, Binder { value: extern "RustCall" fn((std::string::String,)), bound_vars: [] }, ()]), std::string::String])]
stack backtrace:
   0: rust_begin_unwind
   1: core::panicking::panic_fmt
   2: <rustc_type_ir::binder::ArgFolder<rustc_middle::ty::context::TyCtxt>>::type_param_out_of_range
   3: <rustc_type_ir::binder::ArgFolder<rustc_middle::ty::context::TyCtxt> as rustc_type_ir::fold::TypeFolder<rustc_middle::ty::context::TyCtxt>>::fold_ty
   4: <rustc_middle::ty::generics::GenericPredicates>::instantiate_into
   5: <rustc_middle::ty::generics::GenericPredicates>::instantiate
   6: <rustc_borrowck::MirBorrowckCtxt>::report_use_of_moved_or_uninitialized
   7: <rustc_borrowck::MirBorrowckCtxt>::consume_operand
   8: rustc_borrowck::do_mir_borrowck
      [... omitted 1 frame ...]
   9: rustc_interface::passes::run_required_analyses
  10: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  11: rustc_interface::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  12: rustc_interface::interface::run_compiler::<(), 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: rustc 1.86.0 (05f9846f8 2025-03-31) running on x86_64-unknown-linux-gnu

Metadata

Metadata

Assignees

Labels

A-borrow-checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️S-has-bisectionStatus: A bisection has been found for this issueS-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions