Skip to content
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

ICE: field should be captured by immutable ref if we have an Fn instance #129262

Closed
matthiaskrgr opened this issue Aug 19, 2024 · 0 comments · Fixed by #130410
Closed

ICE: field should be captured by immutable ref if we have an Fn instance #129262

matthiaskrgr opened this issue Aug 19, 2024 · 0 comments · Fixed by #130410
Assignees
Labels
C-bug Category: This is a bug. F-async_closure `#![feature(async_closure)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

Code

#![feature(async_closure)]

fn main() {}

fn needs_fn_mut<T>(mut x: impl FnMut() -> T) {
    x();
}

fn hello(x: Ty) {
    needs_fn_mut(async || {
        x.hello();
    });
}

struct Ty;
impl Ty {
    fn hello(self) {}
}

Meta

rustc --version --verbose:

rustc 1.82.0-nightly (804be74e3 2024-08-19)
binary: rustc
commit-hash: 804be74e3c4ec3646b448dfdb7a0fc31237a5818
commit-date: 2024-08-19
host: x86_64-unknown-linux-gnu
release: 1.82.0-nightly
LLVM version: 19.1.0

rustc -Zvalidate-mir --edition=2018 --crate-type=lib -Copt-level=3 file.rs

Error output

error[E0507]: cannot move out of `x` which is behind a mutable reference
  --> a.rs:10:18
   |
10 |     needs_fn_mut(async || {
   |                  ^^^^^^^^ `x` is moved here
11 |         x.hello();
   |         -
   |         |
   |         variable moved due to use in coroutine
   |         move occurs because `x` has type `Ty`, which does not implement the `Copy` trait
   |
note: if `Ty` implemented `Clone`, you could clone the value
  --> a.rs:15:1
   |
11 |         x.hello();
   |         - you could clone this value
...
15 | struct Ty;
   | ^^^^^^^^^ consider implementing `Clone` for this type
Backtrace

thread 'rustc' panicked at compiler/rustc_mir_transform/src/shim.rs:1082:13:
assertion `left matches right` failed: field should be captured by immutable ref if we have an `Fn` instance
  left: Ty
 right: ty::Ref(_, _, hir::Mutability::Not)
stack backtrace:
   0:     0x7d1d541c0fdd - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h8ceea53997a5b9b5
   1:     0x7d1d54a05297 - core::fmt::write::h05be396c0a785199
   2:     0x7d1d55994951 - std::io::Write::write_fmt::h0faf3273f924176d
   3:     0x7d1d541c36bb - std::panicking::default_hook::{{closure}}::h5ff21810d79cb0e4
   4:     0x7d1d541c332e - std::panicking::default_hook::h537184642066c310
   5:     0x7d1d53359419 - std[ddbe28eb9a61e65d]::panicking::update_hook::<alloc[a7f470ce4bd697bf]::boxed::Box<rustc_driver_impl[637e7f0798538e00]::install_ice_hook::{closure#0}>>::{closure#0}
   6:     0x7d1d541c3fd7 - std::panicking::rust_panic_with_hook::hc4a14d811dd29982
   7:     0x7d1d541c3c97 - std::panicking::begin_panic_handler::{{closure}}::h7df3c2502d8a1754
   8:     0x7d1d541c1499 - std::sys::backtrace::__rust_end_short_backtrace::h8861b37587c51d36
   9:     0x7d1d541c3964 - rust_begin_unwind
  10:     0x7d1d51094943 - core::panicking::panic_fmt::he0bc112117bdf86d
  11:     0x7d1d52d5cbef - core::panicking::assert_failed_inner::h5a404377cbdb79cc
  12:     0x7d1d53a6168b - core[b1645707a2a7518c]::panicking::assert_matches_failed::<&rustc_type_ir[66f9952f9eb239cc]::ty_kind::TyKind<rustc_middle[19486b3a4165f2e4]::ty::context::TyCtxt>>
  13:     0x7d1d55348106 - rustc_mir_transform[2c3e1838a62bac28]::shim::make_shim
  14:     0x7d1d5534512f - rustc_query_impl[9179f30695a188d8]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9179f30695a188d8]::query_impl::mir_shims::dynamic_query::{closure#2}::{closure#0}, rustc_middle[19486b3a4165f2e4]::query::erase::Erased<[u8; 8usize]>>
  15:     0x7d1d553450e3 - <rustc_query_impl[9179f30695a188d8]::query_impl::mir_shims::dynamic_query::{closure#2} as core[b1645707a2a7518c]::ops::function::FnOnce<(rustc_middle[19486b3a4165f2e4]::ty::context::TyCtxt, rustc_middle[19486b3a4165f2e4]::ty::instance::InstanceKind)>>::call_once
  16:     0x7d1d553447b5 - rustc_query_system[e02210f0baf32b51]::query::plumbing::try_execute_query::<rustc_query_impl[9179f30695a188d8]::DynamicConfig<rustc_query_system[e02210f0baf32b51]::query::caches::DefaultCache<rustc_middle[19486b3a4165f2e4]::ty::instance::InstanceKind, rustc_middle[19486b3a4165f2e4]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[9179f30695a188d8]::plumbing::QueryCtxt, false>
  17:     0x7d1d553444ff - rustc_query_impl[9179f30695a188d8]::query_impl::mir_shims::get_query_non_incr::__rust_end_short_backtrace
  18:     0x7d1d51556a9b - <rustc_middle[19486b3a4165f2e4]::ty::context::TyCtxt>::instance_mir
  19:     0x7d1d5555a65c - rustc_mir_transform[2c3e1838a62bac28]::inline::cycle::mir_inliner_callees
  20:     0x7d1d5555a528 - rustc_query_impl[9179f30695a188d8]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9179f30695a188d8]::query_impl::mir_inliner_callees::dynamic_query::{closure#2}::{closure#0}, rustc_middle[19486b3a4165f2e4]::query::erase::Erased<[u8; 16usize]>>
  21:     0x7d1d5555a4e7 - <rustc_query_impl[9179f30695a188d8]::query_impl::mir_inliner_callees::dynamic_query::{closure#2} as core[b1645707a2a7518c]::ops::function::FnOnce<(rustc_middle[19486b3a4165f2e4]::ty::context::TyCtxt, rustc_middle[19486b3a4165f2e4]::ty::instance::InstanceKind)>>::call_once
  22:     0x7d1d551993b3 - rustc_query_system[e02210f0baf32b51]::query::plumbing::try_execute_query::<rustc_query_impl[9179f30695a188d8]::DynamicConfig<rustc_query_system[e02210f0baf32b51]::query::caches::DefaultCache<rustc_middle[19486b3a4165f2e4]::ty::instance::InstanceKind, rustc_middle[19486b3a4165f2e4]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[9179f30695a188d8]::plumbing::QueryCtxt, false>
  23:     0x7d1d5519910d - rustc_query_impl[9179f30695a188d8]::query_impl::mir_inliner_callees::get_query_non_incr::__rust_end_short_backtrace
  24:     0x7d1d5596fe23 - rustc_mir_transform[2c3e1838a62bac28]::inline::cycle::mir_callgraph_reachable::process
  25:     0x7d1d5596f130 - rustc_mir_transform[2c3e1838a62bac28]::inline::cycle::mir_callgraph_reachable::process
  26:     0x7d1d5596e214 - rustc_mir_transform[2c3e1838a62bac28]::inline::cycle::mir_callgraph_reachable
  27:     0x7d1d5596e0a9 - rustc_query_impl[9179f30695a188d8]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9179f30695a188d8]::query_impl::mir_callgraph_reachable::dynamic_query::{closure#2}::{closure#0}, rustc_middle[19486b3a4165f2e4]::query::erase::Erased<[u8; 1usize]>>
  28:     0x7d1d5596e06b - <rustc_query_impl[9179f30695a188d8]::query_impl::mir_callgraph_reachable::dynamic_query::{closure#2} as core[b1645707a2a7518c]::ops::function::FnOnce<(rustc_middle[19486b3a4165f2e4]::ty::context::TyCtxt, (rustc_middle[19486b3a4165f2e4]::ty::instance::Instance, rustc_span[9cfca6d3892329e0]::def_id::LocalDefId))>>::call_once
  29:     0x7d1d55197fa5 - rustc_query_system[e02210f0baf32b51]::query::plumbing::try_execute_query::<rustc_query_impl[9179f30695a188d8]::DynamicConfig<rustc_query_system[e02210f0baf32b51]::query::caches::DefaultCache<(rustc_middle[19486b3a4165f2e4]::ty::instance::Instance, rustc_span[9cfca6d3892329e0]::def_id::LocalDefId), rustc_middle[19486b3a4165f2e4]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[9179f30695a188d8]::plumbing::QueryCtxt, false>
  30:     0x7d1d55197cf4 - rustc_query_impl[9179f30695a188d8]::query_impl::mir_callgraph_reachable::get_query_non_incr::__rust_end_short_backtrace
  31:     0x7d1d55365c45 - <rustc_mir_transform[2c3e1838a62bac28]::inline::Inliner>::try_inlining
  32:     0x7d1d553634e0 - <rustc_mir_transform[2c3e1838a62bac28]::inline::Inliner>::process_blocks
  33:     0x7d1d553629a6 - <rustc_mir_transform[2c3e1838a62bac28]::inline::Inline as rustc_middle[19486b3a4165f2e4]::mir::MirPass>::run_pass
  34:     0x7d1d54a02151 - rustc_mir_transform[2c3e1838a62bac28]::pass_manager::run_passes_inner
  35:     0x7d1d5555b537 - rustc_mir_transform[2c3e1838a62bac28]::optimized_mir
  36:     0x7d1d5554875d - rustc_query_impl[9179f30695a188d8]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9179f30695a188d8]::query_impl::optimized_mir::dynamic_query::{closure#2}::{closure#0}, rustc_middle[19486b3a4165f2e4]::query::erase::Erased<[u8; 8usize]>>
  37:     0x7d1d54a263e7 - rustc_query_system[e02210f0baf32b51]::query::plumbing::try_execute_query::<rustc_query_impl[9179f30695a188d8]::DynamicConfig<rustc_query_system[e02210f0baf32b51]::query::caches::DefIdCache<rustc_middle[19486b3a4165f2e4]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[9179f30695a188d8]::plumbing::QueryCtxt, false>
  38:     0x7d1d54a2599f - rustc_query_impl[9179f30695a188d8]::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace
  39:     0x7d1d51556c44 - <rustc_middle[19486b3a4165f2e4]::ty::context::TyCtxt>::instance_mir
  40:     0x7d1d551a30c6 - rustc_interface[b9699dd6fb07a8c]::passes::run_required_analyses
  41:     0x7d1d5558671e - rustc_interface[b9699dd6fb07a8c]::passes::analysis
  42:     0x7d1d555866f1 - rustc_query_impl[9179f30695a188d8]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9179f30695a188d8]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[19486b3a4165f2e4]::query::erase::Erased<[u8; 1usize]>>
  43:     0x7d1d5594fe2e - rustc_query_system[e02210f0baf32b51]::query::plumbing::try_execute_query::<rustc_query_impl[9179f30695a188d8]::DynamicConfig<rustc_query_system[e02210f0baf32b51]::query::caches::SingleCache<rustc_middle[19486b3a4165f2e4]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[9179f30695a188d8]::plumbing::QueryCtxt, false>
  44:     0x7d1d5594fb8f - rustc_query_impl[9179f30695a188d8]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  45:     0x7d1d557d8a6a - rustc_interface[b9699dd6fb07a8c]::interface::run_compiler::<core[b1645707a2a7518c]::result::Result<(), rustc_span[9cfca6d3892329e0]::ErrorGuaranteed>, rustc_driver_impl[637e7f0798538e00]::run_compiler::{closure#0}>::{closure#1}
  46:     0x7d1d557c0c90 - std[ddbe28eb9a61e65d]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[b9699dd6fb07a8c]::util::run_in_thread_with_globals<rustc_interface[b9699dd6fb07a8c]::util::run_in_thread_pool_with_globals<rustc_interface[b9699dd6fb07a8c]::interface::run_compiler<core[b1645707a2a7518c]::result::Result<(), rustc_span[9cfca6d3892329e0]::ErrorGuaranteed>, rustc_driver_impl[637e7f0798538e00]::run_compiler::{closure#0}>::{closure#1}, core[b1645707a2a7518c]::result::Result<(), rustc_span[9cfca6d3892329e0]::ErrorGuaranteed>>::{closure#0}, core[b1645707a2a7518c]::result::Result<(), rustc_span[9cfca6d3892329e0]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[b1645707a2a7518c]::result::Result<(), rustc_span[9cfca6d3892329e0]::ErrorGuaranteed>>
  47:     0x7d1d557c12fa - <<std[ddbe28eb9a61e65d]::thread::Builder>::spawn_unchecked_<rustc_interface[b9699dd6fb07a8c]::util::run_in_thread_with_globals<rustc_interface[b9699dd6fb07a8c]::util::run_in_thread_pool_with_globals<rustc_interface[b9699dd6fb07a8c]::interface::run_compiler<core[b1645707a2a7518c]::result::Result<(), rustc_span[9cfca6d3892329e0]::ErrorGuaranteed>, rustc_driver_impl[637e7f0798538e00]::run_compiler::{closure#0}>::{closure#1}, core[b1645707a2a7518c]::result::Result<(), rustc_span[9cfca6d3892329e0]::ErrorGuaranteed>>::{closure#0}, core[b1645707a2a7518c]::result::Result<(), rustc_span[9cfca6d3892329e0]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[b1645707a2a7518c]::result::Result<(), rustc_span[9cfca6d3892329e0]::ErrorGuaranteed>>::{closure#1} as core[b1645707a2a7518c]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  48:     0x7d1d557c166b - std::sys::pal::unix::thread::Thread::new::thread_start::hea7a4f91b7cfbbd5
  49:     0x7d1d56eee39d - <unknown>
  50:     0x7d1d56f7349c - <unknown>
  51:                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: please attach the file at `/tmp/im/rustc-ice-2024-08-19T05_31_11-2769501.txt` to your bug report

note: compiler flags: -Z validate-mir --crate-type lib -C opt-level=3

query stack during panic:
#0 [mir_shims] generating MIR shim for `hello::{closure#0}`
#1 [mir_inliner_callees] computing all local function calls in `hello::{closure#0}`
end of query stack
error: aborting due to 1 previous error

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

@matthiaskrgr matthiaskrgr added 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. C-bug Category: This is a bug. F-async_closure `#![feature(async_closure)]` labels Aug 19, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 19, 2024
@compiler-errors compiler-errors self-assigned this Aug 19, 2024
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 20, 2024
@matthiaskrgr matthiaskrgr added the S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. label Sep 16, 2024
@bors bors closed this as completed in 9a1004c Sep 16, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 16, 2024
Rollup merge of rust-lang#130410 - compiler-errors:shim-borrowck-err, r=jieyouxu

Don't ICE when generating `Fn` shim for async closure with borrowck error

Turn an assumption that I had originally written as an assert into a delayed bug, because this shim code is reachable even if we have borrowck errors via the MIR inliner.

Fixes rust-lang#129262.
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-async_closure `#![feature(async_closure)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants