Skip to content

ICE: leftover child captures? #123818

Closed
@matthiaskrgr

Description

@matthiaskrgr

auto-reduced (treereduce-rust):

struct DropMe();

pub fn main() {
    block_on(async {
        let b = DropMe(0, 0, o1, o2);
        let async_closure = async move |a: DropMe| {
            println!("{a:?} {b:?}");
        };
    });
}
original code

original:

#![feature(async_closure, noop_waker, async_fn_traits)]

use std::future::Future;
use std::pin::pin;
use std::task::*;

pub fn block_on<T>(fut: impl Future<Output = Double>) -> T {
    let mut fut = pin!(fut);
    let ctx = &mut Context::from_waker(Waker::noop());

    assert_eq!(Some(3), memrchr(b'a', b"aaaa"));
}

async fn call_once(f: impl async FnOnce(DropMe)) {
    f(DropMe("world")).await;
}

#[derive(Debug)]
struct DropMe(&'static str);

impl Drop for DropMe {
    fn drop(&mut self) {
        println!("{}", self.0)async {
        let b = 2i32;
        let mut async_closure = async move |a: i32| {
            println!("{a} {b}");
        };
        call_mut(&mut async_closure).await;
        call_once(async_closure).await;

        // No-capture closures implement `Fn`.
        let async_closure = async move |a: i32| {
            println!("{a}");
        };
        call_normal(&async_closure).await;
        call_normal_once(async_closure).await;
    }
    }
}

pub fn main() {
    block_on(async {
        let b = DropMe(0, 0, o1, o2);
        let async_closure = async move |a: DropMe| {
            println!("{a:?} {b:?}");
        };
        call_once(async_closure).await;
    });
}

Version information

rustc 1.79.0-nightly (df7daa815 2024-04-11)
binary: rustc
commit-hash: df7daa815f4047a1cfe24ecfbc12cfd4f40fa928
commit-date: 2024-04-11
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.3

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc --edition=2021

Program output

error[E0425]: cannot find value `o1` in this scope
 --> /tmp/icemaker_global_tempdir.nBTpTIeUvGiR/rustc_testrunner_tmpdir_reporting.inyB8dpg3IHG/mvce.rs:5:30
  |
5 |         let b = DropMe(0, 0, o1, o2);
  |                              ^^ not found in this scope

error[E0425]: cannot find value `o2` in this scope
 --> /tmp/icemaker_global_tempdir.nBTpTIeUvGiR/rustc_testrunner_tmpdir_reporting.inyB8dpg3IHG/mvce.rs:5:34
  |
5 |         let b = DropMe(0, 0, o1, o2);
  |                                  ^^ not found in this scope

error[E0658]: async closures are unstable
 --> /tmp/icemaker_global_tempdir.nBTpTIeUvGiR/rustc_testrunner_tmpdir_reporting.inyB8dpg3IHG/mvce.rs:6:29
  |
6 |         let async_closure = async move |a: DropMe| {
  |                             ^^^^^
  |
  = note: see issue #62290 <https://github.com/rust-lang/rust/issues/62290> for more information
  = help: add `#![feature(async_closure)]` to the crate attributes to enable
  = note: this compiler was built on 2024-04-11; consider upgrading it if it is out of date
  = help: to use an async block, remove the `||`: `async {`

error[E0061]: this struct takes 0 arguments but 4 arguments were supplied
 --> /tmp/icemaker_global_tempdir.nBTpTIeUvGiR/rustc_testrunner_tmpdir_reporting.inyB8dpg3IHG/mvce.rs:5:17
  |
5 |         let b = DropMe(0, 0, o1, o2);
  |                 ^^^^^^ -  -  --  -- unexpected argument
  |                        |  |  |
  |                        |  |  unexpected argument
  |                        |  unexpected argument of type `{integer}`
  |                        unexpected argument of type `{integer}`
  |
note: tuple struct defined here
 --> /tmp/icemaker_global_tempdir.nBTpTIeUvGiR/rustc_testrunner_tmpdir_reporting.inyB8dpg3IHG/mvce.rs:1:8
  |
1 | struct DropMe();
  |        ^^^^^^
help: remove the extra arguments
  |
5 -         let b = DropMe(0, 0, o1, o2);
5 +         let b = DropMe();
  |

error[E0277]: `DropMe` doesn't implement `Debug`
 --> /tmp/icemaker_global_tempdir.nBTpTIeUvGiR/rustc_testrunner_tmpdir_reporting.inyB8dpg3IHG/mvce.rs:7:23
  |
7 |             println!("{a:?} {b:?}");
  |                       ^^^^^ `DropMe` cannot be formatted using `{:?}`
  |
  = help: the trait `Debug` is not implemented for `DropMe`
  = note: add `#[derive(Debug)]` to `DropMe` or manually `impl Debug for DropMe`
  = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider annotating `DropMe` with `#[derive(Debug)]`
  |
1 + #[derive(Debug)]
2 | struct DropMe();
  |

error[E0277]: `DropMe` doesn't implement `Debug`
 --> /tmp/icemaker_global_tempdir.nBTpTIeUvGiR/rustc_testrunner_tmpdir_reporting.inyB8dpg3IHG/mvce.rs:7:29
  |
7 |             println!("{a:?} {b:?}");
  |                             ^^^^^ `DropMe` cannot be formatted using `{:?}`
  |
  = help: the trait `Debug` is not implemented for `DropMe`
  = note: add `#[derive(Debug)]` to `DropMe` or manually `impl Debug for DropMe`
  = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider annotating `DropMe` with `#[derive(Debug)]`
  |
1 + #[derive(Debug)]
2 | struct DropMe();
  |

error[E0425]: cannot find function `block_on` in this scope
 --> /tmp/icemaker_global_tempdir.nBTpTIeUvGiR/rustc_testrunner_tmpdir_reporting.inyB8dpg3IHG/mvce.rs:4:5
  |
4 |     block_on(async {
  |     ^^^^^^^^ not found in this scope

thread 'rustc' panicked at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/compiler/rustc_middle/src/ty/closure.rs:465:9:
assertion `left == right` failed: leftover child captures?
  left: Some((0, CapturedPlace { var_ident: b#0, place: Place { base_ty: DropMe, base: Upvar(UpvarId(HirId(DefId(0:5 ~ mvce[e21a]::main).20);`b`;DefId(0:8 ~ mvce[e21a]::main::{closure#0}::{closure#0}::{closure#0}))), projections: [] }, info: CaptureInfo { capture_kind_expr_id: Some(HirId(DefId(0:5 ~ mvce[e21a]::main).55)), path_expr_id: Some(HirId(DefId(0:5 ~ mvce[e21a]::main).55)), capture_kind: ByRef(ImmBorrow) }, mutability: Not, region: Some('?29) }))
 right: None
stack backtrace:
   0:     0x767a5c576aa5 - std::backtrace_rs::backtrace::libunwind::trace::hc64e86e536cccd39
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5
   1:     0x767a5c576aa5 - std::backtrace_rs::backtrace::trace_unsynchronized::hc2ca63e7f9339807
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x767a5c576aa5 - std::sys_common::backtrace::_print_fmt::hd6ea6ce7e4523fb4
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x767a5c576aa5 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hd49e39a4e78f23fb
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x767a5c5c5f5b - core::fmt::rt::Argument::fmt::hc21c1343594dc806
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/core/src/fmt/rt.rs:142:9
   5:     0x767a5c5c5f5b - core::fmt::write::hb7e332df30c073e5
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/core/src/fmt/mod.rs:1153:17
   6:     0x767a5c56b6af - std::io::Write::write_fmt::h8c9d6dd69b406bef
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/std/src/io/mod.rs:1850:15
   7:     0x767a5c57687e - std::sys_common::backtrace::_print::hca95aad17316b560
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x767a5c57687e - std::sys_common::backtrace::print::hd755949dac5dd63c
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x767a5c579379 - std::panicking::default_hook::{{closure}}::h26c692f66d8d7682
  10:     0x767a5c579095 - std::panicking::default_hook::hab2f746d14a92384
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/std/src/panicking.rs:291:9
  11:     0x767a58e6eedb - std[7fa9ae6261bd978e]::panicking::update_hook::<alloc[39dac30fc45a9f99]::boxed::Box<rustc_driver_impl[66ddb612e69c0ea9]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x767a5c579a7c - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h996866a35ba08d6f
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/alloc/src/boxed.rs:2032:9
  13:     0x767a5c579a7c - std::panicking::rust_panic_with_hook::hd92d2e2a13ecb66f
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/std/src/panicking.rs:792:13
  14:     0x767a5c579826 - std::panicking::begin_panic_handler::{{closure}}::hef95901e47852a53
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/std/src/panicking.rs:657:13
  15:     0x767a5c576f69 - std::sys_common::backtrace::__rust_end_short_backtrace::h953c748de2483f60
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/std/src/sys_common/backtrace.rs:171:18
  16:     0x767a5c579557 - rust_begin_unwind
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/std/src/panicking.rs:645:5
  17:     0x767a5c5c23f6 - core::panicking::panic_fmt::h6ebc23c163efa34d
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/core/src/panicking.rs:72:14
  18:     0x767a5c5c29bf - core::panicking::assert_failed_inner::h2d379fdfc6a3c525
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/core/src/panicking.rs:397:23
  19:     0x767a58fee47b - core[2857ca4be7b8aaf3]::panicking::assert_failed::<core[2857ca4be7b8aaf3]::option::Option<(usize, &rustc_middle[d6a30e1310c10791]::ty::closure::CapturedPlace)>, core[2857ca4be7b8aaf3]::option::Option<(usize, &rustc_middle[d6a30e1310c10791]::ty::closure::CapturedPlace)>>
  20:     0x767a5901c7c1 - <core[2857ca4be7b8aaf3]::iter::sources::from_coroutine::FromCoroutine<rustc_middle[d6a30e1310c10791]::ty::closure::analyze_coroutine_closure_captures<rustc_middle[d6a30e1310c10791]::ty::Ty, core[2857ca4be7b8aaf3]::iter::adapters::flatten::Flatten<core[2857ca4be7b8aaf3]::option::IntoIter<core[2857ca4be7b8aaf3]::iter::adapters::flatten::FlatMap<indexmap[a281c313777efd8]::map::iter::Values<rustc_hir[cef4958ef576219d]::hir_id::HirId, alloc[39dac30fc45a9f99]::vec::Vec<rustc_middle[d6a30e1310c10791]::ty::closure::CapturedPlace>>, core[2857ca4be7b8aaf3]::slice::iter::Iter<rustc_middle[d6a30e1310c10791]::ty::closure::CapturedPlace>, <rustc_middle[d6a30e1310c10791]::ty::typeck_results::TypeckResults>::closure_min_captures_flattened::{closure#0}::{closure#0}>>>, core[2857ca4be7b8aaf3]::iter::adapters::skip::Skip<core[2857ca4be7b8aaf3]::iter::adapters::flatten::Flatten<core[2857ca4be7b8aaf3]::option::IntoIter<core[2857ca4be7b8aaf3]::iter::adapters::flatten::FlatMap<indexmap[a281c313777efd8]::map::iter::Values<rustc_hir[cef4958ef576219d]::hir_id::HirId, alloc[39dac30fc45a9f99]::vec::Vec<rustc_middle[d6a30e1310c10791]::ty::closure::CapturedPlace>>, core[2857ca4be7b8aaf3]::slice::iter::Iter<rustc_middle[d6a30e1310c10791]::ty::closure::CapturedPlace>, <rustc_middle[d6a30e1310c10791]::ty::typeck_results::TypeckResults>::closure_min_captures_flattened::{closure#0}::{closure#0}>>>>, <rustc_hir_typeck[314e90422a25665d]::fn_ctxt::FnCtxt>::analyze_closure::{closure#0}>::{closure#0}> as core[2857ca4be7b8aaf3]::iter::traits::iterator::Iterator>::next
  21:     0x767a5ab9a1f0 - <rustc_hir_typeck[314e90422a25665d]::fn_ctxt::FnCtxt>::analyze_closure
  22:     0x767a572e981b - <rustc_hir_typeck[314e90422a25665d]::upvar::InferBorrowKindVisitor as rustc_hir[cef4958ef576219d]::intravisit::Visitor>::visit_expr
  23:     0x767a5a565717 - <rustc_hir_typeck[314e90422a25665d]::upvar::InferBorrowKindVisitor as rustc_hir[cef4958ef576219d]::intravisit::Visitor>::visit_block
  24:     0x767a572e97f4 - <rustc_hir_typeck[314e90422a25665d]::upvar::InferBorrowKindVisitor as rustc_hir[cef4958ef576219d]::intravisit::Visitor>::visit_expr
  25:     0x767a572e9522 - <rustc_hir_typeck[314e90422a25665d]::upvar::InferBorrowKindVisitor as rustc_hir[cef4958ef576219d]::intravisit::Visitor>::visit_expr
  26:     0x767a5a5656c4 - <rustc_hir_typeck[314e90422a25665d]::upvar::InferBorrowKindVisitor as rustc_hir[cef4958ef576219d]::intravisit::Visitor>::visit_block
  27:     0x767a5a56799e - rustc_hir_typeck[314e90422a25665d]::typeck
  28:     0x767a5a567089 - rustc_query_impl[6012edbe18cba507]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[6012edbe18cba507]::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[d6a30e1310c10791]::query::erase::Erased<[u8; 8usize]>>
  29:     0x767a5a57fd71 - rustc_query_system[b38cadec3f440a87]::query::plumbing::try_execute_query::<rustc_query_impl[6012edbe18cba507]::DynamicConfig<rustc_query_system[b38cadec3f440a87]::query::caches::VecCache<rustc_span[ffa680444b6ed2b5]::def_id::LocalDefId, rustc_middle[d6a30e1310c10791]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[6012edbe18cba507]::plumbing::QueryCtxt, false>
  30:     0x767a5a57e68c - rustc_query_impl[6012edbe18cba507]::query_impl::typeck::get_query_non_incr::__rust_end_short_backtrace
  31:     0x767a5a57e254 - <rustc_middle[d6a30e1310c10791]::hir::map::Map>::par_body_owners::<rustc_hir_analysis[f834b18f34f102e8]::check_crate::{closure#4}>::{closure#0}
  32:     0x767a5a57cf20 - rustc_hir_analysis[f834b18f34f102e8]::check_crate
  33:     0x767a5aad713a - rustc_interface[603537c6de686282]::passes::analysis
  34:     0x767a5aad6c95 - rustc_query_impl[6012edbe18cba507]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[6012edbe18cba507]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[d6a30e1310c10791]::query::erase::Erased<[u8; 1usize]>>
  35:     0x767a5b053025 - rustc_query_system[b38cadec3f440a87]::query::plumbing::try_execute_query::<rustc_query_impl[6012edbe18cba507]::DynamicConfig<rustc_query_system[b38cadec3f440a87]::query::caches::SingleCache<rustc_middle[d6a30e1310c10791]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[6012edbe18cba507]::plumbing::QueryCtxt, false>
  36:     0x767a5b052d89 - rustc_query_impl[6012edbe18cba507]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  37:     0x767a5aec16d3 - rustc_interface[603537c6de686282]::interface::run_compiler::<core[2857ca4be7b8aaf3]::result::Result<(), rustc_span[ffa680444b6ed2b5]::ErrorGuaranteed>, rustc_driver_impl[66ddb612e69c0ea9]::run_compiler::{closure#0}>::{closure#0}
  38:     0x767a5afbbe9d - std[7fa9ae6261bd978e]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[603537c6de686282]::util::run_in_thread_with_globals<rustc_interface[603537c6de686282]::util::run_in_thread_pool_with_globals<rustc_interface[603537c6de686282]::interface::run_compiler<core[2857ca4be7b8aaf3]::result::Result<(), rustc_span[ffa680444b6ed2b5]::ErrorGuaranteed>, rustc_driver_impl[66ddb612e69c0ea9]::run_compiler::{closure#0}>::{closure#0}, core[2857ca4be7b8aaf3]::result::Result<(), rustc_span[ffa680444b6ed2b5]::ErrorGuaranteed>>::{closure#0}, core[2857ca4be7b8aaf3]::result::Result<(), rustc_span[ffa680444b6ed2b5]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[2857ca4be7b8aaf3]::result::Result<(), rustc_span[ffa680444b6ed2b5]::ErrorGuaranteed>>
  39:     0x767a5afbbcaa - <<std[7fa9ae6261bd978e]::thread::Builder>::spawn_unchecked_<rustc_interface[603537c6de686282]::util::run_in_thread_with_globals<rustc_interface[603537c6de686282]::util::run_in_thread_pool_with_globals<rustc_interface[603537c6de686282]::interface::run_compiler<core[2857ca4be7b8aaf3]::result::Result<(), rustc_span[ffa680444b6ed2b5]::ErrorGuaranteed>, rustc_driver_impl[66ddb612e69c0ea9]::run_compiler::{closure#0}>::{closure#0}, core[2857ca4be7b8aaf3]::result::Result<(), rustc_span[ffa680444b6ed2b5]::ErrorGuaranteed>>::{closure#0}, core[2857ca4be7b8aaf3]::result::Result<(), rustc_span[ffa680444b6ed2b5]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[2857ca4be7b8aaf3]::result::Result<(), rustc_span[ffa680444b6ed2b5]::ErrorGuaranteed>>::{closure#1} as core[2857ca4be7b8aaf3]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  40:     0x767a5c58397b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hb9eb94e208a8e5e4
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/alloc/src/boxed.rs:2018:9
  41:     0x767a5c58397b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::he4de6486bae508a1
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/alloc/src/boxed.rs:2018:9
  42:     0x767a5c58397b - std::sys::pal::unix::thread::Thread::new::thread_start::ha731ee708af4d0a8
                               at /rustc/df7daa815f4047a1cfe24ecfbc12cfd4f40fa928/library/std/src/sys/pal/unix/thread.rs:108:17
  43:     0x767a55ca955a - <unknown>
  44:     0x767a55d26a3c - <unknown>
  45:                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.79.0-nightly (df7daa815 2024-04-11) running on x86_64-unknown-linux-gnu

note: compiler flags: -Z dump-mir-dir=dir

query stack during panic:
#0 [typeck] type-checking `main`
#1 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 7 previous errors

Some errors have detailed explanations: E0061, E0277, E0425, E0658.
For more information about an error, try `rustc --explain E0061`.

Metadata

Metadata

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.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions