Skip to content

[ICE]: RefCell already mutably borrowed #155893

@matthiaskrgr

Description

@matthiaskrgr

auto-reduced (treereduce-rust):

struct S;

fn func(_f: impl Fn()) -> usize {
    0
}

fn test_func(s: &S) -> usize {
    func(|| return 42)
}

original:

// Regression test for #125325

// Tests that we suggest changing an `impl Fn` param
// to `impl FnMut` when the provided closure arg
// is trying to mutate the closure env.
// Ensures that it works that way for both
// functions and methods

struct S;

impl S {
    fn assoc_func(&self, _f: impl Fn()) -> usize {
        0
    }
}

fn func(_f: impl Fn()) -> usize {
    0
}

fn test_func(s: &S) -> usize {
    let mut x = ();
    s.assoc_func(|| x = ());
    //~^ ERROR cannot assign to `x`, as it is a captured variable in a `Fn` closure
    func(|| return 42)
    //~^ ERROR cannot assign to `x`, as it is a captured variable in a `Fn` closure
}

fn main() {}

Version information

rustc 1.97.0-nightly (345a975e7 2026-04-27)
binary: rustc
commit-hash: 345a975e76de74f090c55d5b56f5f6dd41b655a2
commit-date: 2026-04-27
host: x86_64-unknown-linux-gnu
release: 1.97.0-nightly
LLVM version: 22.1.2

Possibly related line of code:

// may occur at the first return expression we see in the closure
// (if it conflicts with the declared return type). Skip adding a
// note in this case, since it would be incorrect.
&& let Some(fn_sig) = fcx.body_fn_sig()
&& fn_sig.output().is_ty_var()
&& fcx.ret_coercion.as_ref().is_some_and(|ret_coercion| {
fcx.resolve_vars_if_possible(ret_coercion.borrow().expected_ty()) == expected
})
{
err.span_note(sp, format!("return type inferred to be `{expected}` here"));
}
err

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc

Program output

error[E0601]: `main` function not found in crate `mvce`
 --> /tmp/icemaker_global_tempdir.E6qQXQsBxYEp/rustc_testrunner_tmpdir_reporting.HAMVYJjzmNyD/mvce.rs:9:2
  |
9 | }
  |  ^ consider adding a `main` function to `/tmp/icemaker_global_tempdir.E6qQXQsBxYEp/rustc_testrunner_tmpdir_reporting.HAMVYJjzmNyD/mvce.rs`


