Skip to content

ICE: encountered type variable (2) #140443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Danila-Bain opened this issue Apr 29, 2025 · 4 comments
Closed

ICE: encountered type variable (2) #140443

Danila-Bain opened this issue Apr 29, 2025 · 4 comments
Labels
C-bug Category: This is a bug. F-unboxed_closures `#![feature(unboxed_closures)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@Danila-Bain
Copy link

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

@Danila-Bain Danila-Bain added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 29, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 29, 2025
@Danila-Bain
Copy link
Author

I would like to add that, for the code

#[test]
fn main() {

    fn take_and_return_any<F>(any: F) -> F {
       any 
    }

    let _closure_back = take_and_return_any(|arg| println!("{arg}"));
}

compiler gives the error

error[E0282]: type annotations needed
 --> tests/ice-report.rs:8:46
  |
8 |     let _closure_back = take_and_return_any(|arg| println!("{arg}"));
  |                                              ^^^
  |
help: consider giving this closure parameter an explicit type
  |
8 |     let _closure_back = take_and_return_any(|arg: /* Type */| println!("{arg}"));
  |                                                 ++++++++++++

For more information about this error, try `rustc --explain E0282`.

The code above has the trait Acceptable removed, comparing to the code in the issue.

I would expect the compiler to produce similar output ("annotations needed" error) for the code in the original issue, or ideally, infer the type from Acceptable trait in unambiguous cases.

@jieyouxu jieyouxu added F-unboxed_closures `#![feature(unboxed_closures)]` and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Apr 29, 2025
@moxian
Copy link
Contributor

moxian commented Apr 29, 2025

Very slightly smaller:

#![feature(unboxed_closures)]

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
}

fn main() {
    // ICE
    let _closure_back = take_and_return_acceptable_closure(|arg| true);
}

Bisection:

Regression in nightly-2024-05-13
commits:
 - 2024-05-11: #124988 (Consolidate obligation cause codes for where clauses) by compiler-errors
 - 2024-05-11: #125028 (Rollup of 6 pull requests) by matthiaskrgr
    - #124096 (Clean up users of rust_dbg_call)
    - #124829 (Enable profiler for armv7-unknown-linux-gnueabihf.)
    - #124939 (Always hide private fields in aliased type)
    - #124963 (Migrate `run-make/rustdoc-shared-flags` to rmake)
    - #124981 (Relax allocator requirements on some Rc/Arc APIs.)
    - #125008 (Add test for #122775)
 - 2024-05-12: #124153 (Refactoring after the `PlaceValue` addition) by scottmcm
 - 2024-05-12: #125001 (Uplift various `*Predicate` types into `rustc_type_ir`) by compiler-errors
 - 2024-05-12: #119427 (Fix, document, and test parser and pretty-printer edge cases related to braced macro calls) by dtolnay
 - 2024-05-12: #124883 (use key-value format in stage0 file) by onur-ozkan
 - 2024-05-12: #125012 (io::Write::write_fmt: panic if the formatter fails when the stream does not fail) by RalfJung
 - 2024-05-12: #124798 (std::alloc: use posix_memalign instead of memalign on solarish) by devnexen
 - 2024-05-12: #125045 (Rollup of 4 pull requests) by GuillaumeGomez
    - #125021 (Update reference safety requirements)
    - #125022 (Migrate rustdoc scrape examples ordering)
    - #125030 (Fix some minor issues from the ui-test auto-porting)
    - #125036 (solve: all "non-structural" logging to trace)
 - 2024-05-12: #125040 (Fix hidden title in rustdoc book) by ickk
 - 2024-05-12: #124902 (Fix MemCategorization and ExprUse visitors for new solver (this time it's better)) by compiler-errors
 - 2024-05-12: #124639 (Match ergonomics 2024: migration lint) by Jules-Bertholet

Not sure how to pinpoint it better, could be either of several PRs there.

@rustbot label: +T-types +S-has-mcve

@rustbot rustbot added S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-types Relevant to the types team, which will review and decide on the PR/issue. labels Apr 29, 2025
@compiler-errors
Copy link
Member

Probably #124902

@matthiaskrgr
Copy link
Member

Closing as duplicate since stacktrace, bisection and mvce are basically the same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-unboxed_closures `#![feature(unboxed_closures)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants