]> git.lizzy.rs Git - rust.git/commitdiff
Add a query for `CapturedPlace::to_symbol`
authorlrh2000 <lrh2000@pku.edu.cn>
Fri, 9 Jul 2021 14:40:51 +0000 (22:40 +0800)
committerlrh2000 <lrh2000@pku.edu.cn>
Fri, 9 Jul 2021 16:00:25 +0000 (00:00 +0800)
compiler/rustc_middle/src/dep_graph/dep_node.rs
compiler/rustc_middle/src/query/mod.rs
compiler/rustc_middle/src/ty/closure.rs
compiler/rustc_middle/src/ty/mod.rs
compiler/rustc_mir_build/src/build/mod.rs

index aa54d1ae7b9d118426d9702dac6384725e0b50c4..8476929eaeced2b1d230e5e6f6d964e2e7d2a088 100644 (file)
@@ -285,7 +285,7 @@ pub mod label_strs {
 // 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);
index 9a2f1149316e26e0a547aca9334c82aeb1a08cf2..419bedaf2bb60de0895fb415dc6d8a2dee98f901 100644 (file)
         }
     }
 
+    query symbols_for_closure_captures(
+        key: (LocalDefId, DefId)
+    ) -> Vec<rustc_span::Symbol> {
+        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> {
index 139846f6dc9740f20ec352613115914e3d4d5617..b8078c18fd9346aa91473bbe415bad501a6f5016 100644 (file)
@@ -162,7 +162,7 @@ pub fn to_string(&self, tcx: TyCtxt<'tcx>) -> String {
     }
 
     /// 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 @@ pub fn get_capture_kind_span(&self, tcx: TyCtxt<'tcx>) -> Span {
     }
 }
 
+fn symbols_for_closure_captures<'tcx>(
+    tcx: TyCtxt<'tcx>,
+    def_id: (LocalDefId, DefId),
+) -> Vec<Symbol> {
+    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 @@ pub fn to_mutbl_lossy(self) -> hir::Mutability {
         }
     }
 }
+
+pub fn provide(providers: &mut ty::query::Providers) {
+    *providers = ty::query::Providers { symbols_for_closure_captures, ..*providers }
+}
index 156e860e1a3fbc0ab1648f8384ed5cd0ef02a78b..24cce81e78c68e1e40a3bc2ee4de0f364a7d077b 100644 (file)
@@ -16,7 +16,6 @@
 pub use self::Variance::*;
 pub use adt::*;
 pub use assoc::*;
-pub use closure::*;
 pub use generics::*;
 pub use vtable::*;
 
 
 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);
index e13dcadeb56a91f717508d124092c29f74495ad8..86a9e47dc53c6f801ee7481ed96187b800fcc87b 100644 (file)
@@ -959,13 +959,16 @@ fn args_and_body(
                 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 @@ fn args_and_body(
 
                     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 @@ fn args_and_body(
                     };
 
                     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,