thread 'rustc' (1135290) panicked at /rustc-dev/345a975e76de74f090c55d5b56f5f6dd41b655a2/compiler/rustc_hir_typeck/src/coercion.rs:2013:59:
RefCell already mutably borrowed
stack backtrace:
   0:     0x7f9f302fae99 - <<std[a9eb3f39b69ad3ff]::sys::backtrace::BacktraceLock>::print::DisplayBacktrace as core[b04a2f1476229c25]::fmt::Display>::fmt
   1:     0x7f9f30a15e48 - core[b04a2f1476229c25]::fmt::write
   2:     0x7f9f30311986 - <std[a9eb3f39b69ad3ff]::sys::stdio::unix::Stderr as std[a9eb3f39b69ad3ff]::io::Write>::write_fmt
   3:     0x7f9f302d109e - std[a9eb3f39b69ad3ff]::panicking::default_hook::{closure#0}
   4:     0x7f9f302ee583 - std[a9eb3f39b69ad3ff]::panicking::default_hook
   5:     0x7f9f2f2d9a71 - std[a9eb3f39b69ad3ff]::panicking::update_hook::<alloc[f5407cb677668734]::boxed::Box<rustc_driver_impl[4eedd31b7089c8cf]::install_ice_hook::{closure#1}>>::{closure#0}
   6:     0x7f9f302ee862 - std[a9eb3f39b69ad3ff]::panicking::panic_with_hook
   7:     0x7f9f302d1162 - std[a9eb3f39b69ad3ff]::panicking::panic_handler::{closure#0}
   8:     0x7f9f302c5659 - std[a9eb3f39b69ad3ff]::sys::backtrace::__rust_end_short_backtrace::<std[a9eb3f39b69ad3ff]::panicking::panic_handler::{closure#0}, !>
   9:     0x7f9f302d2bcd - __rustc[929c9a63fff72118]::rust_begin_unwind
  10:     0x7f9f2cf183cc - core[b04a2f1476229c25]::panicking::panic_fmt
  11:     0x7f9f2eb2766f - core[b04a2f1476229c25]::cell::panic_already_mutably_borrowed
  12:     0x7f9f2ea8043f - <rustc_hir_typeck[4f770d89427b513d]::coercion::CoerceMany>::report_return_mismatched_types
  13:     0x7f9f30e84a6b - <rustc_hir_typeck[4f770d89427b513d]::fn_ctxt::FnCtxt>::check_expr_return
  14:     0x7f9f30e91d9a - <rustc_hir_typeck[4f770d89427b513d]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  15:     0x7f9f30e9bf05 - rustc_hir_typeck[4f770d89427b513d]::check::check_fn
  16:     0x7f9f317b421c - <rustc_hir_typeck[4f770d89427b513d]::fn_ctxt::FnCtxt>::check_expr_closure
  17:     0x7f9f30e92144 - <rustc_hir_typeck[4f770d89427b513d]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  18:     0x7f9f30e6c0f8 - <rustc_hir_typeck[4f770d89427b513d]::fn_ctxt::FnCtxt>::check_expr_call
  19:     0x7f9f30e8fe41 - <rustc_hir_typeck[4f770d89427b513d]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  20:     0x7f9f30ea5932 - <rustc_hir_typeck[4f770d89427b513d]::fn_ctxt::FnCtxt>::check_expr_block
  21:     0x7f9f30e9086f - <rustc_hir_typeck[4f770d89427b513d]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  22:     0x7f9f30e9bf05 - rustc_hir_typeck[4f770d89427b513d]::check::check_fn
  23:     0x7f9f3139a506 - rustc_hir_typeck[4f770d89427b513d]::typeck_with_inspect::{closure#0}
  24:     0x7f9f31397404 - rustc_query_impl[2be19ef8da6108aa]::query_impl::typeck_root::invoke_provider_fn::__rust_begin_short_backtrace
  25:     0x7f9f30b18166 - rustc_query_impl[2be19ef8da6108aa]::execution::try_execute_query::<rustc_data_structures[fb6e4346a2abdb30]::vec_cache::VecCache<rustc_span[bbe1d656f07b00ee]::def_id::LocalDefId, rustc_middle[dbb0bf4bafb0ab2f]::query::erase::ErasedData<[u8; 8usize]>, rustc_middle[dbb0bf4bafb0ab2f]::dep_graph::graph::DepNodeIndex>, false>
  26:     0x7f9f30b17a43 - rustc_query_impl[2be19ef8da6108aa]::query_impl::typeck_root::execute_query_non_incr::__rust_end_short_backtrace
  27:     0x7f9f312a8a54 - <rustc_middle[dbb0bf4bafb0ab2f]::ty::context::TyCtxt>::par_hir_body_owners::<rustc_hir_analysis[54c6f91f6d421f22]::check_crate::{closure#1}>::{closure#0}
  28:     0x7f9f312a8426 - rustc_hir_analysis[54c6f91f6d421f22]::check_crate
  29:     0x7f9f31943821 - rustc_interface[fb0ae15303da83fc]::passes::analysis
  30:     0x7f9f31b7e7c9 - rustc_query_impl[2be19ef8da6108aa]::execution::try_execute_query::<rustc_middle[dbb0bf4bafb0ab2f]::query::caches::SingleCache<rustc_middle[dbb0bf4bafb0ab2f]::query::erase::ErasedData<[u8; 0usize]>>, false>
  31:     0x7f9f31b7e429 - rustc_query_impl[2be19ef8da6108aa]::query_impl::analysis::execute_query_non_incr::__rust_end_short_backtrace
  32:     0x7f9f31b9b742 - rustc_interface[fb0ae15303da83fc]::interface::run_compiler::<(), rustc_driver_impl[4eedd31b7089c8cf]::run_compiler::{closure#0}>::{closure#1}
  33:     0x7f9f31b731fe - std[a9eb3f39b69ad3ff]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[fb0ae15303da83fc]::util::run_in_thread_with_globals<rustc_interface[fb0ae15303da83fc]::util::run_in_thread_pool_with_globals<rustc_interface[fb0ae15303da83fc]::interface::run_compiler<(), rustc_driver_impl[4eedd31b7089c8cf]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  34:     0x7f9f31b738ed - <std[a9eb3f39b69ad3ff]::thread::lifecycle::spawn_unchecked<rustc_interface[fb0ae15303da83fc]::util::run_in_thread_with_globals<rustc_interface[fb0ae15303da83fc]::util::run_in_thread_pool_with_globals<rustc_interface[fb0ae15303da83fc]::interface::run_compiler<(), rustc_driver_impl[4eedd31b7089c8cf]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[b04a2f1476229c25]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  35:     0x7f9f31b7472c - <std[a9eb3f39b69ad3ff]::sys::thread::unix::Thread>::new::thread_start
  36:     0x7f9f2b6a597a - <unknown>
  37:     0x7f9f2b7292bc - <unknown>
  38:                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: please make sure that you have updated to the latest nightly

note: rustc 1.97.0-nightly (345a975e7 2026-04-27) running on x86_64-unknown-linux-gnu

query stack during panic:
#0 [typeck_root] type-checking `test_func`
#1 [analysis] running analysis passes on crate `mvce`
end of query stack
error: aborting due to 1 previous error

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

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions