Skip to content
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

Compute layout with spans for better cycle errors in coroutines #117858

Merged
merged 1 commit into from
Nov 13, 2023
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
7 changes: 5 additions & 2 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
// debuggers and debugger extensions expect it to be called `__awaitee`. They use
// this name to identify what is being awaited by a suspended async functions.
let awaitee_ident = Ident::with_dummy_span(sym::__awaitee);
let (awaitee_pat, awaitee_pat_hid) =
self.pat_ident_binding_mode(span, awaitee_ident, hir::BindingAnnotation::MUT);
let (awaitee_pat, awaitee_pat_hid) = self.pat_ident_binding_mode(
gen_future_span,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use the whole expression span here rather than the span of the .await, when pointing to the local that we use to store the IntoFuture::into_future result. That's because the user probably cares more about the whole async call, not just the fact that the future was awaited.

This matters a lot more when pointing out recursive calls in #117703, but it does make the span a bit better here too.

awaitee_ident,
hir::BindingAnnotation::MUT,
);

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

Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_ty_utils/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,11 +740,11 @@ fn coroutine_layout<'tcx>(
};
let tag_layout = cx.tcx.mk_layout(LayoutS::scalar(cx, tag));

let promoted_layouts = ineligible_locals
.iter()
.map(|local| subst_field(info.field_tys[local].ty))
.map(|ty| Ty::new_maybe_uninit(tcx, ty))
.map(|ty| Ok(cx.layout_of(ty)?.layout));
let promoted_layouts = ineligible_locals.iter().map(|local| {
let field_ty = subst_field(info.field_tys[local].ty);
let uninit_ty = Ty::new_maybe_uninit(tcx, field_ty);
Ok(cx.spanned_layout_of(uninit_ty, info.field_tys[local].source_info.span)?.layout)
});
let prefix_layouts = args
.as_coroutine()
.prefix_tys()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
Static,
),
source_info: SourceInfo {
span: $DIR/async_await.rs:16:9: 16:14 (#8),
span: $DIR/async_await.rs:16:5: 16:14 (#9),
scope: scope[0],
},
ignore_for_traits: false,
Expand All @@ -32,7 +32,7 @@
Static,
),
source_info: SourceInfo {
span: $DIR/async_await.rs:17:9: 17:14 (#10),
span: $DIR/async_await.rs:17:5: 17:14 (#11),
scope: scope[0],
},
ignore_for_traits: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// edition: 2021
// build-fail
//~^^ ERROR cycle detected when computing layout of

#![feature(impl_trait_in_assoc_type)]

Expand All @@ -21,6 +20,7 @@ impl Recur for () {

fn recur(self) -> Self::Recur {
async move { recur(self).await; }
//~^ ERROR cycle detected when computing layout of
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
error[E0391]: cycle detected when computing layout of `{async block@$DIR/indirect-recursion-issue-112047.rs:23:9: 23:42}`
error[E0391]: cycle detected when computing layout of `{async block@$DIR/indirect-recursion-issue-112047.rs:22:9: 22:42}`
--> $DIR/indirect-recursion-issue-112047.rs:22:22
|
= note: ...which requires computing layout of `core::mem::maybe_uninit::MaybeUninit<{async fn body@$DIR/indirect-recursion-issue-112047.rs:15:31: 17:2}>`...
= note: ...which requires computing layout of `core::mem::manually_drop::ManuallyDrop<{async fn body@$DIR/indirect-recursion-issue-112047.rs:15:31: 17:2}>`...
= note: ...which requires computing layout of `{async fn body@$DIR/indirect-recursion-issue-112047.rs:15:31: 17:2}`...
LL | async move { recur(self).await; }
| ^^^^^^^^^^^^^^^^^
|
= note: ...which requires computing layout of `core::mem::maybe_uninit::MaybeUninit<{async fn body@$DIR/indirect-recursion-issue-112047.rs:14:31: 16:2}>`...
= note: ...which requires computing layout of `core::mem::manually_drop::ManuallyDrop<{async fn body@$DIR/indirect-recursion-issue-112047.rs:14:31: 16:2}>`...
note: ...which requires computing layout of `{async fn body@$DIR/indirect-recursion-issue-112047.rs:14:31: 16:2}`...
--> $DIR/indirect-recursion-issue-112047.rs:15:5
|
LL | t.recur().await;
| ^^^^^^^^^^^^^^^
= note: ...which requires computing layout of `core::mem::maybe_uninit::MaybeUninit<<() as Recur>::Recur>`...
= note: ...which requires computing layout of `core::mem::maybe_uninit::MaybeUninit<{async block@$DIR/indirect-recursion-issue-112047.rs:23:9: 23:42}>`...
= note: ...which requires computing layout of `core::mem::manually_drop::ManuallyDrop<{async block@$DIR/indirect-recursion-issue-112047.rs:23:9: 23:42}>`...
= note: ...which again requires computing layout of `{async block@$DIR/indirect-recursion-issue-112047.rs:23:9: 23:42}`, completing the cycle
note: cycle used when elaborating drops for `<impl at $DIR/indirect-recursion-issue-112047.rs:19:1: 19:18>::recur`
--> $DIR/indirect-recursion-issue-112047.rs:22:5
= note: ...which requires computing layout of `core::mem::maybe_uninit::MaybeUninit<{async block@$DIR/indirect-recursion-issue-112047.rs:22:9: 22:42}>`...
= note: ...which requires computing layout of `core::mem::manually_drop::ManuallyDrop<{async block@$DIR/indirect-recursion-issue-112047.rs:22:9: 22:42}>`...
= note: ...which again requires computing layout of `{async block@$DIR/indirect-recursion-issue-112047.rs:22:9: 22:42}`, completing the cycle
note: cycle used when elaborating drops for `<impl at $DIR/indirect-recursion-issue-112047.rs:18:1: 18:18>::recur`
--> $DIR/indirect-recursion-issue-112047.rs:21:5
|
LL | fn recur(self) -> Self::Recur {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
Loading