Skip to content

Commit cda90f5

Browse files
committed
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.
1 parent 29856ac commit cda90f5

File tree

7 files changed

+132
-66
lines changed

7 files changed

+132
-66
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+33-19
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,31 @@ fn prepare_struct_metadata(
12801280
// Tuples
12811281
//=-----------------------------------------------------------------------------
12821282

1283+
/// Returns names of captured upvars for closures and generators.
1284+
///
1285+
/// Here are some examples:
1286+
/// - `name__field1__field2` when the upvar is captured by value.
1287+
/// - `_ref__name__field` when the upvar is captured by reference.
1288+
fn closure_saved_names_of_captured_variables(tcx: TyCtxt<'tcx>, def_id: DefId) -> Vec<String> {
1289+
let body = tcx.optimized_mir(def_id);
1290+
1291+
body.var_debug_info
1292+
.iter()
1293+
.filter_map(|var| {
1294+
let is_ref = match var.value {
1295+
mir::VarDebugInfoContents::Place(place) if place.local == mir::Local::new(1) => {
1296+
// The projection is either `[.., Field, Deref]` or `[.., Field]`. It
1297+
// implies whether the variable is captured by value or by reference.
1298+
matches!(place.projection.last().unwrap(), mir::ProjectionElem::Deref)
1299+
}
1300+
_ => return None,
1301+
};
1302+
let prefix = if is_ref { "_ref__" } else { "" };
1303+
Some(prefix.to_owned() + &var.name.as_str())
1304+
})
1305+
.collect::<Vec<_>>()
1306+
}
1307+
12831308
/// Creates `MemberDescription`s for the fields of a tuple.
12841309
struct TupleMemberDescriptionFactory<'tcx> {
12851310
ty: Ty<'tcx>,
@@ -1289,34 +1314,23 @@ struct TupleMemberDescriptionFactory<'tcx> {
12891314

12901315
impl<'tcx> TupleMemberDescriptionFactory<'tcx> {
12911316
fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec<MemberDescription<'ll>> {
1292-
// For closures and generators, name the captured upvars
1293-
// with the help of `CapturedPlace::to_mangled_name`.
1294-
let closure_def_id = match *self.ty.kind() {
1295-
ty::Generator(def_id, ..) => def_id.as_local(),
1296-
ty::Closure(def_id, ..) => def_id.as_local(),
1297-
_ => None,
1298-
};
1299-
let captures = match closure_def_id {
1300-
Some(local_def_id) => {
1301-
let typeck_results = cx.tcx.typeck(local_def_id);
1302-
let captures = typeck_results
1303-
.closure_min_captures_flattened(local_def_id.to_def_id())
1304-
.collect::<Vec<_>>();
1305-
Some(captures)
1317+
let capture_names = match *self.ty.kind() {
1318+
ty::Generator(def_id, ..) | ty::Closure(def_id, ..) => {
1319+
Some(closure_saved_names_of_captured_variables(cx.tcx, def_id))
13061320
}
13071321
_ => None,
13081322
};
1309-
13101323
let layout = cx.layout_of(self.ty);
13111324
self.component_types
13121325
.iter()
13131326
.enumerate()
13141327
.map(|(i, &component_type)| {
13151328
let (size, align) = cx.size_and_align_of(component_type);
1316-
let name = captures
1317-
.as_ref()
1318-
.map(|c| c[i].to_mangled_name(cx.tcx))
1319-
.unwrap_or_else(|| format!("__{}", i));
1329+
let name = if let Some(names) = capture_names.as_ref() {
1330+
names[i].clone()
1331+
} else {
1332+
format!("__{}", i)
1333+
};
13201334
MemberDescription {
13211335
name,
13221336
type_metadata: type_metadata(cx, component_type, self.span),

compiler/rustc_middle/src/ty/closure.rs

+13-22
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ use crate::hir::place::{
33
};
44
use crate::{mir, ty};
55

6+
use std::fmt::Write;
7+
68
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
79
use rustc_hir as hir;
810
use rustc_hir::def_id::{DefId, LocalDefId};
9-
use rustc_span::Span;
11+
use rustc_span::{Span, Symbol};
1012

1113
use super::{Ty, TyCtxt};
1214

@@ -159,37 +161,26 @@ impl CapturedPlace<'tcx> {
159161
place_to_string_for_capture(tcx, &self.place)
160162
}
161163

162-
/// Returns mangled names of captured upvars. Here are some examples:
163-
/// - `_captured_val__name__field`
164-
/// - `_captured_ref__name__field`
165-
///
166-
/// The purpose is to use those names in debuginfo. They should be human-understandable.
167-
/// Without the names, the end users may get confused when the debuggers just print some
168-
/// pointers in closures or generators.
169-
pub fn to_mangled_name(&self, tcx: TyCtxt<'tcx>) -> String {
170-
let prefix = match self.info.capture_kind {
171-
ty::UpvarCapture::ByValue(_) => "_captured_val__",
172-
ty::UpvarCapture::ByRef(_) => "_captured_ref__",
173-
};
174-
164+
/// Returns a symbol of the captured upvar, which looks like `name__field1__field2`.
165+
pub fn to_symbol(&self, tcx: TyCtxt<'tcx>) -> Symbol {
175166
let hir_id = match self.place.base {
176167
HirPlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id,
177168
base => bug!("Expected an upvar, found {:?}", base),
178169
};
179-
let name = tcx.hir().name(hir_id);
170+
let mut symbol = tcx.hir().name(hir_id).as_str().to_string();
180171

181172
let mut ty = self.place.base_ty;
182-
let mut fields = String::new();
183173
for proj in self.place.projections.iter() {
184174
match proj.kind {
185175
HirProjectionKind::Field(idx, variant) => match ty.kind() {
186-
ty::Tuple(_) => fields = format!("{}__{}", fields, idx),
176+
ty::Tuple(_) => write!(&mut symbol, "__{}", idx).unwrap(),
187177
ty::Adt(def, ..) => {
188-
fields = format!(
189-
"{}__{}",
190-
fields,
178+
write!(
179+
&mut symbol,
180+
"__{}",
191181
def.variants[variant].fields[idx as usize].ident.name.as_str(),
192-
);
182+
)
183+
.unwrap();
193184
}
194185
ty => {
195186
bug!("Unexpected type {:?} for `Field` projection", ty)
@@ -204,7 +195,7 @@ impl CapturedPlace<'tcx> {
204195
ty = proj.ty;
205196
}
206197

207-
prefix.to_owned() + &name.to_string() + &fields
198+
Symbol::intern(&symbol)
208199
}
209200

210201
/// Returns the hir-id of the root variable for the captured place.

compiler/rustc_mir_build/src/build/mod.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_middle::mir::*;
1616
use rustc_middle::thir::{BindingMode, Expr, ExprId, LintLevel, PatKind, Thir};
1717
use rustc_middle::ty::subst::Subst;
1818
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeckResults};
19-
use rustc_span::symbol::{kw, sym};
19+
use rustc_span::symbol::sym;
2020
use rustc_span::Span;
2121
use rustc_target::spec::abi::Abi;
2222
use rustc_target::spec::PanicStrategy;
@@ -974,13 +974,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
974974

975975
let mutability = captured_place.mutability;
976976

977-
// FIXME(project-rfc-2229#8): Store more precise information
978-
let mut name = kw::Empty;
979-
if let Some(Node::Binding(pat)) = tcx_hir.find(var_id) {
980-
if let hir::PatKind::Binding(_, _, ident, _) = pat.kind {
981-
name = ident.name;
982-
}
983-
}
977+
let name = captured_place.to_symbol(tcx);
984978

985979
let mut projs = closure_env_projs.clone();
986980
projs.push(ProjectionElem::Field(Field::new(i), ty));

src/test/debuginfo/captured-fields.rs renamed to src/test/debuginfo/captured-fields-1.rs

+22-10
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,44 @@
44

55
// gdb-command:run
66
// gdb-command:print test
7-
// gdbr-check:$1 = captured_fields::main::{closure#0} {_captured_ref__my_ref__my_field1: 0x[...]}
7+
// gdbr-check:$1 = captured_fields_1::main::{closure#0} {_ref__my_ref__my_field1: 0x[...]}
88
// gdb-command:continue
99
// gdb-command:print test
10-
// gdbr-check:$2 = captured_fields::main::{closure#1} {_captured_ref__my_ref__my_field2: 0x[...]}
10+
// gdbr-check:$2 = captured_fields_1::main::{closure#1} {_ref__my_ref__my_field2: 0x[...]}
1111
// gdb-command:continue
1212
// gdb-command:print test
13-
// gdbr-check:$3 = captured_fields::main::{closure#2} {_captured_ref__my_ref: 0x[...]}
13+
// gdbr-check:$3 = captured_fields_1::main::{closure#2} {_ref__my_ref: 0x[...]}
1414
// gdb-command:continue
1515
// gdb-command:print test
16-
// gdbr-check:$4 = captured_fields::main::{closure#3} {_captured_val__my_ref: 0x[...]}
16+
// gdbr-check:$4 = captured_fields_1::main::{closure#3} {my_ref: 0x[...]}
1717
// gdb-command:continue
1818
// gdb-command:print test
19-
// gdbr-check:$5 = captured_fields::main::{closure#4} {_captured_val__my_var: captured_fields::MyStruct {my_field1: 11, my_field2: 22}}
19+
// gdbr-check:$5 = captured_fields_1::main::{closure#4} {my_var__my_field2: 22}
20+
// gdb-command:continue
21+
// gdb-command:print test
22+
// gdbr-check:$6 = captured_fields_1::main::{closure#5} {my_var: captured_fields_1::MyStruct {my_field1: 11, my_field2: 22}}
2023
// gdb-command:continue
2124

2225
// === LLDB TESTS ==================================================================================
2326

2427
// lldb-command:run
2528
// lldb-command:print test
26-
// lldbg-check:(captured_fields::main::{closure#0}) $0 = { _captured_ref__my_ref__my_field1 = 0x[...] }
29+
// lldbg-check:(captured_fields_1::main::{closure#0}) $0 = { _ref__my_ref__my_field1 = 0x[...] }
30+
// lldb-command:continue
31+
// lldb-command:print test
32+
// lldbg-check:(captured_fields_1::main::{closure#1}) $1 = { _ref__my_ref__my_field2 = 0x[...] }
2733
// lldb-command:continue
2834
// lldb-command:print test
29-
// lldbg-check:(captured_fields::main::{closure#1}) $1 = { _captured_ref__my_ref__my_field2 = 0x[...] }
35+
// lldbg-check:(captured_fields_1::main::{closure#2}) $2 = { _ref__my_ref = 0x[...] }
3036
// lldb-command:continue
3137
// lldb-command:print test
32-
// lldbg-check:(captured_fields::main::{closure#2}) $2 = { _captured_ref__my_ref = 0x[...] }
38+
// lldbg-check:(captured_fields_1::main::{closure#3}) $3 = { my_ref = 0x[...] }
3339
// lldb-command:continue
3440
// lldb-command:print test
35-
// lldbg-check:(captured_fields::main::{closure#3}) $3 = { _captured_val__my_ref = 0x[...] }
41+
// lldbg-check:(captured_fields_1::main::{closure#4}) $4 = { my_var__my_field2 = 22 }
3642
// lldb-command:continue
3743
// lldb-command:print test
38-
// lldbg-check:(captured_fields::main::{closure#4}) $4 = { _captured_val__my_var = { my_field1 = 11 my_field2 = 22 } }
44+
// lldbg-check:(captured_fields_1::main::{closure#5}) $5 = { my_var = { my_field1 = 11 my_field2 = 22 } }
3945
// lldb-command:continue
4046

4147
#![feature(capture_disjoint_fields)]
@@ -77,6 +83,12 @@ fn main() {
7783

7884
_zzz(); // #break
7985

86+
let test = move || {
87+
let a = my_var.my_field2;
88+
};
89+
90+
_zzz(); // #break
91+
8092
let test = || {
8193
let a = my_var;
8294
};
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// compile-flags:-g
2+
3+
// === GDB TESTS ===================================================================================
4+
5+
// gdb-command:run
6+
// gdb-command:print my_ref__my_field1
7+
// gdbr-check:$1 = 11
8+
// gdb-command:continue
9+
// gdb-command:print my_var__my_field2
10+
// gdbr-check:$2 = 22
11+
// gdb-command:continue
12+
13+
// === LLDB TESTS ==================================================================================
14+
15+
// lldb-command:run
16+
// lldb-command:print my_ref__my_field1
17+
// lldbg-check:(unsigned int) $0 = 11
18+
// lldb-command:continue
19+
// lldb-command:print my_var__my_field2
20+
// lldbg-check:(unsigned int) $1 = 22
21+
// lldb-command:continue
22+
23+
#![feature(capture_disjoint_fields)]
24+
#![allow(unused)]
25+
26+
struct MyStruct {
27+
my_field1: u32,
28+
my_field2: u32,
29+
}
30+
31+
fn main() {
32+
let mut my_var = MyStruct {
33+
my_field1: 11,
34+
my_field2: 22,
35+
};
36+
let my_ref = &mut my_var;
37+
38+
let test = || {
39+
let a = my_ref.my_field1;
40+
41+
_zzz(); // #break
42+
};
43+
44+
test();
45+
46+
let test = move || {
47+
let a = my_var.my_field2;
48+
49+
_zzz(); // #break
50+
};
51+
52+
test();
53+
}
54+
55+
fn _zzz() {}

src/test/debuginfo/generator-objects.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111

1212
// gdb-command:run
1313
// gdb-command:print b
14-
// gdb-check:$1 = generator_objects::main::{generator#0}::Unresumed{_captured_ref__a: 0x[...]}
14+
// gdb-check:$1 = generator_objects::main::{generator#0}::Unresumed{_ref__a: 0x[...]}
1515
// gdb-command:continue
1616
// gdb-command:print b
17-
// gdb-check:$2 = generator_objects::main::{generator#0}::Suspend0{c: 6, d: 7, _captured_ref__a: 0x[...]}
17+
// gdb-check:$2 = generator_objects::main::{generator#0}::Suspend0{c: 6, d: 7, _ref__a: 0x[...]}
1818
// gdb-command:continue
1919
// gdb-command:print b
20-
// gdb-check:$3 = generator_objects::main::{generator#0}::Suspend1{c: 7, d: 8, _captured_ref__a: 0x[...]}
20+
// gdb-check:$3 = generator_objects::main::{generator#0}::Suspend1{c: 7, d: 8, _ref__a: 0x[...]}
2121
// gdb-command:continue
2222
// gdb-command:print b
23-
// gdb-check:$4 = generator_objects::main::{generator#0}::Returned{_captured_ref__a: 0x[...]}
23+
// gdb-check:$4 = generator_objects::main::{generator#0}::Returned{_ref__a: 0x[...]}
2424

2525
// === LLDB TESTS ==================================================================================
2626

src/test/debuginfo/issue-57822.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
// gdb-command:run
1212

1313
// gdb-command:print g
14-
// gdb-check:$1 = issue_57822::main::{closure#1} {_captured_val__f: issue_57822::main::{closure#0} {_captured_val__x: 1}}
14+
// gdb-check:$1 = issue_57822::main::{closure#1} {f: issue_57822::main::{closure#0} {x: 1}}
1515

1616
// gdb-command:print b
17-
// gdb-check:$2 = issue_57822::main::{generator#3}::Unresumed{_captured_val__a: issue_57822::main::{generator#2}::Unresumed{_captured_val__y: 2}}
17+
// gdb-check:$2 = issue_57822::main::{generator#3}::Unresumed{a: issue_57822::main::{generator#2}::Unresumed{y: 2}}
1818

1919
// === LLDB TESTS ==================================================================================
2020

2121
// lldb-command:run
2222

2323
// lldb-command:print g
24-
// lldbg-check:(issue_57822::main::{closure#1}) $0 = { _captured_val__f = { _captured_val__x = 1 } }
24+
// lldbg-check:(issue_57822::main::{closure#1}) $0 = { f = { x = 1 } }
2525

2626
// lldb-command:print b
2727
// lldbg-check:(issue_57822::main::{generator#3}) $1 =

0 commit comments

Comments
 (0)