Skip to content

Commit c8ead2e

Browse files
committed
Remove the NodeId of ast::ExprKind::Async
1 parent ab9bb3e commit c8ead2e

File tree

14 files changed

+56
-64
lines changed

14 files changed

+56
-64
lines changed

compiler/rustc_ast/src/ast.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1426,13 +1426,9 @@ pub enum ExprKind {
14261426
Block(P<Block>, Option<Label>),
14271427
/// An async block (`async move { ... }`).
14281428
///
1429-
/// The `NodeId` is the `NodeId` for the closure that results from
1430-
/// desugaring an async block, just like the NodeId field in the
1431-
/// `Async::Yes` variant. This is necessary in order to create a def for the
1432-
/// closure which can be used as a parent of any child defs. Defs
1433-
/// created during lowering cannot be made the parent of any other
1434-
/// preexisting defs.
1435-
Async(CaptureBy, NodeId, P<Block>),
1429+
/// The async block used to have a `NodeId`, which was removed in favor of
1430+
/// using the parent `NodeId` of the parent `Expr`.
1431+
Async(CaptureBy, P<Block>),
14361432
/// An await expression (`my_future.await`).
14371433
Await(P<Expr>),
14381434

compiler/rustc_ast/src/mut_visit.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1407,8 +1407,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
14071407
vis.visit_block(blk);
14081408
visit_opt(label, |label| vis.visit_label(label));
14091409
}
1410-
ExprKind::Async(_capture_by, node_id, body) => {
1411-
vis.visit_id(node_id);
1410+
ExprKind::Async(_capture_by, body) => {
14121411
vis.visit_block(body);
14131412
}
14141413
ExprKind::Await(expr) => vis.visit_expr(expr),

compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
860860
walk_list!(visitor, visit_label, opt_label);
861861
visitor.visit_block(block);
862862
}
863-
ExprKind::Async(_, _, body) => {
863+
ExprKind::Async(_, body) => {
864864
visitor.visit_block(body);
865865
}
866866
ExprKind::Await(expr) => visitor.visit_expr(expr),

compiler/rustc_ast_lowering/src/expr.rs

+37-40
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
6363
ExprKind::ForLoop(pat, head, body, opt_label) => {
6464
return self.lower_expr_for(e, pat, head, body, *opt_label);
6565
}
66-
// Similarly, async blocks do not use `e.id` but rather `closure_node_id`.
67-
ExprKind::Async(capture_clause, closure_node_id, block) => {
68-
let hir_id = self.lower_node_id(*closure_node_id);
69-
self.lower_attrs(hir_id, &e.attrs);
70-
return self.make_async_expr(
71-
*capture_clause,
72-
hir_id,
73-
*closure_node_id,
74-
None,
75-
e.span,
76-
hir::AsyncGeneratorKind::Block,
77-
|this| this.with_new_scopes(|this| this.lower_block_expr(block)),
78-
);
79-
}
8066
_ => (),
8167
}
8268

@@ -187,6 +173,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
187173
self.arena.alloc_from_iter(arms.iter().map(|x| self.lower_arm(x))),
188174
hir::MatchSource::Normal,
189175
),
176+
ExprKind::Async(capture_clause, block) => self.make_async_expr(
177+
*capture_clause,
178+
e.id,
179+
None,
180+
e.span,
181+
hir::AsyncGeneratorKind::Block,
182+
|this| this.with_new_scopes(|this| this.lower_block_expr(block)),
183+
),
190184
ExprKind::Await(expr) => {
191185
let dot_await_span = if expr.span.hi() < e.span.hi() {
192186
let span_with_whitespace = self
@@ -320,7 +314,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
320314
),
321315
ExprKind::Try(sub_expr) => self.lower_expr_try(e.span, sub_expr),
322316

323-
ExprKind::Paren(_) | ExprKind::ForLoop(..) | ExprKind::Async(..) => {
317+
ExprKind::Paren(_) | ExprKind::ForLoop(..) => {
324318
unreachable!("already handled")
325319
}
326320

@@ -591,13 +585,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
591585
pub(super) fn make_async_expr(
592586
&mut self,
593587
capture_clause: CaptureBy,
594-
outer_hir_id: hir::HirId,
595588
closure_node_id: NodeId,
596589
ret_ty: Option<hir::FnRetTy<'hir>>,
597590
span: Span,
598591
async_gen_kind: hir::AsyncGeneratorKind,
599592
body: impl FnOnce(&mut Self) -> hir::Expr<'hir>,
600-
) -> hir::Expr<'hir> {
593+
) -> hir::ExprKind<'hir> {
601594
let output = ret_ty.unwrap_or_else(|| hir::FnRetTy::DefaultReturn(self.lower_span(span)));
602595

603596
// Resume argument type: `ResumeTy`
@@ -644,32 +637,36 @@ impl<'hir> LoweringContext<'_, 'hir> {
644637
});
645638

