Skip to content

Async drop - fix for StorageLive/StorageDead codegen for pinned future #142347

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions compiler/rustc_mir_transform/src/coroutine/drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ fn build_poll_switch<'tcx>(
body: &mut Body<'tcx>,
poll_enum: Ty<'tcx>,
poll_unit_place: &Place<'tcx>,
fut_pin_place: &Place<'tcx>,
ready_block: BasicBlock,
yield_block: BasicBlock,
) -> BasicBlock {
Expand Down Expand Up @@ -162,9 +163,11 @@ fn build_poll_switch<'tcx>(
Rvalue::Discriminant(*poll_unit_place),
))),
};
let storage_dead =
Statement { source_info, kind: StatementKind::StorageDead(fut_pin_place.local) };
let unreachable_block = insert_term_block(body, TerminatorKind::Unreachable);
body.basic_blocks_mut().push(BasicBlockData {
statements: [discr_assign].to_vec(),
statements: [storage_dead, discr_assign].to_vec(),
terminator: Some(Terminator {
source_info,
kind: TerminatorKind::SwitchInt {
Expand Down Expand Up @@ -332,10 +335,17 @@ pub(super) fn expand_async_drops<'tcx>(
kind: StatementKind::Assign(Box::new((context_ref_place, arg))),
});
let yield_block = insert_term_block(body, TerminatorKind::Unreachable); // `kind` replaced later to yield
let switch_block =
build_poll_switch(tcx, body, poll_enum, &poll_unit_place, target, yield_block);
let (pin_bb, fut_pin_place) =
build_pin_fut(tcx, body, fut_place.clone(), UnwindAction::Continue);
let switch_block = build_poll_switch(
tcx,
body,
poll_enum,
&poll_unit_place,
&fut_pin_place,
target,
yield_block,
);
let call_bb = build_poll_call(
tcx,
body,
Expand All @@ -357,16 +367,17 @@ pub(super) fn expand_async_drops<'tcx>(
body.local_decls.push(LocalDecl::new(context_mut_ref, source_info.span)),
);
let drop_yield_block = insert_term_block(body, TerminatorKind::Unreachable); // `kind` replaced later to yield
let (pin_bb2, fut_pin_place2) =
build_pin_fut(tcx, body, fut_place, UnwindAction::Continue);
let drop_switch_block = build_poll_switch(
tcx,
body,
poll_enum,
&poll_unit_place,
&fut_pin_place2,
drop.unwrap(),
drop_yield_block,
);
let (pin_bb2, fut_pin_place2) =
build_pin_fut(tcx, body, fut_place, UnwindAction::Continue);
let drop_call_bb = build_poll_call(
tcx,
body,
Expand Down
14 changes: 14 additions & 0 deletions compiler/rustc_mir_transform/src/elaborate_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,20 @@ where
Location { block: self.succ, statement_index: 0 },
StatementKind::StorageDead(fut.local),
);
// StorageDead(fut) in unwind block (at the begin)
if let Unwind::To(block) = unwind {
self.elaborator.patch().add_statement(
Location { block, statement_index: 0 },
StatementKind::StorageDead(fut.local),
);
}
// StorageDead(fut) in dropline block (at the begin)
if let Some(block) = dropline {
self.elaborator.patch().add_statement(
Location { block, statement_index: 0 },
StatementKind::StorageDead(fut.local),
);
}

// #1:pin_obj_bb >>> call Pin<ObjTy>::new_unchecked(&mut obj)
self.elaborator.patch().patch_terminator(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// MIR for `a::{closure#0}` 0 coroutine_drop_async

fn a::{closure#0}(_1: Pin<&mut {async fn body of a<T>()}>, _2: &mut Context<'_>) -> Poll<()> {
debug _task_context => _19;
debug x => ((*(_1.0: &mut {async fn body of a<T>()})).0: T);
let mut _0: std::task::Poll<()>;
let _3: T;
let mut _4: impl std::future::Future<Output = ()>;
let mut _5: &mut T;
let mut _6: std::pin::Pin<&mut T>;
let mut _7: &mut T;
let mut _8: *mut T;
let mut _9: ();
let mut _10: std::task::Poll<()>;
let mut _11: &mut std::task::Context<'_>;
let mut _12: &mut impl std::future::Future<Output = ()>;
let mut _13: std::pin::Pin<&mut impl std::future::Future<Output = ()>>;
let mut _14: isize;
let mut _15: &mut std::task::Context<'_>;
let mut _16: &mut impl std::future::Future<Output = ()>;
let mut _17: std::pin::Pin<&mut impl std::future::Future<Output = ()>>;
let mut _18: isize;
let mut _19: &mut std::task::Context<'_>;
let mut _20: u32;
scope 1 {
debug x => (((*(_1.0: &mut {async fn body of a<T>()})) as variant#4).0: T);
}

bb0: {
_20 = discriminant((*(_1.0: &mut {async fn body of a<T>()})));
switchInt(move _20) -> [0: bb9, 3: bb12, 4: bb13, otherwise: bb14];
}

bb1: {
nop;
nop;
goto -> bb2;
}

bb2: {
_0 = Poll::<()>::Ready(const ());
return;
}

bb3: {
_0 = Poll::<()>::Pending;
discriminant((*(_1.0: &mut {async fn body of a<T>()}))) = 4;
return;
}

bb4: {
StorageLive(_17);
_16 = &mut (((*(_1.0: &mut {async fn body of a<T>()})) as variant#4).1: impl std::future::Future<Output = ()>);
_17 = Pin::<&mut impl Future<Output = ()>>::new_unchecked(move _16) -> [return: bb7, unwind unreachable];
}

bb5: {
unreachable;
}

bb6: {
StorageDead(_17);
_18 = discriminant(_10);
switchInt(move _18) -> [0: bb1, 1: bb3, otherwise: bb5];
}

bb7: {
_10 = <impl Future<Output = ()> as Future>::poll(move _17, move _15) -> [return: bb6, unwind unreachable];
}

bb8: {
_0 = Poll::<()>::Ready(const ());
return;
}

bb9: {
goto -> bb11;
}

bb10: {
goto -> bb8;
}

bb11: {
drop(((*(_1.0: &mut {async fn body of a<T>()})).0: T)) -> [return: bb10, unwind unreachable];
}

bb12: {
goto -> bb4;
}

bb13: {
goto -> bb4;
}

bb14: {
_0 = Poll::<()>::Ready(const ());
return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// MIR for `a::{closure#0}` 0 coroutine_drop_async

fn a::{closure#0}(_1: Pin<&mut {async fn body of a<T>()}>, _2: &mut Context<'_>) -> Poll<()> {
debug _task_context => _19;
debug x => ((*(_1.0: &mut {async fn body of a<T>()})).0: T);
let mut _0: std::task::Poll<()>;
let _3: T;
let mut _4: impl std::future::Future<Output = ()>;
let mut _5: &mut T;
let mut _6: std::pin::Pin<&mut T>;
let mut _7: &mut T;
let mut _8: *mut T;
let mut _9: ();
let mut _10: std::task::Poll<()>;
let mut _11: &mut std::task::Context<'_>;
let mut _12: &mut impl std::future::Future<Output = ()>;
let mut _13: std::pin::Pin<&mut impl std::future::Future<Output = ()>>;
let mut _14: isize;
let mut _15: &mut std::task::Context<'_>;
let mut _16: &mut impl std::future::Future<Output = ()>;
let mut _17: std::pin::Pin<&mut impl std::future::Future<Output = ()>>;
let mut _18: isize;
let mut _19: &mut std::task::Context<'_>;
let mut _20: u32;
scope 1 {
debug x => (((*(_1.0: &mut {async fn body of a<T>()})) as variant#4).0: T);
}

bb0: {
_20 = discriminant((*(_1.0: &mut {async fn body of a<T>()})));
switchInt(move _20) -> [0: bb12, 2: bb18, 3: bb16, 4: bb17, otherwise: bb19];
}

bb1: {
nop;
nop;
goto -> bb2;
}

bb2: {
_0 = Poll::<()>::Ready(const ());
return;
}

bb3 (cleanup): {
nop;
nop;
goto -> bb5;
}

bb4 (cleanup): {
goto -> bb15;
}

bb5 (cleanup): {
goto -> bb4;
}

bb6: {
_0 = Poll::<()>::Pending;
discriminant((*(_1.0: &mut {async fn body of a<T>()}))) = 4;
return;
}

bb7: {
StorageLive(_17);
_16 = &mut (((*(_1.0: &mut {async fn body of a<T>()})) as variant#4).1: impl std::future::Future<Output = ()>);
_17 = Pin::<&mut impl Future<Output = ()>>::new_unchecked(move _16) -> [return: bb10, unwind: bb15];
}

bb8: {
unreachable;
}

bb9: {
StorageDead(_17);
_18 = discriminant(_10);
switchInt(move _18) -> [0: bb1, 1: bb6, otherwise: bb8];
}

bb10: {
_10 = <impl Future<Output = ()> as Future>::poll(move _17, move _15) -> [return: bb9, unwind: bb3];
}

bb11: {
_0 = Poll::<()>::Ready(const ());
return;
}

bb12: {
goto -> bb14;
}

bb13: {
goto -> bb11;
}

bb14: {
drop(((*(_1.0: &mut {async fn body of a<T>()})).0: T)) -> [return: bb13, unwind: bb4];
}

bb15 (cleanup): {
discriminant((*(_1.0: &mut {async fn body of a<T>()}))) = 2;
resume;
}

bb16: {
goto -> bb7;
}

bb17: {
goto -> bb7;
}

bb18: {
assert(const false, "`async fn` resumed after panicking") -> [success: bb18, unwind continue];
}

bb19: {
_0 = Poll::<()>::Ready(const ());
return;
}
}
11 changes: 11 additions & 0 deletions tests/mir-opt/async_drop_live_dead.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//@ edition:2024
// skip-filecheck
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY

#![feature(async_drop)]
#![allow(incomplete_features)]

// EMIT_MIR async_drop_live_dead.a-{closure#0}.coroutine_drop_async.0.mir
async fn a<T>(x: T) {}

fn main() {}
2 changes: 1 addition & 1 deletion tests/ui/async-await/async-drop/async-drop-initial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn main() {
test_async_drop(&j, 16).await;
test_async_drop(
AsyncStruct { b: AsyncInt(8), a: AsyncInt(7), i: 6 },
if cfg!(panic = "unwind") { 168 } else { 136 },
136,
).await;
test_async_drop(ManuallyDrop::new(AsyncInt(9)), 16).await;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//@ known-bug: #140429
// ex-ice: #140429
//@ compile-flags: -Zlint-mir --crate-type lib
//@ edition:2024
//@ check-pass

#![feature(async_drop)]
#![allow(incomplete_features)]

async fn a<T>(x: T) {}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//@ known-bug: #140531
//@compile-flags: -Zlint-mir --crate-type lib
// ex-ice: #140531
//@ compile-flags: -Zlint-mir --crate-type lib
//@ edition:2024
//@ check-pass

#![feature(async_drop)]
#![allow(incomplete_features)]

async fn call_once(f: impl AsyncFnOnce()) {
let fut = Box::pin(f());
}
Loading
Loading