Skip to content

Commit c807277

Browse files
authored
Rollup merge of #102244 - compiler-errors:issue-102219, r=cjgillot
Only generate closure def id for async fns with body Fixes #102219
2 parents 1a93028 + e99f6fe commit c807277

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

compiler/rustc_ast_lowering/src/item.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1055,9 +1055,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
10551055
asyncness: Async,
10561056
body: Option<&Block>,
10571057
) -> hir::BodyId {
1058-
let closure_id = match asyncness {
1059-
Async::Yes { closure_id, .. } => closure_id,
1060-
Async::No => return self.lower_fn_body_block(span, decl, body),
1058+
let (closure_id, body) = match (asyncness, body) {
1059+
(Async::Yes { closure_id, .. }, Some(body)) => (closure_id, body),
1060+
_ => return self.lower_fn_body_block(span, decl, body),
10611061
};
10621062

10631063
self.lower_body(|this| {
@@ -1199,16 +1199,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
11991199
parameters.push(new_parameter);
12001200
}
12011201

1202-
let body_span = body.map_or(span, |b| b.span);
12031202
let async_expr = this.make_async_expr(
12041203
CaptureBy::Value,
12051204
closure_id,
12061205
None,
1207-
body_span,
1206+
body.span,
12081207
hir::AsyncGeneratorKind::Fn,
12091208
|this| {
12101209
// Create a block from the user's function body:
1211-
let user_body = this.lower_block_expr_opt(body_span, body);
1210+
let user_body = this.lower_block_expr(body);
12121211

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

12411240
(
12421241
this.arena.alloc_from_iter(parameters),
1243-
this.expr(body_span, async_expr, AttrVec::new()),
1242+
this.expr(body.span, async_expr, AttrVec::new()),
12441243
)
12451244
})
12461245
}

compiler/rustc_resolve/src/def_collector.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::{ImplTraitContext, Resolver};
22
use rustc_ast::visit::{self, FnKind};
3-
use rustc_ast::walk_list;
43
use rustc_ast::*;
54
use rustc_expand::expand::AstFragment;
65
use rustc_hir::def_id::LocalDefId;
@@ -148,8 +147,13 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> {
148147
self.with_parent(return_impl_trait_id, |this| {
149148
this.visit_fn_ret_ty(&sig.decl.output)
150149
});
151-
let closure_def = self.create_def(closure_id, DefPathData::ClosureExpr, span);
152-
self.with_parent(closure_def, |this| walk_list!(this, visit_block, body));
150+
// If this async fn has no body (i.e. it's an async fn signature in a trait)
151+
// then the closure_def will never be used, and we should avoid generating a
152+
// def-id for it.
153+
if let Some(body) = body {
154+
let closure_def = self.create_def(closure_id, DefPathData::ClosureExpr, span);
155+
self.with_parent(closure_def, |this| this.visit_block(body));
156+
}
153157
return;
154158
}
155159
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// compile-flags:--crate-type=lib
2+
// edition:2021
3+
// check-pass
4+
5+
#![feature(async_fn_in_trait)]
6+
#![allow(incomplete_features)]
7+
8+
trait T {
9+
async fn foo();
10+
}

0 commit comments

Comments
 (0)