From 29856acffeb87541bb167c33f4fdb13c31ba6de0 Mon Sep 17 00:00:00 2001 From: lrh2000 Date: Wed, 5 May 2021 23:50:44 +0800 Subject: [PATCH 1/4] 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[...]} ``` --- .../src/debuginfo/metadata.rs | 24 ++++- compiler/rustc_middle/src/ty/closure.rs | 48 ++++++++++ src/test/debuginfo/captured-fields.rs | 87 +++++++++++++++++++ src/test/debuginfo/generator-objects.rs | 8 +- src/test/debuginfo/issue-57822.rs | 6 +- 5 files changed, 165 insertions(+), 8 deletions(-) create mode 100644 src/test/debuginfo/captured-fields.rs diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index 7e136c1b24cb2..60c6bd61cd73b 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, diff --git a/compiler/rustc_middle/src/ty/closure.rs b/compiler/rustc_middle/src/ty/closure.rs index 0706a057dd0c6..0d4c635050727 100644 --- a/compiler/rustc_middle/src/ty/closure.rs +++ b/compiler/rustc_middle/src/ty/closure.rs @@ -159,6 +159,54 @@ impl CapturedPlace<'tcx> { place_to_string_for_capture(tcx, &self.place) } + /// Returns mangled names of captured upvars. Here are some examples: + /// - `_captured_val__name__field` + /// - `_captured_ref__name__field` + /// + /// The purpose is to use those names in debuginfo. They should be human-understandable. + /// Without the names, the end users may get confused when the debuggers just print some + /// pointers in closures or generators. + pub fn to_mangled_name(&self, tcx: TyCtxt<'tcx>) -> String { + let prefix = match self.info.capture_kind { + ty::UpvarCapture::ByValue(_) => "_captured_val__", + ty::UpvarCapture::ByRef(_) => "_captured_ref__", + }; + + let hir_id = match self.place.base { + HirPlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id, + base => bug!("Expected an upvar, found {:?}", base), + }; + let name = tcx.hir().name(hir_id); + + let mut ty = self.place.base_ty; + let mut fields = String::new(); + for proj in self.place.projections.iter() { + match proj.kind { + HirProjectionKind::Field(idx, variant) => match ty.kind() { + ty::Tuple(_) => fields = format!("{}__{}", fields, idx), + ty::Adt(def, ..) => { + fields = format!( + "{}__{}", + fields, + def.variants[variant].fields[idx as usize].ident.name.as_str(), + ); + } + ty => { + bug!("Unexpected type {:?} for `Field` projection", ty) + } + }, + + // Ignore derefs for now, as they are likely caused by + // autoderefs that don't appear in the original code. + HirProjectionKind::Deref => {} + proj => bug!("Unexpected projection {:?} in captured place", proj), + } + ty = proj.ty; + } + + prefix.to_owned() + &name.to_string() + &fields + } + /// Returns the hir-id of the root variable for the captured place. /// e.g., if `a.b.c` was captured, would return the hir-id for `a`. pub fn get_root_variable(&self) -> hir::HirId { diff --git a/src/test/debuginfo/captured-fields.rs b/src/test/debuginfo/captured-fields.rs new file mode 100644 index 0000000000000..5489fa1472060 --- /dev/null +++ b/src/test/debuginfo/captured-fields.rs @@ -0,0 +1,87 @@ +// compile-flags:-g + +// === GDB TESTS =================================================================================== + +// gdb-command:run +// gdb-command:print test +// gdbr-check:$1 = captured_fields::main::{closure#0} {_captured_ref__my_ref__my_field1: 0x[...]} +// gdb-command:continue +// gdb-command:print test +// gdbr-check:$2 = captured_fields::main::{closure#1} {_captured_ref__my_ref__my_field2: 0x[...]} +// gdb-command:continue +// gdb-command:print test +// gdbr-check:$3 = captured_fields::main::{closure#2} {_captured_ref__my_ref: 0x[...]} +// gdb-command:continue +// gdb-command:print test +// gdbr-check:$4 = captured_fields::main::{closure#3} {_captured_val__my_ref: 0x[...]} +// gdb-command:continue +// gdb-command:print test +// gdbr-check:$5 = captured_fields::main::{closure#4} {_captured_val__my_var: captured_fields::MyStruct {my_field1: 11, my_field2: 22}} +// gdb-command:continue + +// === LLDB TESTS ================================================================================== + +// lldb-command:run +// lldb-command:print test +// lldbg-check:(captured_fields::main::{closure#0}) $0 = { _captured_ref__my_ref__my_field1 = 0x[...] } +// lldb-command:continue +// lldb-command:print test +// lldbg-check:(captured_fields::main::{closure#1}) $1 = { _captured_ref__my_ref__my_field2 = 0x[...] } +// lldb-command:continue +// lldb-command:print test +// lldbg-check:(captured_fields::main::{closure#2}) $2 = { _captured_ref__my_ref = 0x[...] } +// lldb-command:continue +// lldb-command:print test +// lldbg-check:(captured_fields::main::{closure#3}) $3 = { _captured_val__my_ref = 0x[...] } +// lldb-command:continue +// lldb-command:print test +// lldbg-check:(captured_fields::main::{closure#4}) $4 = { _captured_val__my_var = { my_field1 = 11 my_field2 = 22 } } +// lldb-command:continue + +#![feature(capture_disjoint_fields)] +#![allow(unused)] + +struct MyStruct { + my_field1: u32, + my_field2: u32, +} + +fn main() { + let mut my_var = MyStruct { + my_field1: 11, + my_field2: 22, + }; + let my_ref = &mut my_var; + + let test = || { + let a = &mut my_ref.my_field1; + }; + + _zzz(); // #break + + let test = || { + let a = &my_ref.my_field2; + }; + + _zzz(); // #break + + let test = || { + let a = &my_ref; + }; + + _zzz(); // #break + + let test = || { + let a = my_ref; + }; + + _zzz(); // #break + + let test = || { + let a = my_var; + }; + + _zzz(); // #break +} + +fn _zzz() {} diff --git a/src/test/debuginfo/generator-objects.rs b/src/test/debuginfo/generator-objects.rs index 7ac3304aa9634..46a3d7924a1de 100644 --- a/src/test/debuginfo/generator-objects.rs +++ b/src/test/debuginfo/generator-objects.rs @@ -11,16 +11,16 @@ // gdb-command:run // gdb-command:print b -// gdb-check:$1 = generator_objects::main::{generator#0}::Unresumed(0x[...]) +// gdb-check:$1 = generator_objects::main::{generator#0}::Unresumed{_captured_ref__a: 0x[...]} // gdb-command:continue // gdb-command:print b -// gdb-check:$2 = generator_objects::main::{generator#0}::Suspend0{c: 6, d: 7, __0: 0x[...]} +// gdb-check:$2 = generator_objects::main::{generator#0}::Suspend0{c: 6, d: 7, _captured_ref__a: 0x[...]} // gdb-command:continue // gdb-command:print b -// gdb-check:$3 = generator_objects::main::{generator#0}::Suspend1{c: 7, d: 8, __0: 0x[...]} +// gdb-check:$3 = generator_objects::main::{generator#0}::Suspend1{c: 7, d: 8, _captured_ref__a: 0x[...]} // gdb-command:continue // gdb-command:print b -// gdb-check:$4 = generator_objects::main::{generator#0}::Returned(0x[...]) +// gdb-check:$4 = generator_objects::main::{generator#0}::Returned{_captured_ref__a: 0x[...]} // === LLDB TESTS ================================================================================== diff --git a/src/test/debuginfo/issue-57822.rs b/src/test/debuginfo/issue-57822.rs index f6d2146fe11fa..22d55ae989fb1 100644 --- a/src/test/debuginfo/issue-57822.rs +++ b/src/test/debuginfo/issue-57822.rs @@ -11,17 +11,17 @@ // gdb-command:run // gdb-command:print g -// gdb-check:$1 = issue_57822::main::{closure#1} (issue_57822::main::{closure#0} (1)) +// gdb-check:$1 = issue_57822::main::{closure#1} {_captured_val__f: issue_57822::main::{closure#0} {_captured_val__x: 1}} // gdb-command:print b -// gdb-check:$2 = issue_57822::main::{generator#3}::Unresumed(issue_57822::main::{generator#2}::Unresumed(2)) +// gdb-check:$2 = issue_57822::main::{generator#3}::Unresumed{_captured_val__a: issue_57822::main::{generator#2}::Unresumed{_captured_val__y: 2}} // === LLDB TESTS ================================================================================== // lldb-command:run // lldb-command:print g -// lldbg-check:(issue_57822::main::{closure#1}) $0 = { 0 = { 0 = 1 } } +// lldbg-check:(issue_57822::main::{closure#1}) $0 = { _captured_val__f = { _captured_val__x = 1 } } // lldb-command:print b // lldbg-check:(issue_57822::main::{generator#3}) $1 = From cda90f55419ce449f3a9db327465d9b2ae7dfce9 Mon Sep 17 00:00:00 2001 From: lrh2000 Date: Sat, 15 May 2021 19:01:13 +0800 Subject: [PATCH 2/4] Store names of captured variables in `optimized_mir` - Closures in external crates may get compiled in because of monomorphization. We should store names of captured variables in `optimized_mir`, so that they are written into the metadata file and we can use them to generate debuginfo. - If there are breakpoints inside closures, the names of captured variables stored in `optimized_mir` can be used to print them. Now the name is more precise when disjoint fields are captured. --- .../src/debuginfo/metadata.rs | 52 +++++++++++------- compiler/rustc_middle/src/ty/closure.rs | 35 +++++------- compiler/rustc_mir_build/src/build/mod.rs | 10 +--- ...aptured-fields.rs => captured-fields-1.rs} | 32 +++++++---- src/test/debuginfo/captured-fields-2.rs | 55 +++++++++++++++++++ src/test/debuginfo/generator-objects.rs | 8 +-- src/test/debuginfo/issue-57822.rs | 6 +- 7 files changed, 132 insertions(+), 66 deletions(-) rename src/test/debuginfo/{captured-fields.rs => captured-fields-1.rs} (52%) create mode 100644 src/test/debuginfo/captured-fields-2.rs diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index 60c6bd61cd73b..bc950778bcc57 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -1280,6 +1280,31 @@ fn prepare_struct_metadata( // Tuples //=----------------------------------------------------------------------------- +/// Returns names of captured upvars for closures and generators. +/// +/// Here are some examples: +/// - `name__field1__field2` when the upvar is captured by value. +/// - `_ref__name__field` when the upvar is captured by reference. +fn closure_saved_names_of_captured_variables(tcx: TyCtxt<'tcx>, def_id: DefId) -> Vec { + let body = tcx.optimized_mir(def_id); + + body.var_debug_info + .iter() + .filter_map(|var| { + let is_ref = match var.value { + mir::VarDebugInfoContents::Place(place) if place.local == mir::Local::new(1) => { + // The projection is either `[.., Field, Deref]` or `[.., Field]`. It + // implies whether the variable is captured by value or by reference. + matches!(place.projection.last().unwrap(), mir::ProjectionElem::Deref) + } + _ => return None, + }; + let prefix = if is_ref { "_ref__" } else { "" }; + Some(prefix.to_owned() + &var.name.as_str()) + }) + .collect::>() +} + /// Creates `MemberDescription`s for the fields of a tuple. struct TupleMemberDescriptionFactory<'tcx> { ty: Ty<'tcx>, @@ -1289,34 +1314,23 @@ 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) + let capture_names = match *self.ty.kind() { + ty::Generator(def_id, ..) | ty::Closure(def_id, ..) => { + Some(closure_saved_names_of_captured_variables(cx.tcx, def_id)) } _ => 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)); + let name = if let Some(names) = capture_names.as_ref() { + names[i].clone() + } else { + format!("__{}", i) + }; MemberDescription { name, type_metadata: type_metadata(cx, component_type, self.span), diff --git a/compiler/rustc_middle/src/ty/closure.rs b/compiler/rustc_middle/src/ty/closure.rs index 0d4c635050727..139846f6dc974 100644 --- a/compiler/rustc_middle/src/ty/closure.rs +++ b/compiler/rustc_middle/src/ty/closure.rs @@ -3,10 +3,12 @@ use crate::hir::place::{ }; use crate::{mir, ty}; +use std::fmt::Write; + use rustc_data_structures::fx::{FxHashMap, FxIndexMap}; use rustc_hir as hir; use rustc_hir::def_id::{DefId, LocalDefId}; -use rustc_span::Span; +use rustc_span::{Span, Symbol}; use super::{Ty, TyCtxt}; @@ -159,37 +161,26 @@ impl CapturedPlace<'tcx> { place_to_string_for_capture(tcx, &self.place) } - /// Returns mangled names of captured upvars. Here are some examples: - /// - `_captured_val__name__field` - /// - `_captured_ref__name__field` - /// - /// The purpose is to use those names in debuginfo. They should be human-understandable. - /// Without the names, the end users may get confused when the debuggers just print some - /// pointers in closures or generators. - pub fn to_mangled_name(&self, tcx: TyCtxt<'tcx>) -> String { - let prefix = match self.info.capture_kind { - ty::UpvarCapture::ByValue(_) => "_captured_val__", - ty::UpvarCapture::ByRef(_) => "_captured_ref__", - }; - + /// Returns a symbol of the captured upvar, which looks like `name__field1__field2`. + pub 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), }; - let name = tcx.hir().name(hir_id); + let mut symbol = tcx.hir().name(hir_id).as_str().to_string(); let mut ty = self.place.base_ty; - let mut fields = String::new(); for proj in self.place.projections.iter() { match proj.kind { HirProjectionKind::Field(idx, variant) => match ty.kind() { - ty::Tuple(_) => fields = format!("{}__{}", fields, idx), + ty::Tuple(_) => write!(&mut symbol, "__{}", idx).unwrap(), ty::Adt(def, ..) => { - fields = format!( - "{}__{}", - fields, + write!( + &mut symbol, + "__{}", def.variants[variant].fields[idx as usize].ident.name.as_str(), - ); + ) + .unwrap(); } ty => { bug!("Unexpected type {:?} for `Field` projection", ty) @@ -204,7 +195,7 @@ impl CapturedPlace<'tcx> { ty = proj.ty; } - prefix.to_owned() + &name.to_string() + &fields + Symbol::intern(&symbol) } /// Returns the hir-id of the root variable for the captured place. diff --git a/compiler/rustc_mir_build/src/build/mod.rs b/compiler/rustc_mir_build/src/build/mod.rs index f5f6da3ec0bba..e13dcadeb56a9 100644 --- a/compiler/rustc_mir_build/src/build/mod.rs +++ b/compiler/rustc_mir_build/src/build/mod.rs @@ -16,7 +16,7 @@ use rustc_middle::mir::*; use rustc_middle::thir::{BindingMode, Expr, ExprId, LintLevel, PatKind, Thir}; use rustc_middle::ty::subst::Subst; use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeckResults}; -use rustc_span::symbol::{kw, sym}; +use rustc_span::symbol::sym; use rustc_span::Span; use rustc_target::spec::abi::Abi; use rustc_target::spec::PanicStrategy; @@ -974,13 +974,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let mutability = captured_place.mutability; - // FIXME(project-rfc-2229#8): Store more precise information - let mut name = kw::Empty; - if let Some(Node::Binding(pat)) = tcx_hir.find(var_id) { - if let hir::PatKind::Binding(_, _, ident, _) = pat.kind { - name = ident.name; - } - } + let name = captured_place.to_symbol(tcx); let mut projs = closure_env_projs.clone(); projs.push(ProjectionElem::Field(Field::new(i), ty)); diff --git a/src/test/debuginfo/captured-fields.rs b/src/test/debuginfo/captured-fields-1.rs similarity index 52% rename from src/test/debuginfo/captured-fields.rs rename to src/test/debuginfo/captured-fields-1.rs index 5489fa1472060..65f9e5f532279 100644 --- a/src/test/debuginfo/captured-fields.rs +++ b/src/test/debuginfo/captured-fields-1.rs @@ -4,38 +4,44 @@ // gdb-command:run // gdb-command:print test -// gdbr-check:$1 = captured_fields::main::{closure#0} {_captured_ref__my_ref__my_field1: 0x[...]} +// gdbr-check:$1 = captured_fields_1::main::{closure#0} {_ref__my_ref__my_field1: 0x[...]} // gdb-command:continue // gdb-command:print test -// gdbr-check:$2 = captured_fields::main::{closure#1} {_captured_ref__my_ref__my_field2: 0x[...]} +// gdbr-check:$2 = captured_fields_1::main::{closure#1} {_ref__my_ref__my_field2: 0x[...]} // gdb-command:continue // gdb-command:print test -// gdbr-check:$3 = captured_fields::main::{closure#2} {_captured_ref__my_ref: 0x[...]} +// gdbr-check:$3 = captured_fields_1::main::{closure#2} {_ref__my_ref: 0x[...]} // gdb-command:continue // gdb-command:print test -// gdbr-check:$4 = captured_fields::main::{closure#3} {_captured_val__my_ref: 0x[...]} +// gdbr-check:$4 = captured_fields_1::main::{closure#3} {my_ref: 0x[...]} // gdb-command:continue // gdb-command:print test -// gdbr-check:$5 = captured_fields::main::{closure#4} {_captured_val__my_var: captured_fields::MyStruct {my_field1: 11, my_field2: 22}} +// gdbr-check:$5 = captured_fields_1::main::{closure#4} {my_var__my_field2: 22} +// gdb-command:continue +// gdb-command:print test +// gdbr-check:$6 = captured_fields_1::main::{closure#5} {my_var: captured_fields_1::MyStruct {my_field1: 11, my_field2: 22}} // gdb-command:continue // === LLDB TESTS ================================================================================== // lldb-command:run // lldb-command:print test -// lldbg-check:(captured_fields::main::{closure#0}) $0 = { _captured_ref__my_ref__my_field1 = 0x[...] } +// lldbg-check:(captured_fields_1::main::{closure#0}) $0 = { _ref__my_ref__my_field1 = 0x[...] } +// lldb-command:continue +// lldb-command:print test +// lldbg-check:(captured_fields_1::main::{closure#1}) $1 = { _ref__my_ref__my_field2 = 0x[...] } // lldb-command:continue // lldb-command:print test -// lldbg-check:(captured_fields::main::{closure#1}) $1 = { _captured_ref__my_ref__my_field2 = 0x[...] } +// lldbg-check:(captured_fields_1::main::{closure#2}) $2 = { _ref__my_ref = 0x[...] } // lldb-command:continue // lldb-command:print test -// lldbg-check:(captured_fields::main::{closure#2}) $2 = { _captured_ref__my_ref = 0x[...] } +// lldbg-check:(captured_fields_1::main::{closure#3}) $3 = { my_ref = 0x[...] } // lldb-command:continue // lldb-command:print test -// lldbg-check:(captured_fields::main::{closure#3}) $3 = { _captured_val__my_ref = 0x[...] } +// lldbg-check:(captured_fields_1::main::{closure#4}) $4 = { my_var__my_field2 = 22 } // lldb-command:continue // lldb-command:print test -// lldbg-check:(captured_fields::main::{closure#4}) $4 = { _captured_val__my_var = { my_field1 = 11 my_field2 = 22 } } +// lldbg-check:(captured_fields_1::main::{closure#5}) $5 = { my_var = { my_field1 = 11 my_field2 = 22 } } // lldb-command:continue #![feature(capture_disjoint_fields)] @@ -77,6 +83,12 @@ fn main() { _zzz(); // #break + let test = move || { + let a = my_var.my_field2; + }; + + _zzz(); // #break + let test = || { let a = my_var; }; diff --git a/src/test/debuginfo/captured-fields-2.rs b/src/test/debuginfo/captured-fields-2.rs new file mode 100644 index 0000000000000..c872354a92489 --- /dev/null +++ b/src/test/debuginfo/captured-fields-2.rs @@ -0,0 +1,55 @@ +// compile-flags:-g + +// === GDB TESTS =================================================================================== + +// gdb-command:run +// gdb-command:print my_ref__my_field1 +// gdbr-check:$1 = 11 +// gdb-command:continue +// gdb-command:print my_var__my_field2 +// gdbr-check:$2 = 22 +// gdb-command:continue + +// === LLDB TESTS ================================================================================== + +// lldb-command:run +// lldb-command:print my_ref__my_field1 +// lldbg-check:(unsigned int) $0 = 11 +// lldb-command:continue +// lldb-command:print my_var__my_field2 +// lldbg-check:(unsigned int) $1 = 22 +// lldb-command:continue + +#![feature(capture_disjoint_fields)] +#![allow(unused)] + +struct MyStruct { + my_field1: u32, + my_field2: u32, +} + +fn main() { + let mut my_var = MyStruct { + my_field1: 11, + my_field2: 22, + }; + let my_ref = &mut my_var; + + let test = || { + let a = my_ref.my_field1; + + _zzz(); // #break + }; + + test(); + + let test = move || { + let a = my_var.my_field2; + + _zzz(); // #break + }; + + test(); +} + +fn _zzz() {} diff --git a/src/test/debuginfo/generator-objects.rs b/src/test/debuginfo/generator-objects.rs index 46a3d7924a1de..9bf33a7bb8796 100644 --- a/src/test/debuginfo/generator-objects.rs +++ b/src/test/debuginfo/generator-objects.rs @@ -11,16 +11,16 @@ // gdb-command:run // gdb-command:print b -// gdb-check:$1 = generator_objects::main::{generator#0}::Unresumed{_captured_ref__a: 0x[...]} +// gdb-check:$1 = generator_objects::main::{generator#0}::Unresumed{_ref__a: 0x[...]} // gdb-command:continue // gdb-command:print b -// gdb-check:$2 = generator_objects::main::{generator#0}::Suspend0{c: 6, d: 7, _captured_ref__a: 0x[...]} +// gdb-check:$2 = generator_objects::main::{generator#0}::Suspend0{c: 6, d: 7, _ref__a: 0x[...]} // gdb-command:continue // gdb-command:print b -// gdb-check:$3 = generator_objects::main::{generator#0}::Suspend1{c: 7, d: 8, _captured_ref__a: 0x[...]} +// gdb-check:$3 = generator_objects::main::{generator#0}::Suspend1{c: 7, d: 8, _ref__a: 0x[...]} // gdb-command:continue // gdb-command:print b -// gdb-check:$4 = generator_objects::main::{generator#0}::Returned{_captured_ref__a: 0x[...]} +// gdb-check:$4 = generator_objects::main::{generator#0}::Returned{_ref__a: 0x[...]} // === LLDB TESTS ================================================================================== diff --git a/src/test/debuginfo/issue-57822.rs b/src/test/debuginfo/issue-57822.rs index 22d55ae989fb1..1a26b0a3255b7 100644 --- a/src/test/debuginfo/issue-57822.rs +++ b/src/test/debuginfo/issue-57822.rs @@ -11,17 +11,17 @@ // gdb-command:run // gdb-command:print g -// gdb-check:$1 = issue_57822::main::{closure#1} {_captured_val__f: issue_57822::main::{closure#0} {_captured_val__x: 1}} +// gdb-check:$1 = issue_57822::main::{closure#1} {f: issue_57822::main::{closure#0} {x: 1}} // gdb-command:print b -// gdb-check:$2 = issue_57822::main::{generator#3}::Unresumed{_captured_val__a: issue_57822::main::{generator#2}::Unresumed{_captured_val__y: 2}} +// gdb-check:$2 = issue_57822::main::{generator#3}::Unresumed{a: issue_57822::main::{generator#2}::Unresumed{y: 2}} // === LLDB TESTS ================================================================================== // lldb-command:run // lldb-command:print g -// lldbg-check:(issue_57822::main::{closure#1}) $0 = { _captured_val__f = { _captured_val__x = 1 } } +// lldbg-check:(issue_57822::main::{closure#1}) $0 = { f = { x = 1 } } // lldb-command:print b // lldbg-check:(issue_57822::main::{generator#3}) $1 = From 0cb6f07ef2690b9ad5941c33a0928bf72788829d Mon Sep 17 00:00:00 2001 From: lrh2000 Date: Fri, 9 Jul 2021 21:00:51 +0800 Subject: [PATCH 3/4] Avoid unnecessary `String::clone` --- compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index bc950778bcc57..2cb126f1a7e4d 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -1314,9 +1314,9 @@ struct TupleMemberDescriptionFactory<'tcx> { impl<'tcx> TupleMemberDescriptionFactory<'tcx> { fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec> { - let capture_names = match *self.ty.kind() { + let mut capture_names = match *self.ty.kind() { ty::Generator(def_id, ..) | ty::Closure(def_id, ..) => { - Some(closure_saved_names_of_captured_variables(cx.tcx, def_id)) + Some(closure_saved_names_of_captured_variables(cx.tcx, def_id).into_iter()) } _ => None, }; @@ -1326,8 +1326,8 @@ impl<'tcx> TupleMemberDescriptionFactory<'tcx> { .enumerate() .map(|(i, &component_type)| { let (size, align) = cx.size_and_align_of(component_type); - let name = if let Some(names) = capture_names.as_ref() { - names[i].clone() + let name = if let Some(names) = capture_names.as_mut() { + names.next().unwrap() } else { format!("__{}", i) }; From cf5eda1b4d24d508039e96d939043268e955af6f Mon Sep 17 00:00:00 2001 From: lrh2000 Date: Fri, 9 Jul 2021 22:40:51 +0800 Subject: [PATCH 4/4] 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 aa54d1ae7b9d1..8476929eaeced 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 9a2f1149316e2..419bedaf2bb60 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 139846f6dc974..b8078c18fd934 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 156e860e1a3fb..24cce81e78c68 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 e13dcadeb56a9..86a9e47dc53c6 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,