Skip to content

Commit e5a1be8

Browse files
committed
Use LocalDefId in DumpVisitor::nest_tables
This is a partial fix for #71104
1 parent 7184d13 commit e5a1be8

File tree

2 files changed

+41
-36
lines changed

2 files changed

+41
-36
lines changed

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_ast::walk_list;
2121
use rustc_ast_pretty::pprust::{bounds_to_string, generic_params_to_string, ty_to_string};
2222
use rustc_data_structures::fx::FxHashSet;
2323
use rustc_hir::def::{DefKind as HirDefKind, Res};
24-
use rustc_hir::def_id::DefId;
24+
use rustc_hir::def_id::{DefId, LocalDefId};
2525
use rustc_middle::span_bug;
2626
use rustc_middle::ty::{self, DefIdTree, TyCtxt};
2727
use rustc_session::config::Input;
@@ -103,12 +103,10 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
103103
self.dumper.analysis()
104104
}
105105

106-
fn nest_tables<F>(&mut self, item_id: NodeId, f: F)
106+
fn nest_tables<F>(&mut self, item_def_id: LocalDefId, f: F)
107107
where
108108
F: FnOnce(&mut Self),
109109
{
110-
let item_def_id = self.tcx.hir().local_def_id_from_node_id(item_id);
111-
112110
let tables = if self.tcx.has_typeck_tables(item_def_id) {
113111
self.tcx.typeck_tables_of(item_def_id)
114112
} else {
@@ -271,8 +269,9 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
271269
) {
272270
debug!("process_method: {}:{}", id, ident);
273271

274-
let hir_id = self.tcx.hir().node_id_to_hir_id(id);
275-
self.nest_tables(id, |v| {
272+
let map = &self.tcx.hir();
273+
let hir_id = map.node_id_to_hir_id(id);
274+
self.nest_tables(map.local_def_id(hir_id), |v| {
276275
if let Some(mut method_data) = v.save_ctxt.get_method_data(id, ident, span) {
277276
v.process_formals(&sig.decl.inputs, &method_data.qualname);
278277
v.process_generic_params(&generics, &method_data.qualname, id);
@@ -295,7 +294,8 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
295294
// start walking from the newly-created definition.
296295
match sig.header.asyncness {
297296
ast::Async::Yes { return_impl_trait_id, .. } => {
298-
v.nest_tables(return_impl_trait_id, |v| v.visit_ty(ret_ty))
297+
let hir_id = map.node_id_to_hir_id(return_impl_trait_id);
298+
v.nest_tables(map.local_def_id(hir_id), |v| v.visit_ty(ret_ty))
299299
}
300300
_ => v.visit_ty(ret_ty),
301301
}
@@ -363,8 +363,9 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
363363
ty_params: &'l ast::Generics,
364364
body: Option<&'l ast::Block>,
365365
) {
366-
let hir_id = self.tcx.hir().node_id_to_hir_id(item.id);
367-
self.nest_tables(item.id, |v| {
366+
let map = &self.tcx.hir();
367+
let hir_id = map.node_id_to_hir_id(item.id);
368+
self.nest_tables(map.local_def_id(hir_id), |v| {
368369
if let Some(fn_data) = v.save_ctxt.get_item_data(item) {
369370
down_cast_data!(fn_data, DefData, item.span);
370371
v.process_formals(&decl.inputs, &fn_data.qualname);
@@ -388,7 +389,8 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
388389
// start walking from the newly-created definition.
389390
match header.asyncness {
390391
ast::Async::Yes { return_impl_trait_id, .. } => {
391-
v.nest_tables(return_impl_trait_id, |v| v.visit_ty(ret_ty))
392+
let hir_id = map.node_id_to_hir_id(return_impl_trait_id);
393+
v.nest_tables(map.local_def_id(hir_id), |v| v.visit_ty(ret_ty))
392394
}
393395
_ => v.visit_ty(ret_ty),
394396
}
@@ -406,7 +408,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
406408
expr: Option<&'l ast::Expr>,
407409
) {
408410
let hir_id = self.tcx.hir().node_id_to_hir_id(item.id);
409-
self.nest_tables(item.id, |v| {
411+
self.nest_tables(self.tcx.hir().local_def_id(hir_id), |v| {
410412
if let Some(var_data) = v.save_ctxt.get_item_data(item) {
411413
down_cast_data!(var_data, DefData, item.span);
412414
v.dumper.dump_def(&access_from!(v.save_ctxt, item, hir_id), var_data);
@@ -426,15 +428,13 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
426428
vis: ast::Visibility,
427429
attrs: &'l [Attribute],
428430
) {
429-
let qualname = format!(
430-
"::{}",
431-
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(id).to_def_id())
432-
);
431+
let hir_id = self.tcx.hir().node_id_to_hir_id(id);
432+
let qualname =
433+
format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id(hir_id).to_def_id()));
433434

434435
if !self.span.filter_generated(ident.span) {
435436
let sig = sig::assoc_const_signature(id, ident.name, typ, expr, &self.save_ctxt);
436437
let span = self.span_from_span(ident.span);
437-
let hir_id = self.tcx.hir().node_id_to_hir_id(id);
438438

439439
self.dumper.dump_def(
440440
&access_from_vis!(self.save_ctxt, vis, hir_id),
@@ -456,7 +456,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
456456
}
457457

458458
// walk type and init value
459-
self.nest_tables(id, |v| {
459+
self.nest_tables(self.tcx.hir().local_def_id(hir_id), |v| {
460460
v.visit_ty(typ);
461461
if let Some(expr) = expr {
462462
v.visit_expr(expr);
@@ -473,10 +473,9 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
473473
) {
474474
debug!("process_struct {:?} {:?}", item, item.span);
475475
let name = item.ident.to_string();
476-
let qualname = format!(
477-
"::{}",
478-
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id).to_def_id())
479-
);
476+
let hir_id = self.tcx.hir().node_id_to_hir_id(item.id);
477+
let qualname =
478+
format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id(hir_id).to_def_id()));
480479

481480
let kind = match item.kind {
482481
ast::ItemKind::Struct(_, _) => DefKind::Struct,
@@ -508,7 +507,6 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
508507

509508
if !self.span.filter_generated(item.ident.span) {
510509
let span = self.span_from_span(item.ident.span);
511-
let hir_id = self.tcx.hir().node_id_to_hir_id(item.id);
512510
self.dumper.dump_def(
513511
&access_from!(self.save_ctxt, item, hir_id),
514512
Def {
@@ -528,7 +526,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
528526
);
529527
}
530528

531-
self.nest_tables(item.id, |v| {
529+
self.nest_tables(self.tcx.hir().local_def_id(hir_id), |v| {
532530
for field in def.fields() {
533531
v.process_struct_field_def(field, item.id);
534532
v.visit_ty(&field.ty);
@@ -668,14 +666,15 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
668666
}
669667

670668
let map = &self.tcx.hir();
671-
self.nest_tables(item.id, |v| {
669+
let hir_id = map.node_id_to_hir_id(item.id);
670+
self.nest_tables(map.local_def_id(hir_id), |v| {
672671
v.visit_ty(&typ);
673672
if let &Some(ref trait_ref) = trait_ref {
674673
v.process_path(trait_ref.ref_id, &trait_ref.path);
675674
}
676675
v.process_generic_params(generics, "", item.id);
677676
for impl_item in impl_items {
678-
v.process_impl_item(impl_item, map.local_def_id_from_node_id(item.id).to_def_id());
677+
v.process_impl_item(impl_item, map.local_def_id(hir_id).to_def_id());
679678
}
680679
});
681680
}
@@ -1410,7 +1409,10 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
14101409
}
14111410
ast::TyKind::Array(ref element, ref length) => {
14121411
self.visit_ty(element);
1413-
self.nest_tables(length.id, |v| v.visit_expr(&length.value));
1412+
let hir_id = self.tcx.hir().node_id_to_hir_id(length.id);
1413+
self.nest_tables(self.tcx.hir().local_def_id(hir_id), |v| {
1414+
v.visit_expr(&length.value)
1415+
});
14141416
}
14151417
ast::TyKind::ImplTrait(id, ref bounds) => {
14161418
// FIXME: As of writing, the opaque type lowering introduces
@@ -1422,7 +1424,13 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
14221424
// bounds...
14231425
// This will panic if called on return type `impl Trait`, which
14241426
// we guard against in `process_fn`.
1425-
self.nest_tables(id, |v| v.process_bounds(bounds));
1427+
// FIXME(#71104) Should really be using just `node_id_to_hir_id` but
1428+
// some `NodeId` do not seem to have a corresponding HirId.
1429+
if let Some(hir_id) = self.tcx.hir().opt_node_id_to_hir_id(id) {
1430+
self.nest_tables(self.tcx.hir().local_def_id(hir_id), |v| {
1431+
v.process_bounds(bounds)
1432+
});
1433+
}
14261434
}
14271435
_ => visit::walk_ty(self, t),
14281436
}
@@ -1470,7 +1478,8 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
14701478
}
14711479

14721480
// walk the body
1473-
self.nest_tables(ex.id, |v| {
1481+
let hir_id = self.tcx.hir().node_id_to_hir_id(ex.id);
1482+
self.nest_tables(self.tcx.hir().local_def_id(hir_id), |v| {
14741483
v.process_formals(&decl.inputs, &id);
14751484
v.visit_expr(body)
14761485
});
@@ -1487,7 +1496,10 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
14871496
}
14881497
ast::ExprKind::Repeat(ref element, ref count) => {
14891498
self.visit_expr(element);
1490-
self.nest_tables(count.id, |v| v.visit_expr(&count.value));
1499+
let hir_id = self.tcx.hir().node_id_to_hir_id(count.id);
1500+
self.nest_tables(self.tcx.hir().local_def_id(hir_id), |v| {
1501+
v.visit_expr(&count.value)
1502+
});
14911503
}
14921504
// In particular, we take this branch for call and path expressions,
14931505
// where we'll index the idents involved just by continuing to walk.

src/librustc_typeck/check/mod.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -831,13 +831,6 @@ fn primary_body_of(
831831
}
832832

833833
fn has_typeck_tables(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
834-
// FIXME(#71104) some `LocalDefId` do not seem to have a corresponding `HirId`.
835-
if let Some(def_id) = def_id.as_local() {
836-
if tcx.hir().opt_local_def_id_to_hir_id(def_id).is_none() {
837-
return false;
838-
}
839-
}
840-
841834
// Closures' tables come from their outermost function,
842835
// as they are part of the same "inference environment".
843836
let outer_def_id = tcx.closure_base_def_id(def_id);

0 commit comments

Comments
 (0)