From cf5eda1b4d24d508039e96d939043268e955af6f Mon Sep 17 00:00:00 2001 From: lrh2000 Date: Fri, 9 Jul 2021 22:40:51 +0800 Subject: [PATCH] Add a query for `CapturedPlace::to_symbol` --- compiler/rustc_middle/src/dep_graph/dep_node.rs | 2 +- compiler/rustc_middle/src/query/mod.rs | 10 ++++++++++ compiler/rustc_middle/src/ty/closure.rs | 15 ++++++++++++++- compiler/rustc_middle/src/ty/mod.rs | 8 +++++++- compiler/rustc_mir_build/src/build/mod.rs | 13 +++++++------ 5 files changed, 39 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_middle/src/dep_graph/dep_node.rs b/compiler/rustc_middle/src/dep_graph/dep_node.rs index aa54d1ae7b9..8476929eaec 100644 --- a/compiler/rustc_middle/src/dep_graph/dep_node.rs +++ b/compiler/rustc_middle/src/dep_graph/dep_node.rs @@ -285,7 +285,7 @@ pub type DepNode = rustc_query_system::dep_graph::DepNode; // required that their size stay the same, but we don't want to change // it inadvertently. This assert just ensures we're aware of any change. #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] -static_assert_size!(DepNode, 17); +static_assert_size!(DepNode, 18); #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] static_assert_size!(DepNode, 24); diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 9a2f1149316..419bedaf2bb 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -329,6 +329,16 @@ rustc_queries! { } } + query symbols_for_closure_captures( + key: (LocalDefId, DefId) + ) -> Vec { + desc { + |tcx| "symbols for captures of closure `{}` in `{}`", + tcx.def_path_str(key.1), + tcx.def_path_str(key.0.to_def_id()) + } + } + /// MIR after our optimization passes have run. This is MIR that is ready /// for codegen. This is also the only query that can fetch non-local MIR, at present. query optimized_mir(key: DefId) -> &'tcx mir::Body<'tcx> { diff --git a/compiler/rustc_middle/src/ty/closure.rs b/compiler/rustc_middle/src/ty/closure.rs index 139846f6dc9..b8078c18fd9 100644 --- a/compiler/rustc_middle/src/ty/closure.rs +++ b/compiler/rustc_middle/src/ty/closure.rs @@ -162,7 +162,7 @@ impl CapturedPlace<'tcx> { } /// Returns a symbol of the captured upvar, which looks like `name__field1__field2`. - pub fn to_symbol(&self, tcx: TyCtxt<'tcx>) -> Symbol { + fn to_symbol(&self, tcx: TyCtxt<'tcx>) -> Symbol { let hir_id = match self.place.base { HirPlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id, base => bug!("Expected an upvar, found {:?}", base), @@ -248,6 +248,15 @@ impl CapturedPlace<'tcx> { } } +fn symbols_for_closure_captures<'tcx>( + tcx: TyCtxt<'tcx>, + def_id: (LocalDefId, DefId), +) -> Vec { + let typeck_results = tcx.typeck(def_id.0); + let captures = typeck_results.closure_min_captures_flattened(def_id.1); + captures.into_iter().map(|captured_place| captured_place.to_symbol(tcx)).collect() +} + /// Return true if the `proj_possible_ancestor` represents an ancestor path /// to `proj_capture` or `proj_possible_ancestor` is same as `proj_capture`, /// assuming they both start off of the same root variable. @@ -432,3 +441,7 @@ impl BorrowKind { } } } + +pub fn provide(providers: &mut ty::query::Providers) { + *providers = ty::query::Providers { symbols_for_closure_captures, ..*providers } +} diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 156e860e1a3..24cce81e78c 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -16,7 +16,6 @@ pub use self::IntVarValue::*; pub use self::Variance::*; pub use adt::*; pub use assoc::*; -pub use closure::*; pub use generics::*; pub use vtable::*; @@ -55,6 +54,12 @@ pub use rustc_type_ir::*; pub use self::binding::BindingMode; pub use self::binding::BindingMode::*; +pub use self::closure::{ + is_ancestor_or_same_capture, place_to_string_for_capture, BorrowKind, CaptureInfo, + CapturedPlace, ClosureKind, MinCaptureInformationMap, MinCaptureList, + RootVariableMinCaptureList, UpvarBorrow, UpvarCapture, UpvarCaptureMap, UpvarId, UpvarListMap, + UpvarPath, CAPTURE_STRUCT_LOCAL, +}; pub use self::consts::{Const, ConstInt, ConstKind, InferConst, ScalarInt, Unevaluated, ValTree}; pub use self::context::{ tls, CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, @@ -1979,6 +1984,7 @@ pub fn ast_uint_ty(uty: UintTy) -> ast::UintTy { } pub fn provide(providers: &mut ty::query::Providers) { + closure::provide(providers); context::provide(providers); erase_regions::provide(providers); layout::provide(providers); diff --git a/compiler/rustc_mir_build/src/build/mod.rs b/compiler/rustc_mir_build/src/build/mod.rs index e13dcadeb56..86a9e47dc53 100644 --- a/compiler/rustc_mir_build/src/build/mod.rs +++ b/compiler/rustc_mir_build/src/build/mod.rs @@ -959,13 +959,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { ty::Generator(_, substs, _) => ty::UpvarSubsts::Generator(substs), _ => span_bug!(self.fn_span, "upvars with non-closure env ty {:?}", closure_ty), }; + let def_id = self.def_id.as_local().unwrap(); + let capture_syms = tcx.symbols_for_closure_captures((def_id, fn_def_id)); let capture_tys = upvar_substs.upvar_tys(); - let captures_with_tys = - hir_typeck_results.closure_min_captures_flattened(fn_def_id).zip(capture_tys); + let captures_with_tys = hir_typeck_results + .closure_min_captures_flattened(fn_def_id) + .zip(capture_tys.zip(capture_syms)); self.upvar_mutbls = captures_with_tys .enumerate() - .map(|(i, (captured_place, ty))| { + .map(|(i, (captured_place, (ty, sym)))| { let capture = captured_place.info.capture_kind; let var_id = match captured_place.place.base { HirPlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id, @@ -974,8 +977,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let mutability = captured_place.mutability; - let name = captured_place.to_symbol(tcx); - let mut projs = closure_env_projs.clone(); projs.push(ProjectionElem::Field(Field::new(i), ty)); match capture { @@ -986,7 +987,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { }; self.var_debug_info.push(VarDebugInfo { - name, + name: sym, source_info: SourceInfo::outermost(tcx_hir.span(var_id)), value: VarDebugInfoContents::Place(Place { local: ty::CAPTURE_STRUCT_LOCAL,