Skip to content

ICE: encountered type variable (2) #140443

Closed as duplicate of#131758
Closed as duplicate of#131758
@Danila-Bain

Description

@Danila-Bain

May be related: #131758

Context

I am trying to write a library, with an API that accepts closures of different calling signatures, such that the types of arguments are automatically deduced. For that, I tried the idea to use a trait Acceptable, which is implemented for closures with acceptable signatures, such that compiler can deduce the types of arguments, based on the list of acceptable signatures.

I am new to the language, and would appreciate the suggestions to make this report more useful.

Code with error

#![feature(unboxed_closures)]

#[test]
fn main() {

    trait Acceptable<Args> {}
    impl<F> Acceptable<(f64,)> for F where F: Fn<(f64,)> {}

    fn take_and_return_acceptable_closure<F, Args>(closure: F) -> F
    where F: Acceptable<Args>, {
        closure
    }


    // OK with explicit type annotation
    // let _closure_back = take_and_return_acceptable_closure(|arg: f64| println!("{arg}"));

    // OK with a call with appropriate argument
    // let closure_back = take_and_return_acceptable_closure(|arg| println!("{arg}"));
    // closure_back(1.);

    // ICE
    let _closure_back = take_and_return_acceptable_closure(|arg| println!("{arg}"));
}

For argument type annotation in the closure (or call of the closure) with the type other than f64, we get the expected compiler error "unsatisfied trait bound".

Meta

rustc --version --verbose:

rustc 1.88.0-nightly (6bc57c6bf 2025-04-22)
binary: rustc
commit-hash: 6bc57c6bf7d0024ad9ea5a2c112f3fc9c383c8a4
commit-date: 2025-04-22
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2

Error output

❯ RUST_BACKTRACE=1 cargo test
   Compiling diffurch v0.1.0 (/home/dbain/documents/code/rust/diffurch-rust)
note: no errors encountered even though delayed bugs were created

note: those delayed bugs will now be shown as internal compiler errors

error: internal compiler error: encountered type variable
  --> tests/ice-report.rs:23:61
   |
23 |     let _closure_back = take_and_return_acceptable_closure(|arg| println!("{arg}"));
   |                                                             ^^^
   |
note: delayed at compiler/rustc_hir_typeck/src/expr_use_visitor.rs:199:20

<BACKTRACE OMITTED>

   --> tests/ice-report.rs:23:61
   |
23 |     let _closure_back = take_and_return_acceptable_closure(|arg| println!("{arg}"));
   |                                                             ^^^

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-IC
E%2C+T-compiler&template=ice.md

note: please make sure that you have updated to the latest nightly

note: please attach the file at `/home/dbain/documents/code/rust/diffurch-rust/rustc-ice-2025-04-29T08_49
_09-406486.txt` to your bug report

note: compiler flags: -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:
end of query stack
error: could not compile `diffurch` (test "ice-report")

Caused by:
  process didn't exit successfully: `/home/dbain/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/
rustc --crate-name ice_report --edition=2024 tests/ice-report.rs --error-format=json --json=diagnostic-re
ndered-ansi,artifacts,future-incompat --diagnostic-width=105 --emit=dep-info,link -C embed-bitcode=no -C 
debuginfo=2 --test --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values())' -C metadata=3ca0bf
e5b741aaea -C extra-filename=-6cbc201e0ebcc785 --out-dir /home/dbain/documents/code/rust/diffurch-rust/ta
rget/debug/deps -C incremental=/home/dbain/documents/code/rust/diffurch-rust/target/debug/incremental -L 
dependency=/home/dbain/documents/code/rust/diffurch-rust/target/debug/deps --extern diffurch=/home/dbain/
documents/code/rust/diffurch-rust/target/debug/deps/libdiffurch-e695a35a18729d4a.rlib --extern ndarray=/h
ome/dbain/documents/code/rust/diffurch-rust/target/debug/deps/libndarray-e092c5808888c212.rlib --extern o
pen=/home/dbain/documents/code/rust/diffurch-rust/target/debug/deps/libopen-dc74bfca6b6ddb30.rlib --exter
n pgfplots=/home/dbain/documents/code/rust/diffurch-rust/target/debug/deps/libpgfplots-d411b40ede641f1c.r
lib --extern plotters=/home/dbain/documents/code/rust/diffurch-rust/target/debug/deps/libplotters-b5211b3
525de24ab.rlib --extern plotters_backend=/home/dbain/documents/code/rust/diffurch-rust/target/debug/deps/
libplotters_backend-379316944ec1b50f.rlib -L native=/usr/lib -L native=/usr/lib` (exit status: 101)
Backtrace

Contents of rustc-ice-2025-04-28T15_53_14-386343.txt:

delayed bug: encountered type variable
   0: <rustc_errors::DiagCtxtInner>::emit_diagnostic
   1: <rustc_errors::DiagCtxtHandle>::emit_diagnostic
   2: <rustc_span::ErrorGuaranteed as rustc_errors::diagnostic::EmissionGuarantee>::emit_producing_guarantee
   3: <rustc_errors::DiagCtxtHandle>::span_delayed_bug::<rustc_span::span_encoding::Span, alloc::string::String>
   4: <&rustc_hir_typeck::fn_ctxt::FnCtxt as rustc_hir_typeck::expr_use_visitor::TypeInformationCtxt>::report_bug::<&str>
   5: <rustc_hir_typeck::expr_use_visitor::ExprUseVisitor<&rustc_hir_typeck::fn_ctxt::FnCtxt, &mut rustc_hir_typeck::upvar::InferBorrowKind>>::node_ty
   6: <rustc_hir_typeck::expr_use_visitor::ExprUseVisitor<&rustc_hir_typeck::fn_ctxt::FnCtxt, &mut rustc_hir_typeck::upvar::InferBorrowKind>>::pat_ty_unadjusted
   7: <rustc_hir_typeck::fn_ctxt::FnCtxt>::analyze_closure
   8: <rustc_hir_typeck::upvar::InferBorrowKindVisitor as rustc_hir::intravisit::Visitor>::visit_expr
   9: <rustc_hir_typeck::upvar::InferBorrowKindVisitor as rustc_hir::intravisit::Visitor>::visit_expr
  10: <rustc_hir_typeck::upvar::InferBorrowKindVisitor as rustc_hir::intravisit::Visitor>::visit_block
  11: rustc_hir_typeck::typeck_with_inspect::{closure#0}
  12: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 8]>>
  13: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_data_structures::vec_cache::VecCache<rustc_span::def_id::LocalDefId, rustc_middle::query::erase::Erased<[u8; 8]>, rustc_query_system::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, true>
  14: rustc_query_impl::query_impl::typeck::get_query_incr::__rust_end_short_backtrace
  15: rustc_hir_analysis::check_crate
  16: rustc_interface::passes::run_required_analyses
  17: rustc_interface::passes::analysis
  18: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 0]>>
  19: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 0]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, true>
  20: rustc_query_impl::query_impl::analysis::get_query_incr::__rust_end_short_backtrace
  21: 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}
  22: rustc_interface::interface::run_compiler::<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
  23: std::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface::util::run_in_thread_with_globals<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  24: <<std::thread::Builder>::spawn_unchecked_<rustc_interface::util::run_in_thread_with_globals<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  25: std::sys::pal::unix::thread::Thread::new::thread_start
  26: <unknown>
  27: <unknown>



rustc version: 1.88.0-nightly (6bc57c6bf 2025-04-22)
platform: x86_64-unknown-linux-gnu

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-unboxed_closures`#![feature(unboxed_closures)]`I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️S-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.T-typesRelevant to the types 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