diff options
| author | Camille GILLOT <gillot.camille@gmail.com> | 2022-06-11 21:25:25 +0200 |
|---|---|---|
| committer | Camille GILLOT <gillot.camille@gmail.com> | 2022-06-12 00:16:27 +0200 |
| commit | 3039cfeb6a24c65ab4b7d25f1c60dc0a5df836ac (patch) | |
| tree | 66c3a317d49d7fb64829d951b6d38f4b8ffff2ec /compiler | |
| parent | fa68e73e9947be8ffc5b3b46d899e4953a44e7e9 (diff) | |
| download | rust-3039cfeb6a24c65ab4b7d25f1c60dc0a5df836ac.tar.gz rust-3039cfeb6a24c65ab4b7d25f1c60dc0a5df836ac.zip | |
Make `ExprKind::Closure` a struct variant.
Diffstat (limited to 'compiler')
39 files changed, 139 insertions, 118 deletions
diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 539e33702aa..78a59d603a2 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -577,7 +577,7 @@ impl<'hir> LoweringContext<'_, 'hir> { }; // The closure/generator `FnDecl` takes a single (resume) argument of type `input_ty`. - let decl = self.arena.alloc(hir::FnDecl { + let fn_decl = self.arena.alloc(hir::FnDecl { inputs: arena_vec![self; input_ty], output, c_variadic: false, @@ -598,7 +598,7 @@ impl<'hir> LoweringContext<'_, 'hir> { }; let params = arena_vec![self; param]; - let body_id = self.lower_body(move |this| { + let body = self.lower_body(move |this| { this.generator_kind = Some(hir::GeneratorKind::Async(async_gen_kind)); let old_ctx = this.task_context; @@ -609,13 +609,13 @@ impl<'hir> LoweringContext<'_, 'hir> { }); // `static |_task_context| -> <ret_ty> { body }`: - let generator_kind = hir::ExprKind::Closure( + let generator_kind = hir::ExprKind::Closure { capture_clause, - decl, - body_id, - self.lower_span(span), - Some(hir::Movability::Static), - ); + fn_decl, + body, + fn_decl_span: self.lower_span(span), + movability: Some(hir::Movability::Static), + }; let generator = hir::Expr { hir_id: self.lower_node_id(closure_node_id), kind: generator_kind, @@ -840,7 +840,7 @@ impl<'hir> LoweringContext<'_, 'hir> { body: &Expr, fn_decl_span: Span, ) -> hir::ExprKind<'hir> { - let (body_id, generator_option) = self.with_new_scopes(move |this| { + let (body, generator_option) = self.with_new_scopes(move |this| { let prev = this.current_item; this.current_item = Some(fn_decl_span); let mut generator_kind = None; @@ -858,13 +858,13 @@ impl<'hir> LoweringContext<'_, 'hir> { // Lower outside new scope to preserve `is_in_loop_condition`. let fn_decl = self.lower_fn_decl(decl, None, FnDeclKind::Closure, None); - hir::ExprKind::Closure( + hir::ExprKind::Closure { capture_clause, fn_decl, - body_id, - self.lower_span(fn_decl_span), - generator_option, - ) + body, + fn_decl_span: self.lower_span(fn_decl_span), + movability: generator_option, + } } fn generator_movability_for_fn( @@ -911,7 +911,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let outer_decl = FnDecl { inputs: decl.inputs.clone(), output: FnRetTy::Default(fn_decl_span) }; - let body_id = self.with_new_scopes(|this| { + let body = self.with_new_scopes(|this| { // FIXME(cramertj): allow `async` non-`move` closures with arguments. if capture_clause == CaptureBy::Ref && !decl.inputs.is_empty() { struct_span_err!( @@ -950,13 +950,13 @@ impl<'hir> LoweringContext<'_, 'hir> { // closure argument types. let fn_decl = self.lower_fn_decl(&outer_decl, None, FnDeclKind::Closure, None); - hir::ExprKind::Closure( + hir::ExprKind::Closure { capture_clause, fn_decl, - body_id, - self.lower_span(fn_decl_span), - None, - ) + body, + fn_decl_span: self.lower_span(fn_decl_span), + movability: None, + } } /// Destructure the LHS of complex assignments. diff --git a/compiler/rustc_borrowck/src/diagnostics/mod.rs b/compiler/rustc_borrowck/src/diagnostics/mod.rs index 9581bb65236..474544f320a 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mod.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mod.rs @@ -896,7 +896,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_id, args_span, _) = expr { + if let hir::ExprKind::Closure { body, fn_decl_span, .. } = expr { for (captured_place, place) in self .infcx .tcx @@ -909,11 +909,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_id); + let body = self.infcx.tcx.hir().body(*body); let generator_kind = body.generator_kind(); return Some(( - *args_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 fcb6ae438fe..d6b5089712a 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_name.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_name.rs @@ -311,8 +311,9 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { // Can't have BrEnv in functions, constants or generators. bug!("BrEnv outside of closure."); }; - let hir::ExprKind::Closure(_, _, _, args_span, _) = - tcx.hir().expect_expr(self.mir_hir_id()).kind else { + let hir::ExprKind::Closure { fn_decl_span, .. } + = tcx.hir().expect_expr(self.mir_hir_id()).kind + else { bug!("Closure is not defined by a closure expr"); }; let region_name = self.synthesize_region_name(); @@ -336,7 +337,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { Some(RegionName { name: region_name, source: RegionNameSource::SynthesizedFreeEnvRegion( - args_span, + fn_decl_span, note.to_string(), ), }) @@ -683,16 +684,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(_, return_ty, body_id, span, _), + kind: hir::ExprKind::Closure { fn_decl, body, fn_decl_span, .. }, .. }) => { - let (mut span, mut hir_ty) = match return_ty.output { + let (mut span, mut hir_ty) = match fn_decl.output { hir::FnRetTy::DefaultReturn(_) => { - (tcx.sess.source_map().end_point(*span), None) + (tcx.sess.source_map().end_point(*fn_decl_span), None) } - hir::FnRetTy::Return(hir_ty) => (return_ty.output.span(), Some(hir_ty)), + hir::FnRetTy::Return(hir_ty) => (fn_decl.output.span(), Some(hir_ty)), }; - let mir_description = match hir.body(*body_id).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", @@ -822,8 +823,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(_, _, _, span, _), .. - }) => (tcx.sess.source_map().end_point(*span)), + kind: hir::ExprKind::Closure { fn_decl_span, .. }, + .. + }) => (tcx.sess.source_map().end_point(*fn_decl_span)), _ => self.body.span, }; diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 2f5f271dc50..60f8bb51d65 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1651,7 +1651,7 @@ impl Expr<'_> { ExprKind::Let(..) => ExprPrecedence::Let, ExprKind::Loop(..) => ExprPrecedence::Loop, ExprKind::Match(..) => ExprPrecedence::Match, - ExprKind::Closure(..) => ExprPrecedence::Closure, + ExprKind::Closure { .. } => ExprPrecedence::Closure, ExprKind::Block(..) => ExprPrecedence::Block, ExprKind::Assign(..) => ExprPrecedence::Assign, ExprKind::AssignOp(..) => ExprPrecedence::AssignOp, @@ -1711,7 +1711,7 @@ impl Expr<'_> { | ExprKind::Tup(..) | ExprKind::If(..) | ExprKind::Match(..) - | ExprKind::Closure(..) + | ExprKind::Closure { .. } | ExprKind::Block(..) | ExprKind::Repeat(..) | ExprKind::Array(..) @@ -1794,7 +1794,7 @@ impl Expr<'_> { | ExprKind::Match(..) | ExprKind::MethodCall(..) | ExprKind::Call(..) - | ExprKind::Closure(..) + | ExprKind::Closure { .. } | ExprKind::Block(..) | ExprKind::Repeat(..) | ExprKind::Break(..) @@ -1929,7 +1929,13 @@ pub enum ExprKind<'hir> { /// /// This may also be a generator literal or an `async block` as indicated by the /// `Option<Movability>`. - Closure(CaptureBy, &'hir FnDecl<'hir>, BodyId, Span, Option<Movability>), + Closure { + capture_clause: CaptureBy, + fn_decl: &'hir FnDecl<'hir>, + body: BodyId, + fn_decl_span: Span, + movability: Option<Movability>, + }, /// A block (e.g., `'label: { ... }`). Block(&'hir Block<'hir>, Option<Label>), @@ -3455,7 +3461,7 @@ impl<'hir> Node<'hir> { _ => None, }, Node::Expr(e) => match e.kind { - ExprKind::Closure(..) => Some(FnKind::Closure), + ExprKind::Closure { .. } => Some(FnKind::Closure), _ => None, }, _ => None, diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index bd8587f1106..d1da2519bad 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -1168,14 +1168,13 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) visitor.visit_expr(subexpression); walk_list!(visitor, visit_arm, arms); } - ExprKind::Closure(_, ref function_declaration, body, _fn_decl_span, _gen) => visitor - .visit_fn( - FnKind::Closure, - function_declaration, - body, - expression.span, - expression.hir_id, - ), + ExprKind::Closure { + ref fn_decl, + body, + capture_clause: _, + fn_decl_span: _, + movability: _, + } => visitor.visit_fn(FnKind::Closure, fn_decl, body, expression.span, expression.hir_id), ExprKind::Block(ref block, ref opt_label) => { walk_list!(visitor, visit_label, opt_label); visitor.visit_block(block); diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index fb40008d60b..7317ce7335a 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -1078,7 +1078,9 @@ impl<'a> State<'a> { // parses as the erroneous construct `if (return {})`, not `if (return) {}`. fn cond_needs_par(expr: &hir::Expr<'_>) -> bool { match expr.kind { - hir::ExprKind::Break(..) | hir::ExprKind::Closure(..) | hir::ExprKind::Ret(..) => true, + hir::ExprKind::Break(..) | hir::ExprKind::Closure { .. } | hir::ExprKind::Ret(..) => { + true + } _ => contains_exterior_struct_lit(expr), } } @@ -1455,10 +1457,16 @@ impl<'a> State<'a> { } self.bclose(expr.span); } - hir::ExprKind::Closure(capture_clause, ref decl, body, _fn_decl_span, _gen) => { + hir::ExprKind::Closure { + capture_clause, + ref fn_decl, + body, + fn_decl_span: _, + movability: _, + } => { self.print_capture_clause(capture_clause); - self.print_closure_params(&decl, body); + self.print_closure_params(&fn_decl, body); self.space(); // This is a bare expression. diff --git a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs index 207d2870c5c..400c1968bf5 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs @@ -991,22 +991,24 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> { } if let Some(node_ty) = self.opt_node_type(expr.hir_id) { - if let (&ExprKind::Closure(_, decl, body_id, span, _), ty::Closure(_, substs)) = - (&expr.kind, node_ty.kind()) + if let ( + &ExprKind::Closure { fn_decl, body, fn_decl_span, .. }, + ty::Closure(_, substs), + ) = (&expr.kind, node_ty.kind()) { let output = substs.as_closure().sig().output().skip_binder(); if self.generic_arg_contains_target(output.into()) { - let body = self.infcx.tcx.hir().body(body_id); + let body = self.infcx.tcx.hir().body(body); let should_wrap_expr = if matches!(body.value.kind, ExprKind::Block(..)) { None } else { Some(body.value.span.shrink_to_hi()) }; self.update_infer_source(InferSource { - span, + span: fn_decl_span, kind: InferSourceKind::ClosureReturn { ty: output, - data: &decl.output, + data: &fn_decl.output, should_wrap_expr, }, }) diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs index 96b57b6cd20..b9596cd10ed 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs @@ -55,7 +55,7 @@ pub fn find_param_with_region<'tcx>( // Don't perform this on closures match hir.get(hir_id) { - hir::Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => { + hir::Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => { return None; } _ => {} diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 3285273ba90..b06470b4318 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -2030,7 +2030,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } fn encode_info_for_expr(&mut self, expr: &hir::Expr<'_>) { - if let hir::ExprKind::Closure(..) = expr.kind { + if let hir::ExprKind::Closure { .. } = expr.kind { self.encode_info_for_closure(expr.hir_id); } } diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index ebda9f7588d..1b1c5cb7728 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -22,7 +22,7 @@ fn fn_decl<'hir>(node: Node<'hir>) -> Option<&'hir FnDecl<'hir>> { Node::Item(Item { kind: ItemKind::Fn(sig, _, _), .. }) | Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(sig, _), .. }) | Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(sig, _), .. }) => Some(&sig.decl), - Node::Expr(Expr { kind: ExprKind::Closure(_, fn_decl, ..), .. }) + Node::Expr(Expr { kind: ExprKind::Closure { fn_decl, .. }, .. }) | Node::ForeignItem(ForeignItem { kind: ForeignItemKind::Fn(fn_decl, ..), .. }) => { Some(fn_decl) } @@ -54,7 +54,7 @@ pub fn associated_body<'hir>(node: Node<'hir>) -> Option<BodyId> { kind: ImplItemKind::Const(_, body) | ImplItemKind::Fn(_, body), .. }) - | Node::Expr(Expr { kind: ExprKind::Closure(.., body, _, _), .. }) => Some(*body), + | Node::Expr(Expr { kind: ExprKind::Closure { body, .. }, .. }) => Some(*body), Node::AnonConst(constant) => Some(constant.body), @@ -285,8 +285,8 @@ impl<'hir> Map<'hir> { } Node::Field(_) => DefKind::Field, Node::Expr(expr) => match expr.kind { - ExprKind::Closure(.., None) => DefKind::Closure, - ExprKind::Closure(.., Some(_)) => DefKind::Generator, + ExprKind::Closure { movability: None, .. } => DefKind::Closure, + ExprKind::Closure { movability: Some(_), .. } => DefKind::Generator, _ => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)), }, Node::GenericParam(param) => match param.kind { @@ -758,7 +758,7 @@ impl<'hir> Map<'hir> { Node::Item(_) | Node::ForeignItem(_) | Node::TraitItem(_) - | Node::Expr(Expr { kind: ExprKind::Closure(..), .. }) + | Node::Expr(Expr { kind: ExprKind::Closure { .. }, .. }) | Node::ImplItem(_) => return Some(hir_id), // Ignore `return`s on the first iteration Node::Expr(Expr { kind: ExprKind::Loop(..) | ExprKind::Ret(..), .. }) diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 610234d45ce..fda16eae82e 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1550,7 +1550,7 @@ impl<'tcx> TyCtxt<'tcx> { Node::Item(&hir::Item { kind: ItemKind::Fn(..), .. }) => {} Node::TraitItem(&hir::TraitItem { kind: TraitItemKind::Fn(..), .. }) => {} Node::ImplItem(&hir::ImplItem { kind: ImplItemKind::Fn(..), .. }) => {} - Node::Expr(&hir::Expr { kind: ExprKind::Closure(..), .. }) => {} + Node::Expr(&hir::Expr { kind: ExprKind::Closure { .. }, .. }) => {} _ => return None, } diff --git a/compiler/rustc_mir_build/src/build/mod.rs b/compiler/rustc_mir_build/src/build/mod.rs index 4ae74433df6..3d8f92bae7c 100644 --- a/compiler/rustc_mir_build/src/build/mod.rs +++ b/compiler/rustc_mir_build/src/build/mod.rs @@ -66,8 +66,8 @@ fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_ // Figure out what primary body this item has. let (body_id, return_ty_span, span_with_body) = match tcx.hir().get(id) { - Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(_, decl, body_id, _, _), .. }) => { - (*body_id, decl.output.span(), None) + Node::Expr(hir::Expr { kind: hir::ExprKind::Closure { fn_decl, body, .. }, .. }) => { + (*body, fn_decl.output.span(), None) } Node::Item(hir::Item { kind: hir::ItemKind::Fn(hir::FnSig { decl, .. }, _, body_id), diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index bd9f599fff0..56e2dcd0509 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -418,7 +418,7 @@ impl<'tcx> Cx<'tcx> { } }, - hir::ExprKind::Closure(..) => { + hir::ExprKind::Closure { .. } => { let closure_ty = self.typeck_results().expr_ty(expr); let (def_id, substs, movability) = match *closure_ty.kind() { ty::Closure(def_id, substs) => (def_id, UpvarSubsts::Closure(substs), None), diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 5cc97d326d3..a10606a1dcd 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -2340,7 +2340,7 @@ impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> { fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { let target = match expr.kind { - hir::ExprKind::Closure(..) => Target::Closure, + hir::ExprKind::Closure { .. } => Target::Closure, _ => Target::Expression, }; diff --git a/compiler/rustc_passes/src/liveness.rs b/compiler/rustc_passes/src/liveness.rs index b09d9831d43..80a263f4cb2 100644 --- a/compiler/rustc_passes/src/liveness.rs +++ b/compiler/rustc_passes/src/liveness.rs @@ -405,7 +405,7 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> { } intravisit::walk_expr(self, expr); } - hir::ExprKind::Closure(..) => { + hir::ExprKind::Closure { .. } => { // Interesting control flow (for loops can contain labeled // breaks or continues) self.add_live_node_for_node(expr.hir_id, ExprNode(expr.span, expr.hir_id)); @@ -833,7 +833,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> { hir::ExprKind::Field(ref e, _) => self.propagate_through_expr(&e, succ), - hir::ExprKind::Closure(..) => { + hir::ExprKind::Closure { .. } => { debug!("{:?} is an ExprKind::Closure", expr); // the construction of a closure itself is not important, @@ -1387,7 +1387,7 @@ fn check_expr<'tcx>(this: &mut Liveness<'_, 'tcx>, expr: &'tcx Expr<'tcx>) { | hir::ExprKind::AddrOf(..) | hir::ExprKind::Struct(..) | hir::ExprKind::Repeat(..) - | hir::ExprKind::Closure(..) + | hir::ExprKind::Closure { .. } | hir::ExprKind::Path(_) | hir::ExprKind::Yield(..) | hir::ExprKind::Box(..) diff --git a/compiler/rustc_passes/src/loops.rs b/compiler/rustc_passes/src/loops.rs index e0dac09870d..191a682e8b1 100644 --- a/compiler/rustc_passes/src/loops.rs +++ b/compiler/rustc_passes/src/loops.rs @@ -57,14 +57,14 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> { hir::ExprKind::Loop(ref b, _, source, _) => { self.with_context(Loop(source), |v| v.visit_block(&b)); } - hir::ExprKind::Closure(_, ref function_decl, b, span, movability) => { + hir::ExprKind::Closure { ref fn_decl, body, fn_decl_span, movability, .. } => { let cx = if let Some(Movability::Static) = movability { - AsyncClosure(span) + AsyncClosure(fn_decl_span) } else { - Closure(span) + Closure(fn_decl_span) }; - self.visit_fn_decl(&function_decl); - self.with_context(cx, |v| v.visit_nested_body(b)); + self.visit_fn_decl(&fn_decl); + self.with_context(cx, |v| v.visit_nested_body(body)); } hir::ExprKind::Block(ref b, Some(_label)) => { self.with_context(LabeledBlock, |v| v.visit_block(&b)); diff --git a/compiler/rustc_passes/src/naked_functions.rs b/compiler/rustc_passes/src/naked_functions.rs index 83f728d4076..2f92adc3ee1 100644 --- a/compiler/rustc_passes/src/naked_functions.rs +++ b/compiler/rustc_passes/src/naked_functions.rs @@ -212,7 +212,7 @@ impl<'tcx> CheckInlineAssembly<'tcx> { | ExprKind::Loop(..) | ExprKind::Match(..) | ExprKind::If(..) - | ExprKind::Closure(..) + | ExprKind::Closure { .. } | ExprKind::Assign(..) | ExprKind::AssignOp(..) | ExprKind::Field(..) diff --git a/compiler/rustc_passes/src/reachable.rs b/compiler/rustc_passes/src/reachable.rs index 75376cdc592..6e622ff031e 100644 --- a/compiler/rustc_passes/src/reachable.rs +++ b/compiler/rustc_passes/src/reachable.rs @@ -273,7 +273,7 @@ impl<'tcx> ReachableContext<'tcx> { } hir::ImplItemKind::TyAlias(_) => {} }, - Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(.., body, _, _), .. }) => { + Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { body, .. }, .. }) => { self.visit_nested_body(body); } // Nothing to recurse on for these diff --git a/compiler/rustc_passes/src/upvars.rs b/compiler/rustc_passes/src/upvars.rs index 25fe8e45825..97a461272b4 100644 --- a/compiler/rustc_passes/src/upvars.rs +++ b/compiler/rustc_passes/src/upvars.rs @@ -75,7 +75,7 @@ impl<'tcx> Visitor<'tcx> for CaptureCollector<'_, 'tcx> { } fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { - if let hir::ExprKind::Closure(..) = expr.kind { + if let hir::ExprKind::Closure { .. } = expr.kind { let closure_def_id = self.tcx.hir().local_def_id(expr.hir_id); if let Some(upvars) = self.tcx.upvars_mentioned(closure_def_id) { // Every capture of a closure expression is a local in scope, diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index e77e5a3ca02..b27c986d0f9 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -1981,7 +1981,7 @@ fn visibility(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Visibility { match tcx.hir().get(hir_id) { // Unique types created for closures participate in type privacy checking. // They have visibilities inherited from the module they are defined in. - Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(..), .. }) + Node::Expr(hir::Expr { kind: hir::ExprKind::Closure{..}, .. }) // - AST lowering creates dummy `use` items which don't // get their entries in the resolver's visibility table. // - AST lowering also creates opaque type items with inherited visibilities. diff --git a/compiler/rustc_save_analysis/src/dump_visitor.rs b/compiler/rustc_save_analysis/src/dump_visitor.rs index fe417f45e88..6eb2f2d929d 100644 --- a/compiler/rustc_save_analysis/src/dump_visitor.rs +++ b/compiler/rustc_save_analysis/src/dump_visitor.rs @@ -1360,15 +1360,15 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> { } } } - hir::ExprKind::Closure(_, ref decl, body, _fn_decl_span, _) => { + hir::ExprKind::Closure { ref fn_decl, body, .. } => { let id = format!("${}", ex.hir_id); // walk arg and return types - for ty in decl.inputs { + for ty in fn_decl.inputs { self.visit_ty(ty); } - if let hir::FnRetTy::Return(ref ret_ty) = decl.output { + if let hir::FnRetTy::Return(ref ret_ty) = fn_decl.output { self.visit_ty(ret_ty); } diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 2e7067fa710..76bf0cf7e68 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -1084,11 +1084,11 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { let hir = self.tcx.hir(); Some(match node { Node::Expr(&hir::Expr { - kind: hir::ExprKind::Closure(_, ref _decl, id, span, _), + kind: hir::ExprKind::Closure { body, fn_decl_span, .. }, .. }) => ( - sm.guess_head_span(span), - hir.body(id) + sm.guess_head_span(fn_decl_span), + hir.body(body) .params .iter() .map(|arg| { diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs index 4263a6fdf18..aae0500d9d7 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs @@ -103,10 +103,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { }) }), hir::Node::Expr(hir::Expr { - kind: hir::ExprKind::Closure(_is_move, _, body_id, _, gen_movability), + kind: hir::ExprKind::Closure { body, movability, .. }, .. - }) => self.describe_generator(*body_id).or_else(|| { - Some(if gen_movability.is_some() { "an async closure" } else { "a closure" }) + }) => self.describe_generator(*body).or_else(|| { + Some(if movability.is_some() { "an async closure" } else { "a closure" }) }), hir::Node::Expr(hir::Expr { .. }) => { let parent_hid = hir.get_parent_node(hir_id); diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 185f500808f..c5fddb7fdfd 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -775,14 +775,14 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { // Get the name of the callable and the arguments to be used in the suggestion. let (snippet, sugg) = match hir.get_if_local(def_id) { Some(hir::Node::Expr(hir::Expr { - kind: hir::ExprKind::Closure(_, decl, _, span, ..), + kind: hir::ExprKind::Closure { fn_decl, fn_decl_span, .. }, .. })) => { - err.span_label(*span, "consider calling this closure"); + err.span_label(*fn_decl_span, "consider calling this closure"); let Some(name) = self.get_closure_name(def_id, err, &msg) else { return false; }; - let args = decl.inputs.iter().map(|_| "_").collect::<Vec<_>>().join(", "); + let args = fn_decl.inputs.iter().map(|_| "_").collect::<Vec<_>>().join(", "); let sugg = format!("({})", args); (format!("{}{}", name, sugg), sugg) } diff --git a/compiler/rustc_typeck/src/check/callee.rs b/compiler/rustc_typeck/src/check/callee.rs index af1288b6523..9e23566ae19 100644 --- a/compiler/rustc_typeck/src/check/callee.rs +++ b/compiler/rustc_typeck/src/check/callee.rs @@ -283,11 +283,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let hir_id = self.tcx.hir().get_parent_node(hir_id); let parent_node = self.tcx.hir().get(hir_id); if let ( - hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(_, _, _, sp, ..), .. }), + hir::Node::Expr(hir::Expr { + kind: hir::ExprKind::Closure { fn_decl_span, .. }, .. + }), hir::ExprKind::Block(..), ) = (parent_node, callee_node) { - let start = sp.shrink_to_lo(); + let start = fn_decl_span.shrink_to_lo(); let end = callee_span.shrink_to_hi(); err.multipart_suggestion( "if you meant to create this closure and immediately call it, surround the \ diff --git a/compiler/rustc_typeck/src/check/check.rs b/compiler/rustc_typeck/src/check/check.rs index 80abb28ee58..44d2c2e90e2 100644 --- a/compiler/rustc_typeck/src/check/check.rs +++ b/compiler/rustc_typeck/src/check/check.rs @@ -124,7 +124,7 @@ pub(super) fn check_fn<'a, 'tcx>( .. }) => Some(header), // Closures are RustCall, but they tuple their arguments, so shouldn't be checked - Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => None, + Node::Expr(hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => None, node => bug!("Item being checked wasn't a function/closure: {:?}", node), }; diff --git a/compiler/rustc_typeck/src/check/coercion.rs b/compiler/rustc_typeck/src/check/coercion.rs index d6a8659d54b..43fc49c6801 100644 --- a/compiler/rustc_typeck/src/check/coercion.rs +++ b/compiler/rustc_typeck/src/check/coercion.rs @@ -1577,8 +1577,8 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { let parent_id = fcx.tcx.hir().get_parent_node(id); let parent = fcx.tcx.hir().get(parent_id); if let Some(expr) = expression - && let hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(_, _, body_id, ..), .. }) = parent - && !matches!(fcx.tcx.hir().get(body_id.hir_id), hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Block(..), .. })) + && let hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Closure { body, .. }, .. }) = parent + && !matches!(fcx.tcx.hir().body(*body).value.kind, hir::ExprKind::Block(..)) { fcx.suggest_missing_semicolon(&mut err, expr, expected, true); } diff --git a/compiler/rustc_typeck/src/check/demand.rs b/compiler/rustc_typeck/src/check/demand.rs index 419ccb5a73a..961bbc42661 100644 --- a/compiler/rustc_typeck/src/check/demand.rs +++ b/compiler/rustc_typeck/src/check/demand.rs @@ -483,7 +483,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let param_parent = self.tcx.hir().get_parent_node(*param_hir_id); let Some(Node::Expr(hir::Expr { hir_id: expr_hir_id, - kind: hir::ExprKind::Closure(_, closure_fn_decl, ..), + kind: hir::ExprKind::Closure { fn_decl: closure_fn_decl, .. }, .. })) = self.tcx.hir().find(param_parent) else { return None; diff --git a/compiler/rustc_typeck/src/check/expr.rs b/compiler/rustc_typeck/src/check/expr.rs index 48bbd4d76ea..5ce750b628c 100644 --- a/compiler/rustc_typeck/src/check/expr.rs +++ b/compiler/rustc_typeck/src/check/expr.rs @@ -319,8 +319,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ExprKind::Match(discrim, arms, match_src) => { self.check_match(expr, &discrim, arms, expected, match_src) } - ExprKind::Closure(capture, decl, body_id, _, gen) => { - self.check_expr_closure(expr, capture, &decl, body_id, gen, expected) + ExprKind::Closure { capture_clause, fn_decl, body, movability, .. } => { + self.check_expr_closure(expr, capture_clause, &fn_decl, body, movability, expected) } ExprKind::Block(body, _) => self.check_block_with_expected(&body, expected), ExprKind::Call(callee, args) => self.check_call(expr, &callee, args, expected), diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs b/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs index 0d0cc929839..089a4deb32c 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs @@ -387,7 +387,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { continue; } - let is_closure = matches!(arg.kind, ExprKind::Closure(..)); + let is_closure = matches!(arg.kind, ExprKind::Closure { .. }); if is_closure != check_closures { continue; } @@ -1774,10 +1774,10 @@ fn label_fn_like<'tcx>( } else { match tcx.hir().get_if_local(def_id) { Some(hir::Node::Expr(hir::Expr { - kind: hir::ExprKind::Closure(_, _, _, span, ..), + kind: hir::ExprKind::Closure { fn_decl_span, .. }, .. })) => { - let spans: MultiSpan = (*span).into(); + let spans: MultiSpan = (*fn_decl_span).into(); // Note: We don't point to param spans here because they overlap // with the closure span itself diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs b/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs index ca55a4299eb..34a77645aeb 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs @@ -121,7 +121,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .join(", "); } Some(Node::Expr(hir::Expr { - kind: ExprKind::Closure(_, _, body_id, _, _), + kind: ExprKind::Closure { body: body_id, .. }, span: full_closure_span, .. })) => { diff --git a/compiler/rustc_typeck/src/check/generator_interior/drop_ranges/cfg_build.rs b/compiler/rustc_typeck/src/check/generator_interior/drop_ranges/cfg_build.rs index 58b63804b4a..dbc309b29ff 100644 --- a/compiler/rustc_typeck/src/check/generator_interior/drop_ranges/cfg_build.rs +++ b/compiler/rustc_typeck/src/check/generator_interior/drop_ranges/cfg_build.rs @@ -188,7 +188,7 @@ impl<'a, 'tcx> DropRangeVisitor<'a, 'tcx> { | ExprKind::If(..) | ExprKind::Loop(..) | ExprKind::Match(..) - | ExprKind::Closure(..) + | ExprKind::Closure { .. } | ExprKind::Block(..) | ExprKind::Assign(..) | ExprKind::AssignOp(..) @@ -444,7 +444,7 @@ impl<'a, 'tcx> Visitor<'tcx> for DropRangeVisitor<'a, 'tcx> { | ExprKind::Block(..) | ExprKind::Box(..) | ExprKind::Cast(..) - | ExprKind::Closure(..) + | ExprKind::Closure { .. } | ExprKind::ConstBlock(..) | ExprKind::DropTemps(..) | ExprKind::Err diff --git a/compiler/rustc_typeck/src/check/region.rs b/compiler/rustc_typeck/src/check/region.rs index 6c89c12b470..9f1368a3e07 100644 --- a/compiler/rustc_typeck/src/check/region.rs +++ b/compiler/rustc_typeck/src/check/region.rs @@ -335,7 +335,7 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h match expr.kind { // Manually recurse over closures and inline consts, because they are the only // case of nested bodies that share the parent environment. - hir::ExprKind::Closure(.., body, _, _) + hir::ExprKind::Closure { body, .. } | hir::ExprKind::ConstBlock(hir::AnonConst { body, .. }) => { let body = visitor.tcx.hir().body(body); visitor.visit_body(body); diff --git a/compiler/rustc_typeck/src/check/upvar.rs b/compiler/rustc_typeck/src/check/upvar.rs index 8074ff368cc..0837d9c4a20 100644 --- a/compiler/rustc_typeck/src/check/upvar.rs +++ b/compiler/rustc_typeck/src/check/upvar.rs @@ -142,10 +142,10 @@ struct InferBorrowKindVisitor<'a, 'tcx> { impl<'a, 'tcx> Visitor<'tcx> for InferBorrowKindVisitor<'a, 'tcx> { fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { match expr.kind { - hir::ExprKind::Closure(cc, _, body_id, _, _) => { + hir::ExprKind::Closure { capture_clause, body: body_id, .. } => { let body = self.fcx.tcx.hir().body(body_id); self.visit_body(body); - self.fcx.analyze_closure(expr.hir_id, expr.span, body_id, body, cc); + self.fcx.analyze_closure(expr.hir_id, expr.span, body_id, body, capture_clause); } hir::ExprKind::ConstBlock(anon_const) => { let body = self.fcx.tcx.hir().body(anon_const.body); diff --git a/compiler/rustc_typeck/src/check/writeback.rs b/compiler/rustc_typeck/src/check/writeback.rs index dc135f002f4..9459cf5f8ca 100644 --- a/compiler/rustc_typeck/src/check/writeback.rs +++ b/compiler/rustc_typeck/src/check/writeback.rs @@ -262,7 +262,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for WritebackCx<'cx, 'tcx> { self.fix_index_builtin_expr(e); match e.kind { - hir::ExprKind::Closure(_, _, body, _, _) => { + hir::ExprKind::Closure { body, .. } => { let body = self.fcx.tcx.hir().body(body); for param in body.params { self.visit_node_id(e.span, param.hir_id); diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index de40126e724..b3cde2fdd31 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -295,7 +295,7 @@ impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> { } fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { - if let hir::ExprKind::Closure(..) = expr.kind { + if let hir::ExprKind::Closure { .. } = expr.kind { let def_id = self.tcx.hir().local_def_id(expr.hir_id); self.tcx.ensure().generics_of(def_id); // We do not call `type_of` for closures here as that @@ -1567,7 +1567,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics { } } } - Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => { + Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => { Some(tcx.typeck_root_def_id(def_id)) } Node::Item(item) => match item.kind { @@ -1718,7 +1718,9 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics { // provide junk type parameter defs - the only place that // cares about anything but the length is instantiation, // and we don't do that for closures. - if let Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(.., gen), .. }) = node { + if let Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { movability: gen, .. }, .. }) = + node + { let dummy_args = if gen.is_some() { &["<resume_ty>", "<yield_ty>", "<return_ty>", "<witness>", "<upvars>"][..] } else { @@ -1881,7 +1883,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> { )) } - Expr(&hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => { + Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => { // Closure signatures are not like other function // signatures and cannot be accessed through `fn_sig`. For // example, a closure signature excludes the `self` @@ -2567,9 +2569,9 @@ fn is_foreign_item(tcx: TyCtxt<'_>, def_id: DefId) -> bool { fn generator_kind(tcx: TyCtxt<'_>, def_id: DefId) -> Option<hir::GeneratorKind> { match tcx.hir().get_if_local(def_id) { Some(Node::Expr(&rustc_hir::Expr { - kind: rustc_hir::ExprKind::Closure(_, _, body_id, _, _), + kind: rustc_hir::ExprKind::Closure { body, .. }, .. - })) => tcx.hir().body(body_id).generator_kind(), + })) => tcx.hir().body(body).generator_kind(), Some(_) => None, _ => bug!("generator_kind applied to non-local def-id {:?}", def_id), } diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index 451a953691b..9aaed86f7f5 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -405,7 +405,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { Node::Field(field) => icx.to_ty(field.ty), - Node::Expr(&Expr { kind: ExprKind::Closure(..), .. }) => tcx.typeck(def_id).node_type(hir_id), + Node::Expr(&Expr { kind: ExprKind::Closure{..}, .. }) => tcx.typeck(def_id).node_type(hir_id), Node::AnonConst(_) if let Some(param) = tcx.opt_const_param_of(def_id) => { // We defer to `type_of` of the corresponding parameter @@ -593,7 +593,7 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> { self.tcx.hir() } fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) { - if let hir::ExprKind::Closure(..) = ex.kind { + if let hir::ExprKind::Closure { .. } = ex.kind { let def_id = self.tcx.hir().local_def_id(ex.hir_id); self.check(def_id); } diff --git a/compiler/rustc_typeck/src/expr_use_visitor.rs b/compiler/rustc_typeck/src/expr_use_visitor.rs index 3ebb1dd83e1..920e3d578c8 100644 --- a/compiler/rustc_typeck/src/expr_use_visitor.rs +++ b/compiler/rustc_typeck/src/expr_use_visitor.rs @@ -437,7 +437,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> { self.consume_expr(base); } - hir::ExprKind::Closure(..) => { + hir::ExprKind::Closure { .. } => { self.walk_captures(expr); } diff --git a/compiler/rustc_typeck/src/mem_categorization.rs b/compiler/rustc_typeck/src/mem_categorization.rs index 21916352532..9acae8d79e7 100644 --- a/compiler/rustc_typeck/src/mem_categorization.rs +++ b/compiler/rustc_typeck/src/mem_categorization.rs @@ -359,7 +359,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> { | hir::ExprKind::Call(..) | hir::ExprKind::Assign(..) | hir::ExprKind::AssignOp(..) - | hir::ExprKind::Closure(..) + | hir::ExprKind::Closure { .. } | hir::ExprKind::Ret(..) | hir::ExprKind::Unary(..) | hir::ExprKind::Yield(..) |
