Skip to content

Commit 04c4fe2

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 dc0bae1 commit 04c4fe2

File tree

18 files changed

+115
-15
lines changed

18 files changed

+115
-15
lines changed
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
@@ -21,6 +21,7 @@ use crate::attributes::codegen_attrs::{
2121
TargetFeatureParser, TrackCallerParser, UsedParser,
2222
};
2323
use crate::attributes::confusables::ConfusablesParser;
24+
use crate::attributes::debuginfo::DebuginfoTransparentParser;
2425
use crate::attributes::deprecation::DeprecationParser;
2526
use crate::attributes::dummy::DummyParser;
2627
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
@@ -184,6 +185,7 @@ attribute_parsers!(
184185
Single<WithoutArgs<ConstContinueParser>>,
185186
Single<WithoutArgs<ConstStabilityIndirectParser>>,
186187
Single<WithoutArgs<ConstTraitParser>>,
188+
Single<WithoutArgs<DebuginfoTransparentParser>>,
187189
Single<WithoutArgs<DenyExplicitImplParser>>,
188190
Single<WithoutArgs<DoNotImplementViaObjectParser>>,
189191
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
@@ -667,6 +667,11 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
667667
EncodeCrossCrate::No, loop_match, experimental!(loop_match)
668668
),
669669

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

compiler/rustc_feature/src/unstable.rs

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

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ pub enum AttributeKind {
300300
/// Represents `#[coverage(..)]`.
301301
Coverage(Span, CoverageAttrKind),
302302

303+
/// Represents `#[debuginfo_transparent]`.
304+
DebuginfoTransparent(Span),
305+
303306
///Represents `#[rustc_deny_explicit_impl]`.
304307
DenyExplicitImpl(Span),
305308

compiler/rustc_hir/src/attrs/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,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
184184
target,
185185
Target::Impl { of_trait: true },
186186
),
187+
Attribute::Parsed(AttributeKind::DebuginfoTransparent(attr_span)) => self
188+
.check_generic_attr(
189+
hir_id,
190+
sym::debuginfo_transparent,
191+
*attr_span,
192+
target,
193+
Target::Struct,
194+
),
187195
Attribute::Parsed(
188196
AttributeKind::Stability {
189197
span: attr_span,

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,8 @@ symbols! {
805805
debug_tuple,
806806
debug_tuple_fields_finish,
807807
debugger_visualizer,
808+
debuginfo_attrs,
809+
debuginfo_transparent,
808810
decl_macro,
809811
declare_lint_pass,
810812
decode,

0 commit comments

Comments
 (0)