Skip to content

Commit 05b8202

Browse files
Rollup merge of rust-lang#158904 - KR-bluejay:issue-142559-fix, r=oli-obk
Fix FutureDropPoll shim for by-move async closures Related rust-lang#142559 When the coroutine is coroutine-closures, `build_adrop_for_coroutine_shim` used the the ref of the coroutine body. This PR uses `coroutine_by_move_body_def_id` to fetch the by-move body. (matching the existing `DropGlue` behavior in `shim.rs`)
2 parents 6105f33 + b9ad974 commit 05b8202

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

compiler/rustc_mir_transform/src/shim/async_destructor_ctor.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,23 @@ fn build_adrop_for_coroutine_shim<'tcx>(
203203
let ty::Coroutine(coroutine_def_id, impl_args) = impl_ty.kind() else {
204204
bug!("build_adrop_for_coroutine_shim not for coroutine impl type: ({:?})", shim);
205205
};
206+
let ty::Coroutine(_, id_args) = *tcx.type_of(*coroutine_def_id).skip_binder().kind() else {
207+
bug!()
208+
};
206209
let source_info = SourceInfo::outermost(span);
207-
let body = tcx.optimized_mir(*coroutine_def_id).future_drop_poll().unwrap();
210+
211+
// If the kind tys differ, we must use the by-move body
212+
let def_id = if id_args.as_coroutine().kind_ty() == impl_args.as_coroutine().kind_ty() {
213+
*coroutine_def_id
214+
} else {
215+
assert_eq!(
216+
impl_args.as_coroutine().kind_ty().to_opt_closure_kind().unwrap(),
217+
ty::ClosureKind::FnOnce
218+
);
219+
220+
tcx.coroutine_by_move_body_def_id(*coroutine_def_id)
221+
};
222+
let body = tcx.optimized_mir(def_id).future_drop_poll().unwrap();
208223
let mut body: Body<'tcx> =
209224
EarlyBinder::bind(tcx, body.clone()).instantiate(tcx, impl_args).skip_norm_wip();
210225
body.source.instance = ty::InstanceKind::Shim(shim);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Regression test for #142559
2+
//@ build-pass
3+
//@ compile-flags: --crate-type=lib
4+
#![feature(async_drop)]
5+
#![allow(incomplete_features)]
6+
7+
//@ edition: 2024
8+
9+
async fn run<F: Future>(f: impl Fn() -> F) {
10+
f().await;
11+
}
12+
13+
pub async fn async_drop_async_closure() {
14+
let x = async || async {}.await;
15+
16+
run(x).await;
17+
}

0 commit comments

Comments
 (0)