Rollup merge of #102244 - compiler-errors:issue-102219, r=cjgillot
Only generate closure def id for async fns with body Fixes #102219
This commit is contained in:
commit
c807277382
3 changed files with 23 additions and 10 deletions
|
@ -1055,9 +1055,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||||
asyncness: Async,
|
asyncness: Async,
|
||||||
body: Option<&Block>,
|
body: Option<&Block>,
|
||||||
) -> hir::BodyId {
|
) -> hir::BodyId {
|
||||||
let closure_id = match asyncness {
|
let (closure_id, body) = match (asyncness, body) {
|
||||||
Async::Yes { closure_id, .. } => closure_id,
|
(Async::Yes { closure_id, .. }, Some(body)) => (closure_id, body),
|
||||||
Async::No => return self.lower_fn_body_block(span, decl, body),
|
_ => return self.lower_fn_body_block(span, decl, body),
|
||||||
};
|
};
|
||||||
|
|
||||||
self.lower_body(|this| {
|
self.lower_body(|this| {
|
||||||
|
@ -1199,16 +1199,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||||
parameters.push(new_parameter);
|
parameters.push(new_parameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
let body_span = body.map_or(span, |b| b.span);
|
|
||||||
let async_expr = this.make_async_expr(
|
let async_expr = this.make_async_expr(
|
||||||
CaptureBy::Value,
|
CaptureBy::Value,
|
||||||
closure_id,
|
closure_id,
|
||||||
None,
|
None,
|
||||||
body_span,
|
body.span,
|
||||||
hir::AsyncGeneratorKind::Fn,
|
hir::AsyncGeneratorKind::Fn,
|
||||||
|this| {
|
|this| {
|
||||||
// Create a block from the user's function body:
|
// 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:
|
// Transform into `drop-temps { <user-body> }`, an expression:
|
||||||
let desugared_span =
|
let desugared_span =
|
||||||
|
@ -1240,7 +1239,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||||
|
|
||||||
(
|
(
|
||||||
this.arena.alloc_from_iter(parameters),
|
this.arena.alloc_from_iter(parameters),
|
||||||
this.expr(body_span, async_expr, AttrVec::new()),
|
this.expr(body.span, async_expr, AttrVec::new()),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use crate::{ImplTraitContext, Resolver};
|
use crate::{ImplTraitContext, Resolver};
|
||||||
use rustc_ast::visit::{self, FnKind};
|
use rustc_ast::visit::{self, FnKind};
|
||||||
use rustc_ast::walk_list;
|
|
||||||
use rustc_ast::*;
|
use rustc_ast::*;
|
||||||
use rustc_expand::expand::AstFragment;
|
use rustc_expand::expand::AstFragment;
|
||||||
use rustc_hir::def_id::LocalDefId;
|
use rustc_hir::def_id::LocalDefId;
|
||||||
|
@ -148,8 +147,13 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> {
|
||||||
self.with_parent(return_impl_trait_id, |this| {
|
self.with_parent(return_impl_trait_id, |this| {
|
||||||
this.visit_fn_ret_ty(&sig.decl.output)
|
this.visit_fn_ret_ty(&sig.decl.output)
|
||||||
});
|
});
|
||||||
let closure_def = self.create_def(closure_id, DefPathData::ClosureExpr, span);
|
// If this async fn has no body (i.e. it's an async fn signature in a trait)
|
||||||
self.with_parent(closure_def, |this| walk_list!(this, visit_block, body));
|
// 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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
10
src/test/ui/async-await/in-trait/issue-102219.rs
Normal file
10
src/test/ui/async-await/in-trait/issue-102219.rs
Normal file
|
@ -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();
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue