diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 995dc6aab55..983efa48a45 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -608,14 +608,18 @@ impl<'hir> LoweringContext<'_, 'hir> { }); // `static |_task_context| -> { body }`: - let generator_kind = hir::ExprKind::Closure { - binder: &hir::ClosureBinder::Default, - capture_clause, - bound_generic_params: &[], - fn_decl, - body, - fn_decl_span: self.lower_span(span), - movability: Some(hir::Movability::Static), + let generator_kind = { + let c = self.arena.alloc(hir::Closure { + binder: hir::ClosureBinder::Default, + capture_clause, + bound_generic_params: &[], + fn_decl, + body, + fn_decl_span: self.lower_span(span), + movability: Some(hir::Movability::Static), + }); + + hir::ExprKind::Closure(c) }; let generator = hir::Expr { hir_id: self.lower_node_id(closure_node_id), @@ -864,7 +868,7 @@ impl<'hir> LoweringContext<'_, 'hir> { // Lower outside new scope to preserve `is_in_loop_condition`. let fn_decl = this.lower_fn_decl(decl, None, FnDeclKind::Closure, None); - hir::ExprKind::Closure { + let c = self.arena.alloc(hir::Closure { binder: binder_clause, capture_clause, bound_generic_params, @@ -872,7 +876,9 @@ impl<'hir> LoweringContext<'_, 'hir> { body: body_id, fn_decl_span: this.lower_span(fn_decl_span), movability: generator_option, - } + }); + + hir::ExprKind::Closure(c) }) } @@ -917,7 +923,7 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_closure_binder<'c>( &mut self, binder: &'c ClosureBinder, - ) -> (&'hir hir::ClosureBinder, &'c [GenericParam]) { + ) -> (hir::ClosureBinder, &'c [GenericParam]) { let (binder, params) = match binder { ClosureBinder::NotPresent => (hir::ClosureBinder::Default, &[][..]), &ClosureBinder::For { span, ref generic_params } => { @@ -926,7 +932,7 @@ impl<'hir> LoweringContext<'_, 'hir> { } }; - (self.arena.alloc(binder), params) + (binder, params) } fn lower_expr_async_closure( @@ -991,7 +997,7 @@ impl<'hir> LoweringContext<'_, 'hir> { // closure argument types. let fn_decl = this.lower_fn_decl(&outer_decl, None, FnDeclKind::Closure, None); - hir::ExprKind::Closure { + let c = self.arena.alloc(hir::Closure { binder: binder_clause, capture_clause, bound_generic_params, @@ -999,7 +1005,8 @@ impl<'hir> LoweringContext<'_, 'hir> { body, fn_decl_span: this.lower_span(fn_decl_span), movability: None, - } + }); + hir::ExprKind::Closure(c) }) } diff --git a/compiler/rustc_borrowck/src/diagnostics/mod.rs b/compiler/rustc_borrowck/src/diagnostics/mod.rs index 6fea6941085..daf4040cc4b 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mod.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mod.rs @@ -891,7 +891,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { let hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(local_did); let expr = &self.infcx.tcx.hir().expect_expr(hir_id).kind; debug!("closure_span: hir_id={:?} expr={:?}", hir_id, expr); - if let hir::ExprKind::Closure { body, fn_decl_span, .. } = expr { + if let hir::ExprKind::Closure(&hir::Closure { body, fn_decl_span, .. }) = expr { for (captured_place, place) in self .infcx .tcx @@ -904,11 +904,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { if target_place == place.as_ref() => { debug!("closure_span: found captured local {:?}", place); - let body = self.infcx.tcx.hir().body(*body); + let body = self.infcx.tcx.hir().body(body); let generator_kind = body.generator_kind(); return Some(( - *fn_decl_span, + fn_decl_span, generator_kind, captured_place.get_capture_kind_span(self.infcx.tcx), captured_place.get_path_span(self.infcx.tcx), diff --git a/compiler/rustc_borrowck/src/diagnostics/region_name.rs b/compiler/rustc_borrowck/src/diagnostics/region_name.rs index e60e11f11df..e41af17fbf9 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_name.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_name.rs @@ -325,7 +325,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { // Can't have BrEnv in functions, constants or generators. bug!("BrEnv outside of closure."); }; - let hir::ExprKind::Closure { fn_decl_span, .. } + let hir::ExprKind::Closure(&hir::Closure { fn_decl_span, .. }) = tcx.hir().expect_expr(self.mir_hir_id()).kind else { bug!("Closure is not defined by a closure expr"); @@ -701,16 +701,16 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { let (return_span, mir_description, hir_ty) = match hir.get(mir_hir_id) { hir::Node::Expr(hir::Expr { - kind: hir::ExprKind::Closure { fn_decl, body, fn_decl_span, .. }, + kind: hir::ExprKind::Closure(&hir::Closure { fn_decl, body, fn_decl_span, .. }), .. }) => { let (mut span, mut hir_ty) = match fn_decl.output { hir::FnRetTy::DefaultReturn(_) => { - (tcx.sess.source_map().end_point(*fn_decl_span), None) + (tcx.sess.source_map().end_point(fn_decl_span), None) } hir::FnRetTy::Return(hir_ty) => (fn_decl.output.span(), Some(hir_ty)), }; - let mir_description = match hir.body(*body).generator_kind { + let mir_description = match hir.body(body).generator_kind { Some(hir::GeneratorKind::Async(gen)) => match gen { hir::AsyncGeneratorKind::Block => " of async block", hir::AsyncGeneratorKind::Closure => " of async closure", @@ -841,9 +841,9 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { let yield_span = match tcx.hir().get(self.mir_hir_id()) { hir::Node::Expr(hir::Expr { - kind: hir::ExprKind::Closure { fn_decl_span, .. }, + kind: hir::ExprKind::Closure(&hir::Closure { fn_decl_span, .. }), .. - }) => (tcx.sess.source_map().end_point(*fn_decl_span)), + }) => (tcx.sess.source_map().end_point(fn_decl_span)), _ => self.body.span, }; diff --git a/compiler/rustc_hir/src/arena.rs b/compiler/rustc_hir/src/arena.rs index a6d10f3adae..44335b7f42e 100644 --- a/compiler/rustc_hir/src/arena.rs +++ b/compiler/rustc_hir/src/arena.rs @@ -12,6 +12,7 @@ macro_rules! arena_types { [] asm_operand: (rustc_hir::InlineAsmOperand<'tcx>, rustc_span::Span), [] asm_template: rustc_ast::InlineAsmTemplatePiece, [] attribute: rustc_ast::Attribute, + [] closure: rustc_hir::Closure<'tcx>, [] block: rustc_hir::Block<'tcx>, [] bare_fn_ty: rustc_hir::BareFnTy<'tcx>, [] body: rustc_hir::Body<'tcx>, diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 2252dfd683a..9d198fbdf74 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -922,6 +922,17 @@ pub struct Crate<'hir> { pub hir_hash: Fingerprint, } +#[derive(Debug, HashStable_Generic)] +pub struct Closure<'hir> { + pub binder: ClosureBinder, + pub capture_clause: CaptureBy, + pub bound_generic_params: &'hir [GenericParam<'hir>], + pub fn_decl: &'hir FnDecl<'hir>, + pub body: BodyId, + pub fn_decl_span: Span, + pub movability: Option, +} + /// A block of statements `{ .. }`, which may have a label (in this case the /// `targeted_by_break` field will be `true`) and may be `unsafe` by means of /// the `rules` being anything but `DefaultBlock`. @@ -1930,15 +1941,7 @@ pub enum ExprKind<'hir> { /// /// This may also be a generator literal or an `async block` as indicated by the /// `Option`. - Closure { - binder: &'hir ClosureBinder, - capture_clause: CaptureBy, - bound_generic_params: &'hir [GenericParam<'hir>], - fn_decl: &'hir FnDecl<'hir>, - body: BodyId, - fn_decl_span: Span, - movability: Option, - }, + Closure(&'hir Closure<'hir>), /// A block (e.g., `'label: { ... }`). Block(&'hir Block<'hir>, Option