From 29856acffeb87541bb167c33f4fdb13c31ba6de0 Mon Sep 17 00:00:00 2001 From: lrh2000 Date: Wed, 5 May 2021 23:50:44 +0800 Subject: 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[...]} ``` --- .../rustc_codegen_llvm/src/debuginfo/metadata.rs | 24 +++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'compiler/rustc_codegen_llvm/src/debuginfo') 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> { + // 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::>(); + 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, -- cgit 1.4.1-3-g733a5