diff options
| author | Camille GILLOT <gillot.camille@gmail.com> | 2022-11-06 13:29:21 +0000 |
|---|---|---|
| committer | Camille GILLOT <gillot.camille@gmail.com> | 2022-11-13 14:04:02 +0000 |
| commit | 290f0781b4507b14060aa04db3f412914b1f117d (patch) | |
| tree | 418da3c3f79306b49b52a335b60cd1de8a3fe999 | |
| parent | dba1503ed3c364f388128d42633ba00258ffad49 (diff) | |
| download | rust-290f0781b4507b14060aa04db3f412914b1f117d.tar.gz rust-290f0781b4507b14060aa04db3f412914b1f117d.zip | |
Store LocalDefId in hir::Closure.
| -rw-r--r-- | compiler/rustc_ast_lowering/src/expr.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/hir.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/intravisit.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_hir_analysis/src/collect.rs | 7 | ||||
| -rw-r--r-- | compiler/rustc_hir_analysis/src/collect/type_of.rs | 10 | ||||
| -rw-r--r-- | compiler/rustc_hir_pretty/src/lib.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/encoder.rs | 10 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/hir/map/mod.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_passes/src/liveness.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_passes/src/upvars.rs | 5 |
10 files changed, 24 insertions, 23 deletions
diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index a4ae493af86..432223220c5 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -635,6 +635,7 @@ 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: &[], @@ -887,6 +888,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let fn_decl = self.lower_fn_decl(decl, None, fn_decl_span, FnDeclKind::Closure, None); let c = self.arena.alloc(hir::Closure { + def_id: self.local_def_id(closure_id), binder: binder_clause, capture_clause, bound_generic_params, @@ -991,6 +993,7 @@ impl<'hir> LoweringContext<'_, 'hir> { self.lower_fn_decl(&outer_decl, None, fn_decl_span, FnDeclKind::Closure, None); let c = self.arena.alloc(hir::Closure { + def_id: self.local_def_id(closure_id), binder: binder_clause, capture_clause, bound_generic_params, diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 82e260d158b..1be03552c88 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -921,6 +921,7 @@ pub struct Crate<'hir> { #[derive(Debug, HashStable_Generic)] pub struct Closure<'hir> { + pub def_id: LocalDefId, pub binder: ClosureBinder, pub capture_clause: CaptureBy, pub bound_generic_params: &'hir [GenericParam<'hir>], diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index 3ef58d7d705..d831c7f8591 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -733,6 +733,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) walk_list!(visitor, visit_arm, arms); } ExprKind::Closure(&Closure { + def_id: _, binder: _, bound_generic_params, fn_decl, diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 0f8d4e9654a..f122e99946f 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -311,10 +311,9 @@ impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> { } fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { - 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); - self.tcx.ensure().codegen_fn_attrs(def_id); + if let hir::ExprKind::Closure(closure) = expr.kind { + self.tcx.ensure().generics_of(closure.def_id); + self.tcx.ensure().codegen_fn_attrs(closure.def_id); // We do not call `type_of` for closures here as that // depends on typecheck and would therefore hide // any further errors in case one typeck fails. diff --git a/compiler/rustc_hir_analysis/src/collect/type_of.rs b/compiler/rustc_hir_analysis/src/collect/type_of.rs index 2402495c2e4..0bd459a1762 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of.rs @@ -636,9 +636,8 @@ fn find_opaque_ty_constraints_for_tait(tcx: TyCtxt<'_>, def_id: LocalDefId) -> T self.tcx.hir() } fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) { - if let hir::ExprKind::Closure { .. } = ex.kind { - let def_id = self.tcx.hir().local_def_id(ex.hir_id); - self.check(def_id); + if let hir::ExprKind::Closure(closure) = ex.kind { + self.check(closure.def_id); } intravisit::walk_expr(self, ex); } @@ -771,9 +770,8 @@ fn find_opaque_ty_constraints_for_rpit( self.tcx.hir() } fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) { - if let hir::ExprKind::Closure { .. } = ex.kind { - let def_id = self.tcx.hir().local_def_id(ex.hir_id); - self.check(def_id); + if let hir::ExprKind::Closure(closure) = ex.kind { + self.check(closure.def_id); } intravisit::walk_expr(self, ex); } diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index da27554a229..d4560490711 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -1481,6 +1481,7 @@ impl<'a> State<'a> { body, fn_decl_span: _, movability: _, + def_id: _, }) => { self.print_closure_binder(binder, bound_generic_params); self.print_capture_clause(capture_clause); diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 6a73e14e9f5..ed4b53c4abd 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1708,12 +1708,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } } - fn encode_info_for_closure(&mut self, hir_id: hir::HirId) { - let def_id = self.tcx.hir().local_def_id(hir_id); - debug!("EncodeContext::encode_info_for_closure({:?})", def_id); + #[instrument(level = "debug", skip(self))] + fn encode_info_for_closure(&mut self, def_id: LocalDefId) { // NOTE(eddyb) `tcx.type_of(def_id)` isn't used because it's fully generic, // including on the signature, which is inferred in `typeck. let typeck_result: &'tcx ty::TypeckResults<'tcx> = self.tcx.typeck(def_id); + let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); let ty = typeck_result.node_type(hir_id); match ty.kind() { ty::Generator(..) => { @@ -2115,8 +2115,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } fn encode_info_for_expr(&mut self, expr: &hir::Expr<'_>) { - if let hir::ExprKind::Closure { .. } = expr.kind { - self.encode_info_for_closure(expr.hir_id); + if let hir::ExprKind::Closure(closure) = expr.kind { + self.encode_info_for_closure(closure.def_id); } } } diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 14f50ae87de..7130a2a8809 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -1412,8 +1412,8 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> { } fn visit_expr(&mut self, ex: &'hir Expr<'hir>) { - if matches!(ex.kind, ExprKind::Closure { .. }) { - self.body_owners.push(self.tcx.hir().local_def_id(ex.hir_id)); + if let ExprKind::Closure(closure) = ex.kind { + self.body_owners.push(closure.def_id); } intravisit::walk_expr(self, ex) } diff --git a/compiler/rustc_passes/src/liveness.rs b/compiler/rustc_passes/src/liveness.rs index c181de48a9a..0100860afb9 100644 --- a/compiler/rustc_passes/src/liveness.rs +++ b/compiler/rustc_passes/src/liveness.rs @@ -413,7 +413,7 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> { } intravisit::walk_expr(self, expr); } - hir::ExprKind::Closure { .. } => { + hir::ExprKind::Closure(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)); @@ -423,8 +423,7 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> { // in better error messages than just pointing at the closure // construction site. let mut call_caps = Vec::new(); - let closure_def_id = self.tcx.hir().local_def_id(expr.hir_id); - if let Some(upvars) = self.tcx.upvars_mentioned(closure_def_id) { + if let Some(upvars) = self.tcx.upvars_mentioned(closure.def_id) { call_caps.extend(upvars.keys().map(|var_id| { let upvar = upvars[var_id]; let upvar_ln = self.add_live_node(UpvarNode(upvar.span)); diff --git a/compiler/rustc_passes/src/upvars.rs b/compiler/rustc_passes/src/upvars.rs index 68d9bf22bf9..9e41efce9ce 100644 --- a/compiler/rustc_passes/src/upvars.rs +++ b/compiler/rustc_passes/src/upvars.rs @@ -75,9 +75,8 @@ impl<'tcx> Visitor<'tcx> for CaptureCollector<'_, 'tcx> { } fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { - 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) { + if let hir::ExprKind::Closure(closure) = expr.kind { + if let Some(upvars) = self.tcx.upvars_mentioned(closure.def_id) { // Every capture of a closure expression is a local in scope, // that is moved/copied/borrowed into the closure value, and // for this analysis they are like any other access to a local. |
