about summary refs log tree commit diff
path: root/compiler/rustc_codegen_llvm/src/debuginfo
diff options
context:
space:
mode:
authorlrh2000 <lrh2000@pku.edu.cn>2021-05-05 23:50:44 +0800
committerlrh2000 <lrh2000@pku.edu.cn>2021-07-09 23:06:53 +0800
commit29856acffeb87541bb167c33f4fdb13c31ba6de0 (patch)
tree2bbb04127b4609350704b956985e38371cc62ca5 /compiler/rustc_codegen_llvm/src/debuginfo
parent95fb1315217976ff4c268bb03c9b4132f0dfa9fd (diff)
downloadrust-29856acffeb87541bb167c33f4fdb13c31ba6de0.tar.gz
rust-29856acffeb87541bb167c33f4fdb13c31ba6de0.zip
Name the captured upvars for closures/generators in debuginfo
Previously, debuggers print closures as something like
```
y::main::closure-0 (0x7fffffffdd34)
```
The pointer actually references to an upvar. It is not
very obvious, especially for beginners.

It's because upvars don't have names before, as they
are packed into a tuple. This commit names the upvars,
so we can expect to see something like
```
y::main::closure-0 {_captured_ref__b: 0x[...]}
```
Diffstat (limited to 'compiler/rustc_codegen_llvm/src/debuginfo')
-rw-r--r--compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs24
1 files changed, 23 insertions, 1 deletions
diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
index 7e136c1b24c..60c6bd61cd7 100644
--- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
+++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
@@ -1289,14 +1289,36 @@ struct TupleMemberDescriptionFactory<'tcx> {
 
 impl<'tcx> TupleMemberDescriptionFactory<'tcx> {
     fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec<MemberDescription<'ll>> {
+        // For closures and generators, name the captured upvars
+        // with the help of `CapturedPlace::to_mangled_name`.
+        let closure_def_id = match *self.ty.kind() {
+            ty::Generator(def_id, ..) => def_id.as_local(),
+            ty::Closure(def_id, ..) => def_id.as_local(),
+            _ => None,
+        };
+        let captures = match closure_def_id {
+            Some(local_def_id) => {
+                let typeck_results = cx.tcx.typeck(local_def_id);
+                let captures = typeck_results
+                    .closure_min_captures_flattened(local_def_id.to_def_id())
+                    .collect::<Vec<_>>();
+                Some(captures)
+            }
+            _ => None,
+        };
+
         let layout = cx.layout_of(self.ty);
         self.component_types
             .iter()
             .enumerate()
             .map(|(i, &component_type)| {
                 let (size, align) = cx.size_and_align_of(component_type);
+                let name = captures
+                    .as_ref()
+                    .map(|c| c[i].to_mangled_name(cx.tcx))
+                    .unwrap_or_else(|| format!("__{}", i));
                 MemberDescription {
-                    name: format!("__{}", i),
+                    name,
                     type_metadata: type_metadata(cx, component_type, self.span),
                     offset: layout.fields.offset(i),
                     size,