Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions compiler/rustc_attr_parsing/src/attributes/rustc_dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,35 @@ impl SingleAttributeParser for RustcDumpDefPathParser {
}
}

pub(crate) struct RustcDumpGenericsParser;

impl NoArgsAttributeParser for RustcDumpGenericsParser {
const PATH: &[Symbol] = &[sym::rustc_dump_generics];
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
Comment thread
addiesh marked this conversation as resolved.
Comment thread
addiesh marked this conversation as resolved.
Allow(Target::Struct),
Allow(Target::Enum),
Allow(Target::Union),
Allow(Target::Trait),
Copy link
Copy Markdown
Contributor

@jdonszelmann jdonszelmann Jun 4, 2026

Choose a reason for hiding this comment

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

You'll probably want a test using it on a trait some day, because I imagine the interaction can be a bit more involved for that. Im not quite sure how far along this is from proof of concept to done. You might want to initially not support traits if that test gives you trouble?

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Just added a test for a trait, but it's definitely not exhaustive. Let me know your thoughts.

Allow(Target::TraitAlias),
Allow(Target::Fn),
Allow(Target::Closure),
Allow(Target::TyAlias),
Allow(Target::Const),
Allow(Target::AssocConst),
Allow(Target::AssocTy),
Allow(Target::Impl { of_trait: false }),
Allow(Target::Impl { of_trait: true }),
Allow(Target::Method(MethodKind::Inherent)),
Allow(Target::Method(MethodKind::Trait { body: false })),
Allow(Target::Method(MethodKind::Trait { body: true })),
Allow(Target::Method(MethodKind::TraitImpl)),
Allow(Target::Delegation { mac: false }),
Allow(Target::Delegation { mac: true }),
]);
const STABILITY: AttributeStability = unstable!(rustc_attrs);
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcDumpGenerics;
}

pub(crate) struct RustcDumpHiddenTypeOfOpaquesParser;

