1
Fork 0

Introduce closure_id method on CoroutineKind

This commit is contained in:
Michael Goulet 2023-12-08 21:38:00 +00:00
parent f967532a47
commit 8361a7288e
4 changed files with 24 additions and 24 deletions

View file

@ -2450,6 +2450,14 @@ impl CoroutineKind {
matches!(self, CoroutineKind::Gen { .. }) matches!(self, CoroutineKind::Gen { .. })
} }
pub fn closure_id(self) -> NodeId {
match self {
CoroutineKind::Async { closure_id, .. }
| CoroutineKind::Gen { closure_id, .. }
| CoroutineKind::AsyncGen { closure_id, .. } => closure_id,
}
}
/// In this case this is an `async` or `gen` return, the `NodeId` for the generated `impl Trait` /// In this case this is an `async` or `gen` return, the `NodeId` for the generated `impl Trait`
/// item. /// item.
pub fn return_id(self) -> (NodeId, Span) { pub fn return_id(self) -> (NodeId, Span) {

View file

@ -1035,10 +1035,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let (Some(coroutine_kind), Some(body)) = (coroutine_kind, body) else { let (Some(coroutine_kind), Some(body)) = (coroutine_kind, body) else {
return self.lower_fn_body_block(span, decl, body); return self.lower_fn_body_block(span, decl, body);
}; };
// FIXME(gen_blocks): Introduce `closure_id` method and remove ALL destructuring. let closure_id = coroutine_kind.closure_id();
let (CoroutineKind::Async { closure_id, .. }
| CoroutineKind::Gen { closure_id, .. }
| CoroutineKind::AsyncGen { closure_id, .. }) = coroutine_kind;
self.lower_body(|this| { self.lower_body(|this| {
let mut parameters: Vec<hir::Param<'_>> = Vec::new(); let mut parameters: Vec<hir::Param<'_>> = Vec::new();

View file

@ -163,10 +163,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
// it does not have a corresponding AST node // it does not have a corresponding AST node
if let ast_visit::FnKind::Fn(_, _, sig, _, _, _) = fk { if let ast_visit::FnKind::Fn(_, _, sig, _, _, _) = fk {
if let Some(coro_kind) = sig.header.coroutine_kind { if let Some(coro_kind) = sig.header.coroutine_kind {
let (ast::CoroutineKind::Async { closure_id, .. } self.check_id(coro_kind.closure_id());
| ast::CoroutineKind::Gen { closure_id, .. }
| ast::CoroutineKind::AsyncGen { closure_id, .. }) = coro_kind;
self.check_id(closure_id);
} }
} }
} }
@ -228,10 +225,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
ast::ExprKind::Closure(box ast::Closure { ast::ExprKind::Closure(box ast::Closure {
coroutine_kind: Some(coro_kind), .. coroutine_kind: Some(coro_kind), ..
}) => { }) => {
let (ast::CoroutineKind::Async { closure_id, .. } self.check_id(coro_kind.closure_id());
| ast::CoroutineKind::Gen { closure_id, .. }
| ast::CoroutineKind::AsyncGen { closure_id, .. }) = coro_kind;
self.check_id(closure_id);
} }
_ => {} _ => {}
} }

View file

@ -157,11 +157,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) { fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) {
if let FnKind::Fn(_, _, sig, _, generics, body) = fn_kind { if let FnKind::Fn(_, _, sig, _, generics, body) = fn_kind {
match sig.header.coroutine_kind { match sig.header.coroutine_kind {
Some( Some(coroutine_kind) => {
CoroutineKind::Async { closure_id, .. }
| CoroutineKind::Gen { closure_id, .. }
| CoroutineKind::AsyncGen { closure_id, .. },
) => {
self.visit_generics(generics); self.visit_generics(generics);
// For async functions, we need to create their inner defs inside of a // For async functions, we need to create their inner defs inside of a
@ -176,8 +172,12 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
// then the closure_def will never be used, and we should avoid generating a // then the closure_def will never be used, and we should avoid generating a
// def-id for it. // def-id for it.
if let Some(body) = body { if let Some(body) = body {
let closure_def = let closure_def = self.create_def(
self.create_def(closure_id, kw::Empty, DefKind::Closure, span); coroutine_kind.closure_id(),
kw::Empty,
DefKind::Closure,
span,
);
self.with_parent(closure_def, |this| this.visit_block(body)); self.with_parent(closure_def, |this| this.visit_block(body));
} }
return; return;
@ -289,11 +289,12 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
// we must create two defs. // we must create two defs.
let closure_def = self.create_def(expr.id, kw::Empty, DefKind::Closure, expr.span); let closure_def = self.create_def(expr.id, kw::Empty, DefKind::Closure, expr.span);
match closure.coroutine_kind { match closure.coroutine_kind {
Some( Some(coroutine_kind) => self.create_def(
CoroutineKind::Async { closure_id, .. } coroutine_kind.closure_id(),
| CoroutineKind::Gen { closure_id, .. } kw::Empty,
| CoroutineKind::AsyncGen { closure_id, .. }, DefKind::Closure,
) => self.create_def(closure_id, kw::Empty, DefKind::Closure, expr.span), expr.span,
),
None => closure_def, None => closure_def,
} }
} }