Skip to content

Only generate closure def id for async fns with body #102244

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 2 commits into from
Sep 26, 2022
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
13 changes: 6 additions & 7 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,9 +1055,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
asyncness: Async,
body: Option<&Block>,
) -> hir::BodyId {
let closure_id = match asyncness {
Async::Yes { closure_id, .. } => closure_id,
Async::No => return self.lower_fn_body_block(span, decl, body),
let (closure_id, body) = match (asyncness, body) {
Copy link
Member Author

@compiler-errors compiler-errors Sep 24, 2022

Choose a reason for hiding this comment

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

This means that when a (non-trait) async fn is lacking a body, we now lower it to <ExprKind::Err> instead of wrapping it in an async block first. This will not affect any code that currently compiles though.

(Async::Yes { closure_id, .. }, Some(body)) => (closure_id, body),
_ => return self.lower_fn_body_block(span, decl, body),
};

self.lower_body(|this| {
Expand Down Expand Up @@ -1199,16 +1199,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
parameters.push(new_parameter);
}

let body_span = body.map_or(span, |b| b.span);
let async_expr = this.make_async_expr(
CaptureBy::Value,
closure_id,
None,
body_span,
body.span,
hir::AsyncGeneratorKind::Fn,
|this| {
// Create a block from the user's function body:
let user_body = this.lower_block_expr_opt(body_span, body);
let user_body = this.lower_block_expr(body);

// Transform into `drop-temps { <user-body> }`, an expression:
let desugared_span =
Expand Down Expand Up @@ -1240,7 +1239,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

(
this.arena.alloc_from_iter(parameters),
this.expr(body_span, async_expr, AttrVec::new()),
this.expr(body.span, async_expr, AttrVec::new()),
)
})
}
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_resolve/src/def_collector.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{ImplTraitContext, Resolver};
use rustc_ast::visit::{self, FnKind};
use rustc_ast::walk_list;
use rustc_ast::*;
use rustc_expand::expand::AstFragment;
use rustc_hir::def_id::LocalDefId;
Expand Down Expand Up @@ -148,8 +147,13 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> {
self.with_parent(return_impl_trait_id, |this| {
this.visit_fn_ret_ty(&sig.decl.output)
});
let closure_def = self.create_def(closure_id, DefPathData::ClosureExpr, span);
self.with_parent(closure_def, |this| walk_list!(this, visit_block, body));
// If this async fn has no body (i.e. it's an async fn signature in a trait)
// then the closure_def will never be used, and we should avoid generating a
// def-id for it.
if let Some(body) = body {
let closure_def = self.create_def(closure_id, DefPathData::ClosureExpr, span);
self.with_parent(closure_def, |this| this.visit_block(body));
}
return;
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/async-await/in-trait/issue-102219.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// compile-flags:--crate-type=lib
// edition:2021
// check-pass

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

trait T {
async fn foo();
}