impl NoArgsAttributeParser for RustcDumpHiddenTypeOfOpaquesParser {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ attribute_parsers!(
Single<WithoutArgs<RustcDenyExplicitImplParser>>,
Single<WithoutArgs<RustcDoNotConstCheckParser>>,
Single<WithoutArgs<RustcDumpDefParentsParser>>,
Single<WithoutArgs<RustcDumpGenericsParser>>,
Single<WithoutArgs<RustcDumpHiddenTypeOfOpaquesParser>>,
Single<WithoutArgs<RustcDumpInferredOutlivesParser>>,
Single<WithoutArgs<RustcDumpItemBoundsParser>>,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ pub static BUILTIN_ATTRIBUTES: &[Symbol] = &[
sym::rustc_strict_coherence,
sym::rustc_dump_variances,
sym::rustc_dump_variances_of_opaques,
sym::rustc_dump_generics,
sym::rustc_dump_hidden_type_of_opaques,
sym::rustc_dump_layout,
sym::rustc_abi,
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_hir/src/attrs/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,9 @@ pub enum AttributeKind {
/// Represents `#[rustc_dump_def_path]`
RustcDumpDefPath(Span),

/// Represents `#[rustc_dump_generics]`
RustcDumpGenerics,

/// Represents `#[rustc_dump_hidden_type_of_opaques]`
RustcDumpHiddenTypeOfOpaques,

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/attrs/encode_cross_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl AttributeKind {
RustcDummy => No,
RustcDumpDefParents => No,
RustcDumpDefPath(..) => No,
RustcDumpGenerics => No,
RustcDumpHiddenTypeOfOpaques => No,
RustcDumpInferredOutlives => No,
RustcDumpItemBounds => No,
Expand Down
26 changes: 22 additions & 4 deletions compiler/rustc_hir_analysis/src/collect/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ use rustc_middle::hir::nested_filter;
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt, Unnormalized};
use rustc_span::sym;

pub(crate) fn generics(tcx: TyCtxt<'_>) {
for did in tcx.hir_crate_items(()).definitions() {
if did == hir::def_id::CRATE_DEF_ID {
continue;
}

if find_attr!(tcx, did, RustcDumpGenerics) {
let span = tcx.def_span(did);

let mut diag =
tcx.dcx().struct_span_err(span, format!("{}: {did:?}", sym::rustc_dump_generics));

let generics = tcx.generics_of(did);
diag.span_note(tcx.def_span(did), format!("{generics:#?}"));
diag.emit();
}
}
}

pub(crate) fn opaque_hidden_types(tcx: TyCtxt<'_>) {
if !find_attr!(tcx, crate, RustcDumpHiddenTypeOfOpaques) {
return;
Expand Down Expand Up @@ -101,10 +120,9 @@ pub(crate) fn def_parents(tcx: TyCtxt<'_>) {
for did in [did].into_iter().chain(anon_ct_finder.anon_consts) {
let span = tcx.def_span(did);

let mut diag = tcx.dcx().struct_span_err(
span,
format!("{}: {did:?}", sym::rustc_dump_def_parents.as_str()),
);
let mut diag = tcx
.dcx()
.struct_span_err(span, format!("{}: {did:?}", sym::rustc_dump_def_parents));

let mut current_did = did.to_def_id();
while let Some(parent_did) = tcx.opt_parent(current_did) {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
tcx.sess.time("dumping_rustc_attr_data", || {
outlives::dump::inferred_outlives(tcx);
variance::dump::variances(tcx);
collect::dump::generics(tcx);
collect::dump::opaque_hidden_types(tcx);
collect::dump::predicates_and_item_bounds(tcx);
collect::dump::def_parents(tcx);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
AttributeKind::RustcDummy => (),
AttributeKind::RustcDumpDefParents => (),
AttributeKind::RustcDumpDefPath(..) => (),
AttributeKind::RustcDumpGenerics => (),
AttributeKind::RustcDumpHiddenTypeOfOpaques => (),
AttributeKind::RustcDumpInferredOutlives => (),
AttributeKind::RustcDumpItemBounds => (),
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,7 @@ symbols! {
rustc_dummy,
rustc_dump_def_parents,
rustc_dump_def_path,
rustc_dump_generics,
rustc_dump_hidden_type_of_opaques,
rustc_dump_inferred_outlives,
rustc_dump_item_bounds,
Expand Down
2 changes: 1 addition & 1 deletion library/backtrace
Copy link
Copy Markdown
Contributor

@cjgillot cjgillot Jun 5, 2026

Choose a reason for hiding this comment

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

Is this intentional?

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

nope 😅. I just couldn't figure out how to undo the change in git.

1 change: 1 addition & 0 deletions src/doc/rustc-dev-guide/src/compiler-debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ Here are some notable ones:
|----------------|-------------|
| `rustc_dump_def_parents` | Dumps the chain of `DefId` parents of certain definitions. |
| `rustc_dump_def_path` | Dumps the [`def_path_str`] of an item. |
| `rustc_dump_generics` | Dumps the generics of an item. |
| `rustc_dump_hidden_type_of_opaques` | Dumps the [hidden type of each opaque types][opaq] in the crate. |
| `rustc_dump_inferred_outlives` | Dumps implied bounds of an item. More precisely, the [`inferred_outlives_of`] an item. |
| `rustc_dump_item_bounds` | Dumps the [`item_bounds`] of an item. |
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/attributes/dump_generics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//@ normalize-stderr: "DefId\(.+?\)" -> "DefId(..)"

#![feature(rustc_attrs)]

fn foo() {
|| {
qux::<'_, '_, 400, u32, u64>();
};
}

#[rustc_dump_generics]
const fn qux<'a: 'a, 'b: 'b, const N: usize, T, U>() {}
//~^ ERROR: rustc_dump_generics: DefId(0:6 ~ dump_generics[c1f6]::qux)

fn main() {}
71 changes: 71 additions & 0 deletions tests/ui/attributes/dump_generics.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
error: rustc_dump_generics: DefId(..)
--> $DIR/dump_generics.rs:12:1
|
LL | const fn qux<'a: 'a, 'b: 'b, const N: usize, T, U>() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: Generics {
parent: None,
parent_count: 0,
own_params: [
GenericParamDef {
name: "'a",
def_id: DefId(..),
index: 0,
pure_wrt_drop: false,
kind: Lifetime,
},
GenericParamDef {
name: "'b",
def_id: DefId(..),
index: 1,
pure_wrt_drop: false,
kind: Lifetime,
},
GenericParamDef {
name: "N",
def_id: DefId(..),
index: 2,
pure_wrt_drop: false,
kind: Const {
has_default: false,
},
},
GenericParamDef {
name: "T",
def_id: DefId(..),
index: 3,
pure_wrt_drop: false,
kind: Type {
has_default: false,
synthetic: false,
},
},
GenericParamDef {
name: "U",
def_id: DefId(..),
index: 4,
pure_wrt_drop: false,
kind: Type {
has_default: false,
synthetic: false,
},
},
],
param_def_id_to_index: {
DefId(..): 2,
DefId(..): 0,
DefId(..): 4,
DefId(..): 3,
DefId(..): 1,
},
has_self: false,
has_late_bound_regions: None,
}
--> $DIR/dump_generics.rs:12:1
|
LL | const fn qux<'a: 'a, 'b: 'b, const N: usize, T, U>() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

17 changes: 17 additions & 0 deletions tests/ui/attributes/dump_generics_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@ normalize-stderr: "DefId\(.+?\)" -> "DefId(..)"

#![feature(rustc_attrs)]

#[rustc_dump_generics]
trait NiceOfThePrincess<'a, 'b, const N: usize, T, U: Clone> {
//~^ ERROR: rustc_dump_generics: DefId(0:3 ~ dump_generics_trait[89b1]::NiceOfThePrincess)

type ToInviteUs<V: PartialEq>;

fn over_for_a_picnic<const NN: usize>(
eh: [u8; N],
luigi: [u32; NN]
);
}

fn main() {}
82 changes: 82 additions & 0 deletions tests/ui/attributes/dump_generics_trait.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
error: rustc_dump_generics: DefId(..)
--> $DIR/dump_generics_trait.rs:6:1
|
LL | trait NiceOfThePrincess<'a, 'b, const N: usize, T, U: Clone> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: Generics {
parent: None,
parent_count: 0,
own_params: [
GenericParamDef {
name: "Self",
def_id: DefId(..),
index: 0,
pure_wrt_drop: false,
kind: Type {
has_default: false,
synthetic: false,
},
},
GenericParamDef {
name: "'a",
def_id: DefId(..),
index: 1,
pure_wrt_drop: false,
kind: Lifetime,
},
GenericParamDef {
name: "'b",
def_id: DefId(..),
index: 2,
pure_wrt_drop: false,
kind: Lifetime,
},
GenericParamDef {
name: "N",
def_id: DefId(..),
index: 3,
pure_wrt_drop: false,
kind: Const {
has_default: false,
},
},
GenericParamDef {
name: "T",
def_id: DefId(..),
index: 4,
pure_wrt_drop: false,
kind: Type {
has_default: false,
synthetic: false,
},
},
GenericParamDef {
name: "U",
def_id: DefId(..),
index: 5,
pure_wrt_drop: false,
kind: Type {
has_default: false,
synthetic: false,
},
},
],
param_def_id_to_index: {
DefId(..): 4,
DefId(..): 0,
DefId(..): 2,
DefId(..): 3,
DefId(..): 1,
DefId(..): 5,
},
has_self: true,
has_late_bound_regions: None,
}
--> $DIR/dump_generics_trait.rs:6:1
|
LL | trait NiceOfThePrincess<'a, 'b, const N: usize, T, U: Clone> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

Loading