Skip to content

Commit 45c1dcd

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 3507a74 commit 45c1dcd

File tree

20 files changed

+135
-15
lines changed

20 files changed

+135
-15
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use rustc_hir::Target;
2+
use rustc_hir::attrs::AttributeKind;
3+
use rustc_span::{Span, Symbol, sym};
4+
5+
use crate::attributes::{NoArgsAttributeParser, OnDuplicate};
6+
use crate::context::MaybeWarn::Allow;
7+
use crate::context::{AllowedTargets, Stage};
8+
9+
pub(crate) struct DebuginfoTransparentParser;
10+
impl<S: Stage> NoArgsAttributeParser<S> for DebuginfoTransparentParser {
11+
const PATH: &[Symbol] = &[sym::debuginfo_transparent];
12+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
13+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
14+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::DebuginfoTransparent;
15+
}

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub(crate) mod cfg;
3131
pub(crate) mod cfg_old;
3232
pub(crate) mod codegen_attrs;
3333
pub(crate) mod confusables;
34+
pub(crate) mod debuginfo;
3435
pub(crate) mod deprecation;
3536
pub(crate) mod dummy;
3637
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
@@ -25,6 +25,7 @@ use crate::attributes::codegen_attrs::{
2525
TargetFeatureParser, TrackCallerParser, UsedParser,
2626
};
2727
use crate::attributes::confusables::ConfusablesParser;
28+
use crate::attributes::debuginfo::DebuginfoTransparentParser;
2829
use crate::attributes::deprecation::DeprecationParser;
2930
use crate::attributes::dummy::DummyParser;
3031
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
@@ -199,6 +200,7 @@ attribute_parsers!(
199200
Single<WithoutArgs<ConstStabilityIndirectParser>>,
200201
Single<WithoutArgs<ConstTraitParser>>,
201202
Single<WithoutArgs<CoroutineParser>>,
203+
Single<WithoutArgs<DebuginfoTransparentParser>>,
202204
Single<WithoutArgs<DenyExplicitImplParser>>,
203205
Single<WithoutArgs<DoNotImplementViaObjectParser>>,
204206
Single<WithoutArgs<ExportStableParser>>,

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ use libc::{c_longlong, c_uint};
99
use rustc_abi::{Align, Size};
1010
use rustc_codegen_ssa::debuginfo::type_names::{VTableNameKind, cpp_like_debuginfo};
1111
use rustc_codegen_ssa::traits::*;
12+
use rustc_hir::attrs::AttributeKind;
1213
use rustc_hir::def::{CtorKind, DefKind};
1314
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
15+
use rustc_hir::find_attr;
1416
use rustc_middle::bug;
1517
use rustc_middle::ty::layout::{
1618
HasTypingEnv, LayoutOf, TyAndLayout, WIDE_PTR_ADDR, WIDE_PTR_EXTRA,
@@ -1070,6 +1072,16 @@ fn build_struct_type_di_node<'ll, 'tcx>(
10701072
None
10711073
};
10721074

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

compiler/rustc_feature/src/builtin_attrs.rs

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

883+
gated!(
884+
debuginfo_transparent, Normal, template!(Word), WarnFollowing,
885+
EncodeCrossCrate::Yes, debuginfo_attrs, experimental!(debuginfo_transparent)
886+
),
887+
883888
// ==========================================================================
884889
// Internal attributes: Stability, deprecation, and unsafe:
885890
// ==========================================================================

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,8 @@ declare_features! (
459459
(unstable, custom_inner_attributes, "1.30.0", Some(54726)),
460460
/// Allows custom test frameworks with `#![test_runner]` and `#[test_case]`.
461461
(unstable, custom_test_frameworks, "1.30.0", Some(50297)),
462+
/// Allows `debuginfo_*` attributes.
463+
(unstable, debuginfo_attrs, "CURRENT_RUSTC_VERSION", None),
462464
/// Allows declarative macros 2.0 (`macro`).
463465
(unstable, decl_macro, "1.17.0", Some(39412)),
464466
/// 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
@@ -324,6 +324,9 @@ pub enum AttributeKind {
324324
/// Represents `#[coverage(..)]`.
325325
Coverage(Span, CoverageAttrKind),
326326

327+
/// Represents `#[debuginfo_transparent]`.
328+
DebuginfoTransparent(Span),
329+
327330
///Represents `#[rustc_deny_explicit_impl]`.
328331
DenyExplicitImpl(Span),
329332

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ impl AttributeKind {
3131
ConstTrait(..) => No,
3232
Coroutine(..) => No,
3333
Coverage(..) => No,
34+
DebuginfoTransparent { .. } => Yes,
3435
DenyExplicitImpl(..) => No,
3536
Deprecation { .. } => Yes,
3637
DoNotImplementViaObject(..) => No,

compiler/rustc_passes/messages.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ passes_dead_codes =
8383
}
8484
} never {$participle}
8585
86+
passes_debuginfo_transparent =
87+
attribute should be applied to `#[repr(transparent)]` types
88+
.label = not a `#[repr(transparent)]` type
89+
8690
passes_debug_visualizer_invalid =
8791
invalid argument
8892
.note_1 = expected: `natvis_file = "..."`

compiler/rustc_passes/src/check_attr.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
138138
&Attribute::Parsed(AttributeKind::TypeConst(attr_span)) => {
139139
self.check_type_const(hir_id, attr_span, target)
140140
}
141+
&Attribute::Parsed(AttributeKind::DebuginfoTransparent(attr_span)) => {
142+
self.check_debuginfo_transparent(attr_span, span, attrs)
143+
},
141144
Attribute::Parsed(
142145
AttributeKind::Stability {
143146
span: attr_span,
@@ -2015,6 +2018,14 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
20152018
}
20162019
}
20172020

2021+
fn check_debuginfo_transparent(&self, attr_span: Span, span: Span, attrs: &[Attribute]) {
2022+
if !find_attr!(attrs, AttributeKind::Repr { reprs, .. } => reprs.iter().any(|(r, _)| r == &ReprAttr::ReprTransparent))
2023+
.unwrap_or(false)
2024+
{
2025+
self.dcx().emit_err(errors::DebuginfoTransparent { span, attr_span });
2026+
}
2027+
}
2028+
20182029
fn check_rustc_pub_transparent(&self, attr_span: Span, span: Span, attrs: &[Attribute]) {
20192030
if !find_attr!(attrs, AttributeKind::Repr { reprs, .. } => reprs.iter().any(|(r, _)| r == &ReprAttr::ReprTransparent))
20202031
.unwrap_or(false)

0 commit comments

Comments
 (0)