Skip to content

Add debuginfo_transparent attribute for structs #144223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bjorn3
Copy link
Member

@bjorn3 bjorn3 commented Jul 20, 2025

This attribute causes the struct to be unwrapped at the debuginfo level the same way that repr(transparent) unwraps it at the ABI level. This is useful for preventing types like NonNull and Unique from making the debuginfo harder to read when pretty printers aren't used.

@rustbot
Copy link
Collaborator

rustbot commented Jul 20, 2025

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 20, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 20, 2025

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann

Some changes occurred in compiler/rustc_attr_data_structures

cc @jdonszelmann

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann

@@ -1070,6 +1071,16 @@ fn build_struct_type_di_node<'ll, 'tcx>(
None
};

if find_attr!(cx.tcx.get_all_attrs(adt_def.did()), AttributeKind::DebuginfoTransparent(..)) {
let ty = struct_type_and_layout.non_1zst_field(cx).unwrap().1.ty;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to ensure that #[debuginfo_transparent] can only be used when #[repr(transparent)] is also used? And how should I handle the case where all fields are 1zst's?

@@ -7,7 +7,7 @@
// gdb-command:run

// gdb-command:print plain_string
// gdb-check:$1 = alloc::string::String {vec: alloc::vec::Vec<u8, alloc::alloc::Global> {buf: alloc::raw_vec::RawVec<u8, alloc::alloc::Global> {inner: alloc::raw_vec::RawVecInner<alloc::alloc::Global> {ptr: core::ptr::unique::Unique<u8> {pointer: core::ptr::non_null::NonNull<u8> {pointer: 0x[...]}, _marker: core::marker::PhantomData<u8>}, cap: core::num::niche_types::UsizeNoHighBit (5), alloc: alloc::alloc::Global}, _marker: core::marker::PhantomData<u8>}, len: 5}}
// gdb-check:$1 = alloc::string::String {vec: alloc::vec::Vec<u8, alloc::alloc::Global> {buf: alloc::raw_vec::RawVec<u8, alloc::alloc::Global> {inner: alloc::raw_vec::RawVecInner<alloc::alloc::Global> {ptr: 0x[...], cap: 5, alloc: alloc::alloc::Global}, _marker: core::marker::PhantomData<u8>}, len: 5}}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the improvement this PR causes. Further improvements could be made using future #[debuginfo_flatten] and #[debuginfo_hidden] attributes on RawVec/RawVecInner and the _marker field, which would result in

alloc::string::String {vec: alloc::vec::Vec<u8, alloc::alloc::Global> {ptr: 0x[...], cap: 5, alloc: alloc::alloc::Global, len: 5}}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar changes would need to be made for LLDB, but LLDB is broken on the dev-desktop.

@rust-log-analyzer

This comment has been minimized.

@bjorn3 bjorn3 added the A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) label Jul 20, 2025
This attribute causes the struct to be unwrapped at the debuginfo level
the same way that repr(transparent) unwraps it at the ABI level. This is
useful for preventing types like NonNull and Unique from making the
debuginfo harder to read when pretty printers aren't used.
@bjorn3 bjorn3 force-pushed the better_debuginfo branch from 0958203 to 9c64c42 Compare July 20, 2025 15:29
@rustbot
Copy link
Collaborator

rustbot commented Jul 20, 2025

There are changes to the tidy tool.

cc @jieyouxu

@rustbot rustbot added A-tidy Area: The tidy tool T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) labels Jul 20, 2025
@rust-log-analyzer
Copy link
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
Checking tidy rustdoc_json...
Running eslint on rustdoc JS files
No error code explanation was removed!
tidy: Skipping binary file check, read-only filesystem
##[error]tidy error: /checkout/compiler/rustc_feature/src/unstable.rs:466: no tracking issue for feature debuginfo_attrs
removing old virtual environment
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10' and 'venv'
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10' and 'virtualenv'
Requirement already satisfied: pip in ./build/venv/lib/python3.10/site-packages (25.1.1)
linting python files
---
1 file would be reformatted, 27 files already formatted
--- /checkout/src/etc/gdb_providers.py
+++ /checkout/src/etc/gdb_providers.py
@@ -284,8 +284,9 @@
             internal_type = gdb.lookup_type(internal_type_name)
             return node.cast(internal_type.pointer())
 
-        if node_ptr.type.name is not None and \
-            node_ptr.type.name.startswith("alloc::collections::btree::node::BoxedNode<"):
+        if node_ptr.type.name is not None and node_ptr.type.name.startswith(
+            "alloc::collections::btree::node::BoxedNode<"
+        ):
             # BACKCOMPAT: rust 1.49
             node_ptr = node_ptr["ptr"]
         node_ptr = unwrap_unique_or_non_null(node_ptr)

rerun tidy with `--extra-checks=py:fmt --bless` to reformat Python code
some tidy checks failed
tidy error: checks with external tool 'ruff' failed
Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:01:20
  local time: Sun Jul 20 15:35:10 UTC 2025
  network time: Sun, 20 Jul 2025 15:35:10 GMT
##[error]Process completed with exit code 1.
Post job cleanup.

@petrochenkov
Copy link
Contributor

I don't have a strong opinion on whether this attribute should be added or not.
On one hand it duplicates functionality of gdb pretty-printers or equivalents + maybe #[debugger_visualizer], on the other hand it's clearly easier to use.

I'll delegate this to the lang team, because it approves new attributes anyway.

Also, for the implementation maybe find some other reviewer more interested in debuginfo?

@petrochenkov petrochenkov added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). T-lang Relevant to the language team and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-tidy Area: The tidy tool S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). T-lang Relevant to the language team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants