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[...]}
```
This commit is contained in:
lrh2000 2021-05-05 23:50:44 +08:00
parent 95fb131521
commit 29856acffe
5 changed files with 165 additions and 8 deletions

View file

@ -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,