Skip to content

Conversation

azhogin
Copy link
Contributor

@azhogin azhogin commented Aug 12, 2025

Support for Box async drop.
Support for dyn traits async drop.

This is a draft PR because of experimental solution for dyn traits support.

Dyn traits async drop requires additional vtable slot 'COMMON_VTABLE_ENTRIES_ASYNCDROPINPLACE', similar to 'COMMON_VTABLE_ENTRIES_DROPINPLACE'. This slot exists in any vtable, just without 'async_drop' feature it is set to null.

'AsyncDropInPlaceDyn' is lang item function declared in std library (in alloc):

/// Async drop for usage in vtable
#[lang = "async_drop_in_place_dyn"]
pub unsafe fn async_drop_in_place_dyn<T: ?Sized + 'static>(
    to_drop: *mut T,
) -> Pin<Box<dyn Future<Output = ()>>> {
    Box::pin(unsafe { async_drop_in_place(to_drop) })
}

This function converts 'async_drop_in_place::{closure}' to common 'Pin<Box<dyn Future<Output = ()>>>'.
And this future, provided by virtual call, is polled in the similar way as 'ordinary' async drop features.

Dyn async drop is expanded in StateTransform into AsyncDropInPlaceDyn lang_item call and is codegen'ed later to virtual call (in the similar way as virtual sync drop call, just with return value). Only implemented in ssa codegen yet.

async_drop_in_place<T>::{closure} is a special case for vtable generation, because such coroutine is its async drop future itself.
To drop this coroutine we need to continue poll it. So, its async drop constructor function in vtable returns its address (from argument), boxed and pinned. 'async_drop_in_place_self' lang item function is used for it.

#[lang = "async_drop_in_place_self"]
pub unsafe fn async_drop_in_place_self(
    to_drop: *mut dyn Future<Output = ()>,
) -> Pin<Box<dyn Future<Output = ()>>> {
    Box::into_pin(unsafe { Box::from_raw(to_drop) })
}

Fixes #143658.

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 12, 2025
@petrochenkov petrochenkov added the F-async_drop `#![feature(async_drop)]` label Aug 13, 2025
@azhogin azhogin force-pushed the azhogin/async-drop-box-support branch from 54fef57 to cfc843d Compare August 13, 2025 17:37
@azhogin
Copy link
Contributor Author

azhogin commented Aug 13, 2025

r? @oli-obk

@rustbot
Copy link
Collaborator

rustbot commented Aug 13, 2025

oli-obk is not on the review rotation at the moment.
They may take a while to respond.

@azhogin azhogin force-pushed the azhogin/async-drop-box-support branch from cfc843d to a5631d1 Compare August 18, 2025 06:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. F-async_drop `#![feature(async_drop)]` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AsyncDrop not called when type is inside Box
4 participants