Skip to content

Commit 5527944

Browse files
committed
add rustc_dump_def_parents attribute
1 parent 6c34855 commit 5527944

File tree

6 files changed

+217
-1
lines changed

6 files changed

+217
-1
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
11171117
TEST, rustc_dump_predicates, Normal, template!(Word),
11181118
WarnFollowing, EncodeCrossCrate::No
11191119
),
1120+
rustc_attr!(
1121+
TEST, rustc_dump_def_parents, Normal, template!(Word),
1122+
WarnFollowing, EncodeCrossCrate::No
1123+
),
11201124
rustc_attr!(
11211125
TEST, rustc_object_lifetime_default, Normal, template!(Word),
11221126
WarnFollowing, EncodeCrossCrate::No

compiler/rustc_hir_analysis/src/collect/dump.rs

+49-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use rustc_hir::def::DefKind;
2-
use rustc_hir::def_id::CRATE_DEF_ID;
2+
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
3+
use rustc_hir::intravisit;
4+
use rustc_middle::hir::nested_filter::OnlyBodies;
35
use rustc_middle::ty::TyCtxt;
46
use rustc_span::sym;
57

@@ -41,3 +43,49 @@ pub(crate) fn predicates_and_item_bounds(tcx: TyCtxt<'_>) {
4143
}
4244
}
4345
}
46+
47+
pub(crate) fn def_parents(tcx: TyCtxt<'_>) {
48+
for did in tcx.hir().body_owners() {
49+
if tcx.has_attr(did, sym::rustc_dump_def_parents) {
50+
struct AnonConstFinder<'tcx> {
51+
tcx: TyCtxt<'tcx>,
52+
anon_consts: Vec<LocalDefId>,
53+
}
54+
55+
impl<'tcx> intravisit::Visitor<'tcx> for AnonConstFinder<'tcx> {
56+
type NestedFilter = OnlyBodies;
57+
58+
fn nested_visit_map(&mut self) -> Self::Map {
59+
self.tcx.hir()
60+
}
61+
62+
fn visit_anon_const(&mut self, c: &'tcx rustc_hir::AnonConst) {
63+
self.anon_consts.push(c.def_id);
64+
intravisit::walk_anon_const(self, c)
65+
}
66+
}
67+
68+
// Look for any anon consts inside of this body owner as there is no way to apply
69+
// the `rustc_dump_def_parents` attribute to the anon const so it would not be possible
70+
// to see what its def parent is.
71+
let mut anon_ct_finder = AnonConstFinder { tcx, anon_consts: vec![] };
72+
intravisit::walk_expr(&mut anon_ct_finder, tcx.hir().body_owned_by(did).value);
73+
74+
for did in [did].into_iter().chain(anon_ct_finder.anon_consts) {
75+
let span = tcx.def_span(did);
76+
77+
let mut diag = tcx.dcx().struct_span_err(
78+
span,
79+
format!("{}: {did:?}", sym::rustc_dump_def_parents.as_str()),
80+
);
81+
82+
let mut current_did = did.to_def_id();
83+
while let Some(parent_did) = tcx.opt_parent(current_did) {
84+
current_did = parent_did;
85+
diag.span_note(tcx.def_span(parent_did), format!("{parent_did:?}"));
86+
}
87+
diag.emit();
88+
}
89+
}
90+
}
91+
}

compiler/rustc_hir_analysis/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
169169
tcx.sess.time("variance_dumping", || variance::dump::variances(tcx));
170170
collect::dump::opaque_hidden_types(tcx);
171171
collect::dump::predicates_and_item_bounds(tcx);
172+
collect::dump::def_parents(tcx);
172173
}
173174

