Skip to content

Commit 8361a72

Browse files
Introduce closure_id method on CoroutineKind
1 parent f967532 commit 8361a72

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2450,6 +2450,14 @@ impl CoroutineKind {
24502450
matches!(self, CoroutineKind::Gen { .. })
24512451
}
24522452

2453+
pub fn closure_id(self) -> NodeId {
2454+
match self {
2455+
CoroutineKind::Async { closure_id, .. }
2456+
| CoroutineKind::Gen { closure_id, .. }
2457+
| CoroutineKind::AsyncGen { closure_id, .. } => closure_id,
2458+
}
2459+
}
2460+
24532461
/// In this case this is an `async` or `gen` return, the `NodeId` for the generated `impl Trait`
24542462
/// item.
24552463
pub fn return_id(self) -> (NodeId, Span) {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,10 +1035,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10351035
let (Some(coroutine_kind), Some(body)) = (coroutine_kind, body) else {
10361036
return self.lower_fn_body_block(span, decl, body);
10371037
};
1038-
// FIXME(gen_blocks): Introduce `closure_id` method and remove ALL destructuring.
1039-
let (CoroutineKind::Async { closure_id, .. }
1040-
| CoroutineKind::Gen { closure_id, .. }
1041-
| CoroutineKind::AsyncGen { closure_id, .. }) = coroutine_kind;
1038+
let closure_id = coroutine_kind.closure_id();
10421039

10431040
self.lower_body(|this| {
10441041
let mut parameters: Vec<hir::Param<'_>> = Vec::new();

compiler/rustc_lint/src/early.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
163163
// it does not have a corresponding AST node
164164
if let ast_visit::FnKind::Fn(_, _, sig, _, _, _) = fk {
165165
if let Some(coro_kind) = sig.header.coroutine_kind {
166-
let (ast::CoroutineKind::Async { closure_id, .. }
167-
| ast::CoroutineKind::Gen { closure_id, .. }
168-
| ast::CoroutineKind::AsyncGen { closure_id, .. }) = coro_kind;
169-
self.check_id(closure_id);
166+
self.check_id(coro_kind.closure_id());
170167
}
171168
}
172169
}
@@ -228,10 +225,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
228225
ast::ExprKind::Closure(box ast::Closure {
229226
coroutine_kind: Some(coro_kind), ..
230227
}) => {
231-
let (ast::CoroutineKind::Async { closure_id, .. }
232-
| ast::CoroutineKind::Gen { closure_id, .. }
233-
| ast::CoroutineKind::AsyncGen { closure_id, .. }) = coro_kind;
234-
self.check_id(closure_id);
228+
self.check_id(coro_kind.closure_id());
235229
}
236230
_ => {}
237231
}

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
157157
fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) {
158158
if let FnKind::Fn(_, _, sig, _, generics, body) = fn_kind {
159159
match sig.header.coroutine_kind {
160-
Some(
161-
CoroutineKind::Async { closure_id, .. }
162-
| CoroutineKind::Gen { closure_id, .. }
163-
| CoroutineKind::AsyncGen { closure_id, .. },
164-
) => {
160+
Some(coroutine_kind) => {
165161
self.visit_generics(generics);
166162

167163
// 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> {
176172
// then the closure_def will never be used, and we should avoid generating a
177173
// def-id for it.
178174
if let Some(body) = body {
179-
let closure_def =
180-
self.create_def(closure_id, kw::Empty, DefKind::Closure, span);
175+
let closure_def = self.create_def(
176+
coroutine_kind.closure_id(),
177+
kw::Empty,
178+
DefKind::Closure,
179+
span,
180+
);
181181
self.with_parent(closure_def, |this| this.visit_block(body));
182182
}
183183
return;
@@ -289,11 +289,12 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
289289
// we must create two defs.
290290
let closure_def = self.create_def(expr.id, kw::Empty, DefKind::Closure, expr.span);
291291
match closure.coroutine_kind {
292-
Some(
293-
CoroutineKind::Async { closure_id, .. }
294-
| CoroutineKind::Gen { closure_id, .. }
295-
| CoroutineKind::AsyncGen { closure_id, .. },
296-
) => self.create_def(closure_id, kw::Empty, DefKind::Closure, expr.span),
292+
Some(coroutine_kind) => self.create_def(
293+
coroutine_kind.closure_id(),
294+
kw::Empty,
295+
DefKind::Closure,
296+
expr.span,
297+
),
297298
None => closure_def,
298299
}
299300
}

0 commit comments

Comments
 (0)