646639
// `static |_task_context| -> <ret_ty> { body }`:
647-
let generator_kind = {
648-
let c = self.arena.alloc(hir::Closure {
649-
def_id: self.local_def_id(closure_node_id),
650-
binder: hir::ClosureBinder::Default,
651-
capture_clause,
652-
bound_generic_params: &[],
653-
fn_decl,
654-
body,
655-
fn_decl_span: self.lower_span(span),
656-
fn_arg_span: None,
657-
movability: Some(hir::Movability::Static),
658-
constness: hir::Constness::NotConst,
659-
});
660-
661-
hir::ExprKind::Closure(c)
662-
};
640+
hir::ExprKind::Closure(self.arena.alloc(hir::Closure {
641+
def_id: self.local_def_id(closure_node_id),
642+
binder: hir::ClosureBinder::Default,
643+
capture_clause,
644+
bound_generic_params: &[],
645+
fn_decl,
646+
body,
647+
fn_decl_span: self.lower_span(span),
648+
fn_arg_span: None,
649+
movability: Some(hir::Movability::Static),
650+
constness: hir::Constness::NotConst,
651+
}))
652+
}
663653

664-
let hir_id = self.lower_node_id(closure_node_id);
654+
/// Forwards a possible `#[track_caller]` annotation from `outer_hir_id` to
655+
/// `inner_hir_id` in case the `closure_track_caller` feature is enabled.
656+
pub(super) fn maybe_forward_track_caller(
657+
&mut self,
658+
span: Span,
659+
outer_hir_id: hir::HirId,
660+
inner_hir_id: hir::HirId,
661+
) {
665662
if self.tcx.features().closure_track_caller
666663
&& let Some(attrs) = self.attrs.get(&outer_hir_id.local_id)
667664
&& attrs.into_iter().any(|attr| attr.has_name(sym::track_caller))
668665
{
669666
let unstable_span =
670667
self.mark_span_with_reason(DesugaringKind::Async, span, self.allow_gen_future.clone());
671668
self.lower_attrs(
672-
hir_id,
669+
inner_hir_id,
673670
&[Attribute {
674671
kind: AttrKind::Normal(ptr::P(NormalAttr {
675672
item: AttrItem {
@@ -685,8 +682,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
685682
}],
686683
);
687684
}
688-
689-
hir::Expr { hir_id, kind: generator_kind, span: self.lower_span(span) }
690685
}
691686

692687
/// Desugar `<expr>.await` into:
@@ -1001,15 +996,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
1001996
None
1002997
};
1003998

1004-
this.make_async_expr(
999+
let async_body = this.make_async_expr(
10051000
capture_clause,
1006-
closure_hir_id,
10071001
inner_closure_id,
10081002
async_ret_ty,
10091003
body.span,
10101004
hir::AsyncGeneratorKind::Closure,
10111005
|this| this.with_new_scopes(|this| this.lower_expr_mut(body)),
1012-
)
1006+
);
1007+
let hir_id = this.lower_node_id(inner_closure_id);
1008+
this.maybe_forward_track_caller(body.span, closure_hir_id, hir_id);
1009+
hir::Expr { hir_id, kind: async_body, span: this.lower_span(body.span) }
10131010
});
10141011
body_id
10151012
});

compiler/rustc_ast_lowering/src/item.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
11461146

11471147
let async_expr = this.make_async_expr(
11481148
CaptureBy::Value,
1149-
fn_id,
11501149
closure_id,
11511150
None,
11521151
body.span,
@@ -1180,7 +1179,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
11801179
},
11811180
);
11821181

