diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2023-03-27 18:56:19 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-27 18:56:19 +0200 |
| commit | b1e8be783f13dfb2f6e7ade995bde66778a5a43d (patch) | |
| tree | 013470d0a60dd58afa0676c946a2f9fc59913ef8 /compiler | |
| parent | 52c8084f912e3728b8b06ae7e077521dba89d326 (diff) | |
| parent | c8ead2e693a22fe94c6b3edeb3f49c7e6aec3912 (diff) | |
| download | rust-b1e8be783f13dfb2f6e7ade995bde66778a5a43d.tar.gz rust-b1e8be783f13dfb2f6e7ade995bde66778a5a43d.zip | |
Rollup merge of #109354 - Swatinem:rm-closureid, r=compiler-errors
Remove the `NodeId` of `ast::ExprKind::Async` This is a followup to https://github.com/rust-lang/rust/pull/104833#pullrequestreview-1314537416. In my original attempt, I was using `LoweringContext::expr`, which was not correct as it creates a fresh `DefId`. It now uses the correct `DefId` for the wrapping `Expr`, and also makes forwarding `#[track_caller]` attributes more explicit.
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_ast/src/ast.rs | 10 | ||||
| -rw-r--r-- | compiler/rustc_ast/src/mut_visit.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_ast/src/visit.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_ast_lowering/src/expr.rs | 77 | ||||
| -rw-r--r-- | compiler/rustc_ast_lowering/src/item.rs | 7 | ||||
| -rw-r--r-- | compiler/rustc_ast_pretty/src/pprust/state/expr.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/assert/context.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_lint/src/early.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/expr.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/def_collector.rs | 4 |
10 files changed, 52 insertions, 60 deletions
diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 5d164bc4b3c..ab8b7f632e8 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -1426,13 +1426,9 @@ pub enum ExprKind { Block(P<Block>, Option<Label>), /// An async block (`async move { ... }`). /// - /// The `NodeId` is the `NodeId` for the closure that results from - /// desugaring an async block, just like the NodeId field in the - /// `Async::Yes` variant. This is necessary in order to create a def for the - /// closure which can be used as a parent of any child defs. Defs - /// created during lowering cannot be made the parent of any other - /// preexisting defs. - Async(CaptureBy, NodeId, P<Block>), + /// The async block used to have a `NodeId`, which was removed in favor of + /// using the parent `NodeId` of the parent `Expr`. + Async(CaptureBy, P<Block>), /// An await expression (`my_future.await`). Await(P<Expr>), diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 45a5a3ecb53..46e46ab575e 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -1407,8 +1407,7 @@ pub fn noop_visit_expr<T: MutVisitor>( vis.visit_block(blk); visit_opt(label, |label| vis.visit_label(label)); } - ExprKind::Async(_capture_by, node_id, body) => { - vis.visit_id(node_id); + ExprKind::Async(_capture_by, body) => { vis.visit_block(body); } ExprKind::Await(expr) => vis.visit_expr(expr), diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index 9a4da6d4396..608f87ab6eb 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -860,7 +860,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) { walk_list!(visitor, visit_label, opt_label); visitor.visit_block(block); } - ExprKind::Async(_, _, body) => { + ExprKind::Async(_, body) => { visitor.visit_block(body); } ExprKind::Await(expr) => visitor.visit_expr(expr), diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 181f94ab74f..3247802345b 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -63,20 +63,6 @@ impl<'hir> LoweringContext<'_, 'hir> { ExprKind::ForLoop(pat, head, body, opt_label) => { return self.lower_expr_for(e, pat, head, body, *opt_label); } - // Similarly, async blocks do not use `e.id` but rather `closure_node_id`. - ExprKind::Async(capture_clause, closure_node_id, block) => { - let hir_id = self.lower_node_id(*closure_node_id); - self.lower_attrs(hir_id, &e.attrs); - return self.make_async_expr( - *capture_clause, - hir_id, - *closure_node_id, - None, - e.span, - hir::AsyncGeneratorKind::Block, - |this| this.with_new_scopes(|this| this.lower_block_expr(block)), - ); - } _ => (), } @@ -187,6 +173,14 @@ impl<'hir> LoweringContext<'_, 'hir> { self.arena.alloc_from_iter(arms.iter().map(|x| self.lower_arm(x))), hir::MatchSource::Normal, ), + ExprKind::Async(capture_clause, block) => self.make_async_expr( + *capture_clause, + e.id, + None, + e.span, + hir::AsyncGeneratorKind::Block, + |this| this.with_new_scopes(|this| this.lower_block_expr(block)), + ), ExprKind::Await(expr) => { let dot_await_span = if expr.span.hi() < e.span.hi() { let span_with_whitespace = self @@ -320,7 +314,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ), ExprKind::Try(sub_expr) => self.lower_expr_try(e.span, sub_expr), - ExprKind::Paren(_) | ExprKind::ForLoop(..) | ExprKind::Async(..) => { + ExprKind::Paren(_) | ExprKind::ForLoop(..) => { unreachable!("already handled") } @@ -591,13 +585,12 @@ impl<'hir> LoweringContext<'_, 'hir> { pub(super) fn make_async_expr( &mut self, capture_clause: CaptureBy, - outer_hir_id: hir::HirId, closure_node_id: NodeId, ret_ty: Option<hir::FnRetTy<'hir>>, span: Span, async_gen_kind: hir::AsyncGeneratorKind, body: impl FnOnce(&mut Self) -> hir::Expr<'hir>, - ) -> hir::Expr<'hir> { + ) -> hir::ExprKind<'hir> { let output = ret_ty.unwrap_or_else(|| hir::FnRetTy::DefaultReturn(self.lower_span(span))); // Resume argument type: `ResumeTy` @@ -644,24 +637,28 @@ impl<'hir> LoweringContext<'_, 'hir> { }); // `static |_task_context| -> <ret_ty> { body }`: - let generator_kind = { - let c = self.arena.alloc(hir::Closure { - def_id: self.local_def_id(closure_node_id), - binder: hir::ClosureBinder::Default, - capture_clause, - bound_generic_params: &[], - fn_decl, - body, - fn_decl_span: self.lower_span(span), - fn_arg_span: None, - movability: Some(hir::Movability::Static), - constness: hir::Constness::NotConst, - }); - - hir::ExprKind::Closure(c) - }; + hir::ExprKind::Closure(self.arena.alloc(hir::Closure { + def_id: self.local_def_id(closure_node_id), + binder: hir::ClosureBinder::Default, + capture_clause, + bound_generic_params: &[], + fn_decl, + body, + fn_decl_span: self.lower_span(span), + fn_arg_span: None, + movability: Some(hir::Movability::Static), + constness: hir::Constness::NotConst, + })) + } - let hir_id = self.lower_node_id(closure_node_id); + /// Forwards a possible `#[track_caller]` annotation from `outer_hir_id` to + /// `inner_hir_id` in case the `closure_track_caller` feature is enabled. + pub(super) fn maybe_forward_track_caller( + &mut self, + span: Span, + outer_hir_id: hir::HirId, + inner_hir_id: hir::HirId, + ) { if self.tcx.features().closure_track_caller && let Some(attrs) = self.attrs.get(&outer_hir_id.local_id) && attrs.into_iter().any(|attr| attr.has_name(sym::track_caller)) @@ -669,7 +666,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let unstable_span = self.mark_span_with_reason(DesugaringKind::Async, span, self.allow_gen_future.clone()); self.lower_attrs( - hir_id, + inner_hir_id, &[Attribute { kind: AttrKind::Normal(ptr::P(NormalAttr { item: AttrItem { @@ -685,8 +682,6 @@ impl<'hir> LoweringContext<'_, 'hir> { }], ); } - - hir::Expr { hir_id, kind: generator_kind, span: self.lower_span(span) } } /// Desugar `<expr>.await` into: @@ -1001,15 +996,17 @@ impl<'hir> LoweringContext<'_, 'hir> { None }; - this.make_async_expr( + let async_body = this.make_async_expr( capture_clause, - closure_hir_id, inner_closure_id, async_ret_ty, body.span, hir::AsyncGeneratorKind::Closure, |this| this.with_new_scopes(|this| this.lower_expr_mut(body)), - ) + ); + let hir_id = this.lower_node_id(inner_closure_id); + this.maybe_forward_track_caller(body.span, closure_hir_id, hir_id); + hir::Expr { hir_id, kind: async_body, span: this.lower_span(body.span) } }); body_id }); diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 9a117ac9a3c..cc879982abc 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -1146,7 +1146,6 @@ impl<'hir> LoweringContext<'_, 'hir> { let async_expr = this.make_async_expr( CaptureBy::Value, - fn_id, closure_id, None, body.span, @@ -1180,7 +1179,11 @@ impl<'hir> LoweringContext<'_, 'hir> { }, ); - (this.arena.alloc_from_iter(parameters), async_expr) + let hir_id = this.lower_node_id(closure_id); + this.maybe_forward_track_caller(body.span, fn_id, hir_id); + let expr = hir::Expr { hir_id, kind: async_expr, span: this.lower_span(body.span) }; + + (this.arena.alloc_from_iter(parameters), expr) }) } diff --git a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs index e2f63641ffa..776bf54244e 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs @@ -439,7 +439,7 @@ impl<'a> State<'a> { self.ibox(0); self.print_block_with_attrs(blk, attrs); } - ast::ExprKind::Async(capture_clause, _, blk) => { + ast::ExprKind::Async(capture_clause, blk) => { self.word_nbsp("async"); self.print_capture_clause(*capture_clause); // cbox/ibox in analogy to the `ExprKind::Block` arm above diff --git a/compiler/rustc_builtin_macros/src/assert/context.rs b/compiler/rustc_builtin_macros/src/assert/context.rs index 5d8f4db76f9..c9e3cd486f8 100644 --- a/compiler/rustc_builtin_macros/src/assert/context.rs +++ b/compiler/rustc_builtin_macros/src/assert/context.rs @@ -287,7 +287,7 @@ impl<'cx, 'a> Context<'cx, 'a> { // sync with the `rfc-2011-nicer-assert-messages/all-expr-kinds.rs` test. ExprKind::Assign(_, _, _) | ExprKind::AssignOp(_, _, _) - | ExprKind::Async(_, _, _) + | ExprKind::Async(_, _) | ExprKind::Await(_) | ExprKind::Block(_, _) | ExprKind::Break(_, _) diff --git a/compiler/rustc_lint/src/early.rs b/compiler/rustc_lint/src/early.rs index 9b0d8d6c072..65607d71805 100644 --- a/compiler/rustc_lint/src/early.rs +++ b/compiler/rustc_lint/src/early.rs @@ -224,8 +224,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T> ast::ExprKind::Closure(box ast::Closure { asyncness: ast::Async::Yes { closure_id, .. }, .. - }) - | ast::ExprKind::Async(_, closure_id, ..) => self.check_id(closure_id), + }) => self.check_id(closure_id), _ => {} } } diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 8b69b3cb036..c4605e63cf3 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -2911,7 +2911,7 @@ impl<'a> Parser<'a> { self.expect_keyword(kw::Async)?; let capture_clause = self.parse_capture_clause()?; let (attrs, body) = self.parse_inner_attrs_and_block()?; - let kind = ExprKind::Async(capture_clause, DUMMY_NODE_ID, body); + let kind = ExprKind::Async(capture_clause, body); Ok(self.mk_expr_with_attrs(lo.to(self.prev_token.span), kind, attrs)) } diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index e7ff236f846..356d7f365fe 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -260,9 +260,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { Async::No => closure_def, } } - ExprKind::Async(_, async_id, _) => { - self.create_def(async_id, DefPathData::ClosureExpr, expr.span) - } + ExprKind::Async(_, _) => self.create_def(expr.id, DefPathData::ClosureExpr, expr.span), _ => self.parent_def, }; |