174175
// Make sure we evaluate all static and (non-associated) const items, even if unused.

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,7 @@ symbols! {
16141614
rustc_do_not_const_check,
16151615
rustc_doc_primitive,
16161616
rustc_dummy,
1617+
rustc_dump_def_parents,
16171618
rustc_dump_item_bounds,
16181619
rustc_dump_predicates,
16191620
rustc_dump_user_args,
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//@ normalize-stderr-test "DefId\(.+?\)" -> "DefId(..)"
2+
#![feature(rustc_attrs)]
3+
4+
fn bar() {
5+
fn foo() {
6+
fn baz() {
7+
#[rustc_dump_def_parents]
8+
|| {
9+
//~^ ERROR: rustc_dump_def_parents: DefId
10+
qux::<
11+
{
12+
//~^ ERROR: rustc_dump_def_parents: DefId
13+
fn inhibits_dump() {
14+
qux::<
15+
{
16+
"hi";
17+
1
18+
},
19+
>();
20+
}
21+
22+
qux::<{ 1 + 1 }>();
23+
//~^ ERROR: rustc_dump_def_parents: DefId
24+
1
25+
},
26+
>();
27+
};
28+
}
29+
}
30+
}
31+
32+
const fn qux<const N: usize>() {}
33+
34+
fn main() {}
+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
error: rustc_dump_def_parents: DefId(..)
2+
--> $DIR/dump_def_parents.rs:8:13
3+
|
4+
LL | || {
5+
| ^^
6+
|
7+
note: DefId(..)
8+
--> $DIR/dump_def_parents.rs:6:9
9+
|
10+
LL | fn baz() {
11+
| ^^^^^^^^
12+
note: DefId(..)
13+
--> $DIR/dump_def_parents.rs:5:5
14+
|
15+
LL | fn foo() {
16+
| ^^^^^^^^
17+
note: DefId(..)
18+
--> $DIR/dump_def_parents.rs:4:1
19+
|
20+
LL | fn bar() {
21+
| ^^^^^^^^
22+
note: DefId(..)
23+
--> $DIR/dump_def_parents.rs:2:1
24+
|
25+
LL | / #![feature(rustc_attrs)]
26+
LL | |
27+
LL | | fn bar() {
28+
LL | | fn foo() {
29+
... |
30+
LL | |
31+
LL | | fn main() {}
32+
| |____________^
33+
34+
error: rustc_dump_def_parents: DefId(..)
35+
--> $DIR/dump_def_parents.rs:11:21
36+
|
37+
LL | / {
38+
LL | |
39+
LL | | fn inhibits_dump() {
40+
LL | | qux::<
41+
... |
42+
LL | | 1
43+
LL | | },
44+
| |_____________________^
45+
|
46+
note: DefId(..)
47+
--> $DIR/dump_def_parents.rs:8:13
48+
|
49+
LL | || {
50+
| ^^
51+
note: DefId(..)
52+
--> $DIR/dump_def_parents.rs:6:9
53+
|
54+
LL | fn baz() {
55+
| ^^^^^^^^
56+
note: DefId(..)
57+
--> $DIR/dump_def_parents.rs:5:5
58+
|
59+
LL | fn foo() {
60+
| ^^^^^^^^
61+
note: DefId(..)
62+
--> $DIR/dump_def_parents.rs:4:1
63+
|
64+
LL | fn bar() {
65+
| ^^^^^^^^
66+
note: DefId(..)
67+
--> $DIR/dump_def_parents.rs:2:1
68+
|
69+
LL | / #![feature(rustc_attrs)]
70+
LL | |
71+
LL | | fn bar() {
72+
LL | | fn foo() {
73+
... |
74+
LL | |
75+
LL | | fn main() {}
76+
| |____________^
77+
78+
error: rustc_dump_def_parents: DefId(..)
79+
--> $DIR/dump_def_parents.rs:22:31
80+
|
81+
LL | qux::<{ 1 + 1 }>();
82+
| ^^^^^^^^^
83+
|
84+
note: DefId(..)
85+
--> $DIR/dump_def_parents.rs:11:21
86+
|
87+
LL | / {
88+
LL | |
89+
LL | | fn inhibits_dump() {
90+
LL | | qux::<
91+
... |
92+
LL | | 1
93+
LL | | },
94+
| |_____________________^
95+
note: DefId(..)
96+
--> $DIR/dump_def_parents.rs:8:13
97+
|
98+
LL | || {
99+
| ^^
100+
note: DefId(..)
101+
--> $DIR/dump_def_parents.rs:6:9
102+
|
103+
LL | fn baz() {
104+
| ^^^^^^^^
105+
note: DefId(..)
106+
--> $DIR/dump_def_parents.rs:5:5
107+
|
108+
LL | fn foo() {
109+
| ^^^^^^^^
110+
note: DefId(..)
111+
--> $DIR/dump_def_parents.rs:4:1
112+
|
113+
LL | fn bar() {
114+
| ^^^^^^^^
115+
note: DefId(..)
116+
--> $DIR/dump_def_parents.rs:2:1
117+
|
118+
LL | / #![feature(rustc_attrs)]
119+
LL | |
120+
LL | | fn bar() {
121+
LL | | fn foo() {
122+
... |
123+
LL | |
124+
LL | | fn main() {}
125+
| |____________^
126+
127+
error: aborting due to 3 previous errors
128+

0 commit comments

Comments
 (0)