1183-
(this.arena.alloc_from_iter(parameters), async_expr)
1182+
let hir_id = this.lower_node_id(closure_id);
1183+
this.maybe_forward_track_caller(body.span, fn_id, hir_id);
1184+
let expr = hir::Expr { hir_id, kind: async_expr, span: this.lower_span(body.span) };
1185+
1186+
(this.arena.alloc_from_iter(parameters), expr)
11841187
})
11851188
}
11861189

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ impl<'a> State<'a> {
439439
self.ibox(0);
440440
self.print_block_with_attrs(blk, attrs);
441441
}
442-
ast::ExprKind::Async(capture_clause, _, blk) => {
442+
ast::ExprKind::Async(capture_clause, blk) => {
443443
self.word_nbsp("async");
444444
self.print_capture_clause(*capture_clause);
445445
// cbox/ibox in analogy to the `ExprKind::Block` arm above

compiler/rustc_builtin_macros/src/assert/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
287287
// sync with the `rfc-2011-nicer-assert-messages/all-expr-kinds.rs` test.
288288
ExprKind::Assign(_, _, _)
289289
| ExprKind::AssignOp(_, _, _)
290-
| ExprKind::Async(_, _, _)
290+
| ExprKind::Async(_, _)
291291
| ExprKind::Await(_)
292292
| ExprKind::Block(_, _)
293293
| ExprKind::Break(_, _)

compiler/rustc_lint/src/early.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
224224
ast::ExprKind::Closure(box ast::Closure {
225225
asyncness: ast::Async::Yes { closure_id, .. },
226226
..
227-
})
228-
| ast::ExprKind::Async(_, closure_id, ..) => self.check_id(closure_id),
227+
}) => self.check_id(closure_id),
229228
_ => {}
230229
}
231230
}

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2917,7 +2917,7 @@ impl<'a> Parser<'a> {
29172917
self.expect_keyword(kw::Async)?;
29182918
let capture_clause = self.parse_capture_clause()?;
29192919
let (attrs, body) = self.parse_inner_attrs_and_block()?;
2920-
let kind = ExprKind::Async(capture_clause, DUMMY_NODE_ID, body);
2920+
let kind = ExprKind::Async(capture_clause, body);
29212921
Ok(self.mk_expr_with_attrs(lo.to(self.prev_token.span), kind, attrs))
29222922
}
29232923

compiler/rustc_resolve/src/def_collector.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
260260
Async::No => closure_def,
261261
}
262262
}
263-
ExprKind::Async(_, async_id, _) => {
264-
self.create_def(async_id, DefPathData::ClosureExpr, expr.span)
265-
}
263+
ExprKind::Async(_, _) => self.create_def(expr.id, DefPathData::ClosureExpr, expr.span),
266264
_ => self.parent_def,
267265
};
268266

src/tools/clippy/clippy_lints/src/redundant_async_block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl EarlyLintPass for RedundantAsyncBlock {
4242
if expr.span.from_expansion() {
4343
return;
4444
}
45-
if let ExprKind::Async(_, _, block) = &expr.kind && block.stmts.len() == 1 &&
45+
if let ExprKind::Async(_, block) = &expr.kind && block.stmts.len() == 1 &&
4646
let Some(Stmt { kind: StmtKind::Expr(last), .. }) = block.stmts.last() &&
4747
let ExprKind::Await(future) = &last.kind &&
4848
!future.span.from_expansion() &&

src/tools/clippy/clippy_lints/src/suspicious_operation_groupings.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ fn ident_difference_expr_with_base_location(
578578
| (Assign(_, _, _), Assign(_, _, _))
579579
| (TryBlock(_), TryBlock(_))
580580
| (Await(_), Await(_))
581-
| (Async(_, _, _), Async(_, _, _))
581+
| (Async(_, _), Async(_, _))
582582
| (Block(_, _), Block(_, _))
583583
| (Closure(_), Closure(_))
584584
| (Match(_, _), Match(_, _))

src/tools/clippy/clippy_utils/src/ast_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
209209
&& eq_fn_decl(lf, rf)
210210
&& eq_expr(le, re)
211211
},
212-
(Async(lc, _, lb), Async(rc, _, rb)) => lc == rc && eq_block(lb, rb),
212+
(Async(lc, lb), Async(rc, rb)) => lc == rc && eq_block(lb, rb),
213213
(Range(lf, lt, ll), Range(rf, rt, rl)) => ll == rl && eq_expr_opt(lf, rf) && eq_expr_opt(lt, rt),
214214
(AddrOf(lbk, lm, le), AddrOf(rbk, rm, re)) => lbk == rbk && lm == rm && eq_expr(le, re),
215215
(Path(lq, lp), Path(rq, rp)) => both(lq, rq, eq_qself) && eq_path(lp, rp),

src/tools/rustfmt/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ pub(crate) fn format_expr(
366366
))
367367
}
368368
}
369-
ast::ExprKind::Async(capture_by, _node_id, ref block) => {
369+
ast::ExprKind::Async(capture_by, ref block) => {
370370
let mover = if capture_by == ast::CaptureBy::Value {
371371
"move "
372372
} else {

0 commit comments

Comments
 (0)