Skip to content

Commit 9c64c42

Browse files
committed
Add debuginfo_transparent attribute for structs
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.
1 parent 6707bf0 commit 9c64c42

File tree

19 files changed

+115
-15
lines changed

19 files changed

+115
-15
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ pub enum AttributeKind {
293293
/// Represents `#[coverage]`.
294294
Coverage(Span, CoverageStatus),
295295

296+
/// Represents `#[debuginfo_transparent]`.
297+
DebuginfoTransparent(Span),
298+
296299
///Represents `#[rustc_deny_explicit_impl]`.
297300
DenyExplicitImpl(Span),
298301

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ impl AttributeKind {
2929
ConstStabilityIndirect => No,
3030
ConstTrait(..) => No,
3131
Coverage(..) => No,
32+
DebuginfoTransparent { .. } => Yes,
3233
DenyExplicitImpl(..) => No,
3334
Deprecation { .. } => Yes,
3435
DoNotImplementViaObject(..) => No,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use rustc_attr_data_structures::AttributeKind;
2+
use rustc_span::{Span, Symbol, sym};
3+
4+
use crate::attributes::{NoArgsAttributeParser, OnDuplicate};
5+
use crate::context::Stage;
6+
7+
pub(crate) struct DebuginfoTransparentParser;
8+
impl<S: Stage> NoArgsAttributeParser<S> for DebuginfoTransparentParser {
9+
const PATH: &[Symbol] = &[sym::debuginfo_transparent];
10+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
11+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::DebuginfoTransparent;
12+
}

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub(crate) mod cfg;
3030
pub(crate) mod cfg_old;
3131
pub(crate) mod codegen_attrs;
3232
pub(crate) mod confusables;
33+
pub(crate) mod debuginfo;
3334
pub(crate) mod deprecation;
3435
pub(crate) mod dummy;
3536
pub(crate) mod inline;

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::attributes::codegen_attrs::{
2222
UsedParser,
2323
};
2424
use crate::attributes::confusables::ConfusablesParser;
25+
use crate::attributes::debuginfo::DebuginfoTransparentParser;
2526
use crate::attributes::deprecation::DeprecationParser;
2627
use crate::attributes::dummy::DummyParser;
2728
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
@@ -167,6 +168,7 @@ attribute_parsers!(
167168
Single<WithoutArgs<ConstContinueParser>>,
168169
Single<WithoutArgs<ConstStabilityIndirectParser>>,
169170
Single<WithoutArgs<ConstTraitParser>>,
171+
Single<WithoutArgs<DebuginfoTransparentParser>>,
170172
Single<WithoutArgs<DenyExplicitImplParser>>,
171173
Single<WithoutArgs<DoNotImplementViaObjectParser>>,
172174
Single<WithoutArgs<ExportStableParser>>,

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{iter, ptr};
77

88
use libc::{c_longlong, c_uint};
99
use rustc_abi::{Align, Size};
10+
use rustc_attr_data_structures::{AttributeKind, find_attr};
1011
use rustc_codegen_ssa::debuginfo::type_names::{VTableNameKind, cpp_like_debuginfo};
1112
use rustc_codegen_ssa::traits::*;
1213
use rustc_hir::def::{CtorKind, DefKind};
@@ -1070,6 +1071,16 @@ fn build_struct_type_di_node<'ll, 'tcx>(
10701071
None
10711072
};
10721073

1074+
if find_attr!(cx.tcx.get_all_attrs(adt_def.did()), AttributeKind::DebuginfoTransparent(..)) {
1075+
let ty = struct_type_and_layout.non_1zst_field(cx).unwrap().1.ty;
1076+
1077+
let di_node = type_di_node(cx, ty);
1078+
1079+
return_if_di_node_created_in_meantime!(cx, unique_type_id);
1080+
1081+
return DINodeCreationResult::new(di_node, false);
1082+
}
1083+
10731084
type_map::build_type_with_children(
10741085
cx,
10751086
type_map::stub(

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,11 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
666666
EncodeCrossCrate::No, loop_match, experimental!(loop_match)
667667
),
668668

669+
gated!(
670+
debuginfo_transparent, Normal, template!(Word), WarnFollowing,
671+
EncodeCrossCrate::Yes, debuginfo_attrs, experimental!(debuginfo_transparent)
672+
),
673+
669674
// ==========================================================================
670675
// Internal attributes: Stability, deprecation, and unsafe:
671676
// ==========================================================================

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ declare_features! (
462462
(unstable, custom_inner_attributes, "1.30.0", Some(54726)),
463463
/// Allows custom test frameworks with `#![test_runner]` and `#[test_case]`.
464464
(unstable, custom_test_frameworks, "1.30.0", Some(50297)),
465+
/// Allows `debuginfo_*` attributes.
466+
(unstable, debuginfo_attrs, "CURRENT_RUSTC_VERSION", None),
465467
/// Allows declarative macros 2.0 (`macro`).
466468
(unstable, decl_macro, "1.17.0", Some(39412)),
467469
/// Allows the use of default values on struct definitions and the construction of struct

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ pub fn check_builtin_meta_item(
319319
| sym::no_implicit_prelude
320320
| sym::automatically_derived
321321
| sym::coverage
322+
| sym::debuginfo_transparent
322323
) {
323324
return;
324325
}

compiler/rustc_passes/src/check_attr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
170170
target,
171171
Target::Impl { of_trait: true },
172172
),
173+
Attribute::Parsed(AttributeKind::DebuginfoTransparent(attr_span)) => self
174+
.check_generic_attr(
175+
hir_id,
176+
sym::debuginfo_transparent,
177+
*attr_span,
178+
target,
179+
Target::Struct,
180+
),
173181
Attribute::Parsed(
174182
AttributeKind::Stability {
175183
span: attr_span,

0 commit comments

Comments
 (0)