Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2c5b029

Browse files
committedNov 13, 2023
Auto merge of rust-lang#117703 - compiler-errors:recursive-async, r=<try>
Support async recursive calls (as long as they have indirection) Before rust-lang#101692, we stored coroutine witness types directly inside of the coroutine. That means that a coroutine could not contain itself (as a witness field) without creating a cycle in the type representation of the coroutine, which we detected with the `OpaqueTypeExpander`, which is used to detect cycles when expanding opaque types after that are inferred to contain themselves. After `-Zdrop-tracking-mir` was stabilized, we no longer store these generator witness fields directly, but instead behind a def-id based query. That means there is no technical obstacle in the compiler preventing coroutines from containing themselves per se, other than the fact that for a coroutine to have a non-infinite layout, it must contain itself wrapped in a layer of allocation indirection (like a `Box`). This means that it should be valid for this code to work: ``` async fn async_fibonacci(i: u32) -> u32 { if i == 0 || i == 1 { i } else { Box::pin(async_fibonacci(i - 1)).await + Box::pin(async_fibonacci(i - 2)).await } } ``` Whereas previously, you'd need to coerce the future to `Pin<Box<dyn Future<Output = ...>>` before `await`ing it, to prevent the async's desugared coroutine from containing itself across as await point. This PR does two things: 1. Remove the behavior from `OpaqueTypeExpander` where it intentionally fetches and walks through the coroutine's witness fields. This was kept around after `-Zdrop-tracking-mir` was stabilized so we would not be introducing new stable behavior, and to preserve the much better diagnostics of async recursion compared to a layout error. 2. Reworks the way we report layout errors having to do with coroutines, to make up for the diagnostic regressions introduced by (1.). We actually do even better now, pointing out the call sites of the recursion!
2 parents 0828c15 + 19eb35e commit 2c5b029

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+310
-189
lines changed
 

‎compiler/rustc_ast_lowering/src/expr.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -792,8 +792,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
792792
// debuggers and debugger extensions expect it to be called `__awaitee`. They use
793793
// this name to identify what is being awaited by a suspended async functions.
794794
let awaitee_ident = Ident::with_dummy_span(sym::__awaitee);
795-
let (awaitee_pat, awaitee_pat_hid) =
796-
self.pat_ident_binding_mode(span, awaitee_ident, hir::BindingAnnotation::MUT);
795+
let (awaitee_pat, awaitee_pat_hid) = self.pat_ident_binding_mode(
796+
gen_future_span,
797+
awaitee_ident,
798+
hir::BindingAnnotation::MUT,
799+
);
797800

798801
let task_context_ident = Ident::with_dummy_span(sym::_task_context);
799802

‎compiler/rustc_hir/src/hir.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,15 @@ pub enum CoroutineKind {
15291529
Coroutine,
15301530
}
15311531

1532+
impl CoroutineKind {
1533+
pub fn is_fn_like(self) -> bool {
1534+
matches!(
1535+
self,
1536+
CoroutineKind::Async(CoroutineSource::Fn) | CoroutineKind::Gen(CoroutineSource::Fn)
1537+
)
1538+
}
1539+
}
1540+
15321541
impl fmt::Display for CoroutineKind {
15331542
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15341543
match self {

0 commit comments

Comments
 (0)
Please sign in to comment.