Skip to content

Commit a36ce9b

Browse files
committed
Visit closures and opaque types in rustc_dump_def_parents
1 parent a146e1d commit a36ce9b

File tree

3 files changed

+68
-14
lines changed

3 files changed

+68
-14
lines changed

compiler/rustc_hir_analysis/src/collect/dump.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_hir::def::DefKind;
22
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
33
use rustc_hir::intravisit;
4-
use rustc_middle::hir::nested_filter::OnlyBodies;
4+
use rustc_middle::hir::nested_filter;
55
use rustc_middle::ty::TyCtxt;
66
use rustc_span::sym;
77

@@ -47,31 +47,42 @@ pub(crate) fn predicates_and_item_bounds(tcx: TyCtxt<'_>) {
4747
pub(crate) fn def_parents(tcx: TyCtxt<'_>) {
4848
for did in tcx.hir().body_owners() {
4949
if tcx.has_attr(did, sym::rustc_dump_def_parents) {
50-
struct AnonConstFinder<'tcx> {
50+
struct ExprDefFinder<'tcx> {
5151
tcx: TyCtxt<'tcx>,
52-
anon_consts: Vec<LocalDefId>,
52+
defs: Vec<LocalDefId>,
5353
}
5454

55-
impl<'tcx> intravisit::Visitor<'tcx> for AnonConstFinder<'tcx> {
56-
type NestedFilter = OnlyBodies;
55+
impl<'tcx> intravisit::Visitor<'tcx> for ExprDefFinder<'tcx> {
56+
type NestedFilter = nested_filter::All;
5757

5858
fn nested_visit_map(&mut self) -> Self::Map {
5959
self.tcx.hir()
6060
}
6161

6262
fn visit_anon_const(&mut self, c: &'tcx rustc_hir::AnonConst) {
63-
self.anon_consts.push(c.def_id);
63+
self.defs.push(c.def_id);
6464
intravisit::walk_anon_const(self, c)
6565
}
66+
67+
fn visit_expr(&mut self, ex: &'tcx rustc_hir::Expr<'tcx>) -> Self::Result {
68+
match &ex.kind {
69+
rustc_hir::ExprKind::Closure(closure) => self.defs.push(closure.def_id),
70+
_ => {}
71+
}
72+
intravisit::walk_expr(self, ex)
73+
}
6674
}
6775

6876
// Look for any anon consts inside of this body owner as there is no way to apply
6977
// the `rustc_dump_def_parents` attribute to the anon const so it would not be possible
7078
// 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);
79+
let mut expr_def_finder = ExprDefFinder { tcx, defs: vec![] };
80+
tcx.hir()
81+
.fn_decl_by_hir_id(tcx.local_def_id_to_hir_id(did))
82+
.map(|decl| intravisit::walk_fn_decl(&mut expr_def_finder, decl));
83+
intravisit::walk_expr(&mut expr_def_finder, tcx.hir().body_owned_by(did).value);
7384

74-
for did in [did].into_iter().chain(anon_ct_finder.anon_consts) {
85+
for did in [did].into_iter().chain(expr_def_finder.defs) {
7586
let span = tcx.def_span(did);
7687

7788
let mut diag = tcx.dcx().struct_span_err(

tests/ui/attributes/dump_def_parents.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ fn bar() {
1010
qux::<
1111
{
1212
//~^ ERROR: rustc_dump_def_parents: DefId
13-
fn inhibits_dump() {
13+
fn doesnt_inhibit_dump() {
1414
qux::<
1515
{
16+
//~^ ERROR: rustc_dump_def_parents: DefId
1617
"hi";
1718
1
1819
},

tests/ui/attributes/dump_def_parents.stderr

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ error: rustc_dump_def_parents: DefId(..)
4242
|
4343
LL | / {
4444
LL | |
45-
LL | | fn inhibits_dump() {
45+
LL | | fn doesnt_inhibit_dump() {
4646
LL | | qux::<
4747
... |
4848
LL | | 1
@@ -88,7 +88,49 @@ LL | | fn main() {}
8888
| |____________^
8989

9090
error: rustc_dump_def_parents: DefId(..)
91-
--> $DIR/dump_def_parents.rs:22:31
91+
--> $DIR/dump_def_parents.rs:15:33
92+
|
93+
LL | / ... {
94+
LL | | ...
95+
LL | | ... "hi";
96+
LL | | ... 1
97+
LL | | ... },
98+
| |_______________________^
99+
|
100+
note: DefId(..)
101+
--> $DIR/dump_def_parents.rs:13:25
102+
|
103+
LL | fn doesnt_inhibit_dump() {
104+
| ^^^^^^^^^^^^^^^^^^^^^^^^
105+
note: DefId(..)
106+
--> $DIR/dump_def_parents.rs:6:9
107+
|
108+
LL | fn baz() {
109+
| ^^^^^^^^
110+
note: DefId(..)
111+
--> $DIR/dump_def_parents.rs:5:5
112+
|
113+
LL | fn foo() {
114+
| ^^^^^^^^
115+
note: DefId(..)
116+
--> $DIR/dump_def_parents.rs:4:1
117+
|
118+
LL | fn bar() {
119+
| ^^^^^^^^
120+
note: DefId(..)
121+
--> $DIR/dump_def_parents.rs:2:1
122+
|
123+
LL | / #![feature(rustc_attrs)]
124+
LL | |
125+
LL | | fn bar() {
126+
LL | | fn foo() {
127+
... |
128+
LL | |
129+
LL | | fn main() {}
130+
| |____________^
131+
132+
error: rustc_dump_def_parents: DefId(..)
133+
--> $DIR/dump_def_parents.rs:23:31
92134
|
93135
LL | qux::<{ 1 + 1 }>();
94136
| ^^^^^^^^^
@@ -98,7 +140,7 @@ note: DefId(..)
98140
|
99141
LL | / {
100142
LL | |
101-
LL | | fn inhibits_dump() {
143+
LL | | fn doesnt_inhibit_dump() {
102144
LL | | qux::<
103145
... |
104146
LL | | 1
@@ -142,5 +184,5 @@ LL | |
142184
LL | | fn main() {}
143185
| |____________^
144186

145-
error: aborting due to 3 previous errors
187+
error: aborting due to 4 previous errors
146188

0 commit comments

Comments
 (0)