Skip to content

Commit e039dc8

Browse files
authored
Rollup merge of rust-lang#95270 - michaelwoerister:fix-box-unsized-debuginfo, r=wesleywiser
debuginfo: Fix debuginfo for Box<T> where T is unsized. Before this fix, the debuginfo for the fields was generated from the struct defintion of Box<T>, but (at least at the moment) the compiler pretends that Box<T> is just a (fat) pointer, so the fields need to be `pointer` and `vtable` instead of `__0: Unique<T>` and `__1: Allocator`. This is meant as a temporary mitigation until we can make sure that simply treating Box as a regular struct in debuginfo does not cause too much breakage in the ecosystem. r? ````@wesleywiser````
2 parents 1fcb8fc + e169261 commit e039dc8

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
166166
pointee_type: Ty<'tcx>,
167167
unique_type_id: UniqueTypeId<'tcx>,
168168
) -> DINodeCreationResult<'ll> {
169+
// The debuginfo generated by this function is only valid if `ptr_type` is really just
170+
// a (fat) pointer. Make sure it is not called for e.g. `Box<T, NonZSTAllocator>`.
171+
debug_assert_eq!(
172+
cx.size_and_align_of(ptr_type),
173+
cx.size_and_align_of(cx.tcx.mk_mut_ptr(pointee_type))
174+
);
175+
169176
let pointee_type_di_node = type_di_node(cx, pointee_type);
170177

171178
return_if_di_node_created_in_meantime!(cx, unique_type_id);
@@ -212,7 +219,17 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
212219
DIFlags::FlagZero,
213220
),
214221
|cx, owner| {
215-
let layout = cx.layout_of(ptr_type);
222+
// FIXME: If this fat pointer is a `Box` then we don't want to use its
223+
// type layout and instead use the layout of the raw pointer inside
224+
// of it.
225+
// The proper way to handle this is to not treat Box as a pointer
226+
// at all and instead emit regular struct debuginfo for it. We just
227+
// need to make sure that we don't break existing debuginfo consumers
228+
// by doing that (at least not without a warning period).
229+
let layout_type =
230+
if ptr_type.is_box() { cx.tcx.mk_mut_ptr(pointee_type) } else { ptr_type };
231+
232+
let layout = cx.layout_of(layout_type);
216233
let addr_field = layout.field(cx, abi::FAT_PTR_ADDR);
217234
let extra_field = layout.field(cx, abi::FAT_PTR_EXTRA);
218235

src/test/debuginfo/unsized.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616
// gdbg-check:$3 = {pointer = [...], vtable = [...]}
1717
// gdbr-check:$3 = &unsized::Foo<dyn core::fmt::Debug> {pointer: [...], vtable: [...]}
1818

19+
// gdb-command:print _box
20+
// gdbg-check:$4 = {pointer = [...], vtable = [...]}
21+
// gdbr-check:$4 = alloc::boxed::Box<unsized::Foo<dyn core::fmt::Debug>, alloc::alloc::Global> {pointer: [...], vtable: [...]}
22+
1923
// gdb-command:print tuple_slice
20-
// gdbg-check:$4 = {data_ptr = [...], length = 2}
21-
// gdbr-check:$4 = &(i32, i32, [i32]) {data_ptr: [...], length: 2}
24+
// gdbg-check:$5 = {data_ptr = [...], length = 2}
25+
// gdbr-check:$5 = &(i32, i32, [i32]) {data_ptr: [...], length: 2}
2226

2327
// gdb-command:print tuple_dyn
24-
// gdbg-check:$5 = {pointer = [...], vtable = [...]}
25-
// gdbr-check:$5 = &(i32, i32, dyn core::fmt::Debug) {pointer: [...], vtable: [...]}
28+
// gdbg-check:$6 = {pointer = [...], vtable = [...]}
29+
// gdbr-check:$6 = &(i32, i32, dyn core::fmt::Debug) {pointer: [...], vtable: [...]}
2630

2731
// === CDB TESTS ===================================================================================
2832

@@ -42,6 +46,12 @@
4246
// cdb-check: [+0x000] pointer : 0x[...] [Type: unsized::Foo<dyn$<core::fmt::Debug> > *]
4347
// cdb-check: [...] vtable : 0x[...] [Type: unsigned [...]int[...] (*)[3]]
4448

49+
// cdb-command:dx _box
50+
// cdb-check:
51+
// cdb-check:_box [Type: alloc::boxed::Box<unsized::Foo<dyn$<core::fmt::Debug> >,alloc::alloc::Global>]
52+
// cdb-check:[+0x000] pointer : 0x[...] [Type: unsized::Foo<dyn$<core::fmt::Debug> > *]
53+
// cdb-check:[...] vtable : 0x[...] [Type: unsigned [...]int[...] (*)[3]]
54+
4555
// cdb-command:dx tuple_slice
4656
// cdb-check:tuple_slice [Type: ref$<tuple$<i32,i32,slice$<i32> > >]
4757
// cdb-check: [+0x000] data_ptr : 0x[...] [Type: tuple$<i32,i32,slice$<i32> > *]
@@ -69,6 +79,7 @@ fn main() {
6979
let a: &Foo<[u8]> = &foo.value;
7080
let b: &Foo<Foo<[u8]>> = &foo;
7181
let c: &Foo<dyn std::fmt::Debug> = &Foo { value: 7i32 };
82+
let _box: Box<Foo<dyn std::fmt::Debug>> = Box::new(Foo { value: 8i32 });
7283

7384
// Also check unsized tuples
7485
let tuple_slice: &(i32, i32, [i32]) = &(0, 1, [2, 3]);

0 commit comments

Comments
 (0)