From 3039cfeb6a24c65ab4b7d25f1c60dc0a5df836ac Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 11 Jun 2022 21:25:25 +0200 Subject: [PATCH 1/4] Make `ExprKind::Closure` a struct variant. --- compiler/rustc_ast_lowering/src/expr.rs | 40 +++++++++--------- .../rustc_borrowck/src/diagnostics/mod.rs | 6 +-- .../src/diagnostics/region_name.rs | 22 +++++----- compiler/rustc_hir/src/hir.rs | 16 +++++--- compiler/rustc_hir/src/intravisit.rs | 15 ++++--- compiler/rustc_hir_pretty/src/lib.rs | 14 +++++-- .../infer/error_reporting/need_type_info.rs | 12 +++--- .../error_reporting/nice_region_error/util.rs | 2 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 2 +- compiler/rustc_middle/src/hir/map/mod.rs | 10 ++--- compiler/rustc_middle/src/ty/context.rs | 2 +- compiler/rustc_mir_build/src/build/mod.rs | 4 +- compiler/rustc_mir_build/src/thir/cx/expr.rs | 2 +- compiler/rustc_passes/src/check_attr.rs | 2 +- compiler/rustc_passes/src/liveness.rs | 6 +-- compiler/rustc_passes/src/loops.rs | 10 ++--- compiler/rustc_passes/src/naked_functions.rs | 2 +- compiler/rustc_passes/src/reachable.rs | 2 +- compiler/rustc_passes/src/upvars.rs | 2 +- compiler/rustc_privacy/src/lib.rs | 2 +- .../rustc_save_analysis/src/dump_visitor.rs | 6 +-- .../src/traits/error_reporting/mod.rs | 6 +-- .../error_reporting/on_unimplemented.rs | 6 +-- .../src/traits/error_reporting/suggestions.rs | 6 +-- compiler/rustc_typeck/src/check/callee.rs | 6 ++- compiler/rustc_typeck/src/check/check.rs | 2 +- compiler/rustc_typeck/src/check/coercion.rs | 4 +- compiler/rustc_typeck/src/check/demand.rs | 2 +- compiler/rustc_typeck/src/check/expr.rs | 4 +- .../rustc_typeck/src/check/fn_ctxt/checks.rs | 6 +-- .../src/check/fn_ctxt/suggestions.rs | 2 +- .../drop_ranges/cfg_build.rs | 4 +- compiler/rustc_typeck/src/check/region.rs | 2 +- compiler/rustc_typeck/src/check/upvar.rs | 4 +- compiler/rustc_typeck/src/check/writeback.rs | 2 +- compiler/rustc_typeck/src/collect.rs | 14 ++++--- compiler/rustc_typeck/src/collect/type_of.rs | 4 +- compiler/rustc_typeck/src/expr_use_visitor.rs | 2 +- .../rustc_typeck/src/mem_categorization.rs | 2 +- .../src/blocks_in_if_conditions.rs | 4 +- .../clippy/clippy_lints/src/bytecount.rs | 4 +- .../clippy/clippy_lints/src/dereference.rs | 2 +- .../clippy/clippy_lints/src/eta_reduction.rs | 2 +- .../clippy/clippy_lints/src/infinite_iter.rs | 4 +- .../src/loops/needless_range_loop.rs | 4 +- .../clippy_lints/src/loops/never_loop.rs | 2 +- .../src/loops/while_let_on_iterator.rs | 6 +-- .../clippy_lints/src/manual_async_fn.rs | 4 +- .../clippy/clippy_lints/src/manual_ok_or.rs | 4 +- .../clippy/clippy_lints/src/map_clone.rs | 4 +- .../clippy/clippy_lints/src/map_err_ignore.rs | 17 +++++--- .../clippy/clippy_lints/src/map_unit_fn.rs | 8 ++-- .../src/matches/match_single_binding.rs | 2 +- .../matches/significant_drop_in_scrutinee.rs | 2 +- .../src/methods/bind_instead_of_map.rs | 6 +-- .../clippy_lints/src/methods/filter_map.rs | 8 ++-- .../src/methods/option_as_ref_deref.rs | 4 +- .../src/methods/option_map_or_none.rs | 41 +++++++++---------- .../src/methods/search_is_some.rs | 4 +- .../src/methods/unnecessary_filter_map.rs | 4 +- .../src/methods/unnecessary_fold.rs | 4 +- .../src/methods/unnecessary_lazy_eval.rs | 4 +- .../src/mixed_read_write_in_expression.rs | 6 +-- .../clippy_lints/src/needless_for_each.rs | 4 +- .../clippy/clippy_lints/src/no_effect.rs | 2 +- .../src/only_used_in_recursion.rs | 4 +- .../src/redundant_closure_call.rs | 2 +- .../src/unit_return_expecting_ord.rs | 8 ++-- .../clippy_lints/src/unnecessary_sort_by.rs | 2 +- .../clippy/clippy_lints/src/utils/author.rs | 10 ++++- .../clippy/clippy_utils/src/eager_or_lazy.rs | 2 +- .../clippy/clippy_utils/src/hir_utils.rs | 8 ++-- src/tools/clippy/clippy_utils/src/lib.rs | 8 ++-- src/tools/clippy/clippy_utils/src/sugg.rs | 8 ++-- src/tools/clippy/clippy_utils/src/usage.rs | 2 +- 75 files changed, 251 insertions(+), 216 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| -> { 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`. - Closure(CaptureBy, &'hir FnDecl<'hir>, BodyId, Span, Option), + Closure { + capture_clause: CaptureBy, + fn_decl: &'hir FnDecl<'hir>, + body: BodyId, + fn_decl_span: Span, + movability: Option, + }, /// A block (e.g., `'label: { ... }`). Block(&'hir Block<'hir>, Option