Skip to content

Commit

Permalink
Use LocalDefId in DumpVisitor::nest_tables
Browse files Browse the repository at this point in the history
This is a partial fix for #71104
  • Loading branch information
marmeladema committed May 3, 2020
1 parent 7184d13 commit e5a1be8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 36 deletions.
70 changes: 41 additions & 29 deletions src/librustc_save_analysis/dump_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use rustc_ast::walk_list;
use rustc_ast_pretty::pprust::{bounds_to_string, generic_params_to_string, ty_to_string};
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::def::{DefKind as HirDefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::span_bug;
use rustc_middle::ty::{self, DefIdTree, TyCtxt};
use rustc_session::config::Input;
Expand Down Expand Up @@ -103,12 +103,10 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
self.dumper.analysis()
}

fn nest_tables<F>(&mut self, item_id: NodeId, f: F)
fn nest_tables<F>(&mut self, item_def_id: LocalDefId, f: F)
where
F: FnOnce(&mut Self),
{
let item_def_id = self.tcx.hir().local_def_id_from_node_id(item_id);

let tables = if self.tcx.has_typeck_tables(item_def_id) {
self.tcx.typeck_tables_of(item_def_id)
} else {
Expand Down Expand Up @@ -271,8 +269,9 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
) {
debug!("process_method: {}:{}", id, ident);

let hir_id = self.tcx.hir().node_id_to_hir_id(id);
self.nest_tables(id, |v| {
let map = &self.tcx.hir();
let hir_id = map.node_id_to_hir_id(id);
self.nest_tables(map.local_def_id(hir_id), |v| {
if let Some(mut method_data) = v.save_ctxt.get_method_data(id, ident, span) {
v.process_formals(&sig.decl.inputs, &method_data.qualname);
v.process_generic_params(&generics, &method_data.qualname, id);
Expand All @@ -295,7 +294,8 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
// start walking from the newly-created definition.
match sig.header.asyncness {
ast::Async::Yes { return_impl_trait_id, .. } => {
v.nest_tables(return_impl_trait_id, |v| v.visit_ty(ret_ty))
let hir_id = map.node_id_to_hir_id(return_impl_trait_id);
v.nest_tables(map.local_def_id(hir_id), |v| v.visit_ty(ret_ty))
}
_ => v.visit_ty(ret_ty),
}
Expand Down Expand Up @@ -363,8 +363,9 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
ty_params: &'l ast::Generics,
body: Option<&'l ast::Block>,
) {
let hir_id = self.tcx.hir().node_id_to_hir_id(item.id);
self.nest_tables(item.id, |v| {
let map = &self.tcx.hir();
let hir_id = map.node_id_to_hir_id(item.id);
self.nest_tables(map.local_def_id(hir_id), |v| {
if let Some(fn_data) = v.save_ctxt.get_item_data(item) {
down_cast_data!(fn_data, DefData, item.span);
v.process_formals(&decl.inputs, &fn_data.qualname);
Expand All @@ -388,7 +389,8 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
// start walking from the newly-created definition.
match header.asyncness {
ast::Async::Yes { return_impl_trait_id, .. } => {
v.nest_tables(return_impl_trait_id, |v| v.visit_ty(ret_ty))
let hir_id = map.node_id_to_hir_id(return_impl_trait_id);
v.nest_tables(map.local_def_id(hir_id), |v| v.visit_ty(ret_ty))
}
_ => v.visit_ty(ret_ty),
}
Expand All @@ -406,7 +408,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
expr: Option<&'l ast::Expr>,
) {
let hir_id = self.tcx.hir().node_id_to_hir_id(item.id);
self.nest_tables(item.id, |v| {
self.nest_tables(self.tcx.hir().local_def_id(hir_id), |v| {
if let Some(var_data) = v.save_ctxt.get_item_data(item) {
down_cast_data!(var_data, DefData, item.span);
v.dumper.dump_def(&access_from!(v.save_ctxt, item, hir_id), var_data);
Expand All @@ -426,15 +428,13 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
vis: ast::Visibility,
attrs: &'l [Attribute],
) {
let qualname = format!(
"::{}",
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(id).to_def_id())
);
let hir_id = self.tcx.hir().node_id_to_hir_id(id);
let qualname =
format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id(hir_id).to_def_id()));

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

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

// walk type and init value
self.nest_tables(id, |v| {
self.nest_tables(self.tcx.hir().local_def_id(hir_id), |v| {
v.visit_ty(typ);
if let Some(expr) = expr {
v.visit_expr(expr);
Expand All @@ -473,10 +473,9 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
) {
debug!("process_struct {:?} {:?}", item, item.span);
let name = item.ident.to_string();
let qualname = format!(
"::{}",
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id).to_def_id())
);
let hir_id = self.tcx.hir().node_id_to_hir_id(item.id);
let qualname =
format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id(hir_id).to_def_id()));

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

if !self.span.filter_generated(item.ident.span) {
let span = self.span_from_span(item.ident.span);
let hir_id = self.tcx.hir().node_id_to_hir_id(item.id);
self.dumper.dump_def(
&access_from!(self.save_ctxt, item, hir_id),
Def {
Expand All @@ -528,7 +526,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
);
}

self.nest_tables(item.id, |v| {
self.nest_tables(self.tcx.hir().local_def_id(hir_id), |v| {
for field in def.fields() {
v.process_struct_field_def(field, item.id);
v.visit_ty(&field.ty);
Expand Down Expand Up @@ -668,14 +666,15 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
}

let map = &self.tcx.hir();
self.nest_tables(item.id, |v| {
let hir_id = map.node_id_to_hir_id(item.id);
self.nest_tables(map.local_def_id(hir_id), |v| {
v.visit_ty(&typ);
if let &Some(ref trait_ref) = trait_ref {
v.process_path(trait_ref.ref_id, &trait_ref.path);
}
v.process_generic_params(generics, "", item.id);
for impl_item in impl_items {
v.process_impl_item(impl_item, map.local_def_id_from_node_id(item.id).to_def_id());
v.process_impl_item(impl_item, map.local_def_id(hir_id).to_def_id());
}
});
}
Expand Down Expand Up @@ -1410,7 +1409,10 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
}
ast::TyKind::Array(ref element, ref length) => {
self.visit_ty(element);
self.nest_tables(length.id, |v| v.visit_expr(&length.value));
let hir_id = self.tcx.hir().node_id_to_hir_id(length.id);
self.nest_tables(self.tcx.hir().local_def_id(hir_id), |v| {
v.visit_expr(&length.value)
});
}
ast::TyKind::ImplTrait(id, ref bounds) => {
// FIXME: As of writing, the opaque type lowering introduces
Expand All @@ -1422,7 +1424,13 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
// bounds...
// This will panic if called on return type `impl Trait`, which
// we guard against in `process_fn`.
self.nest_tables(id, |v| v.process_bounds(bounds));
// FIXME(#71104) Should really be using just `node_id_to_hir_id` but
// some `NodeId` do not seem to have a corresponding HirId.
if let Some(hir_id) = self.tcx.hir().opt_node_id_to_hir_id(id) {
self.nest_tables(self.tcx.hir().local_def_id(hir_id), |v| {
v.process_bounds(bounds)
});
}
}
_ => visit::walk_ty(self, t),
}
Expand Down Expand Up @@ -1470,7 +1478,8 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
}

// walk the body
self.nest_tables(ex.id, |v| {
let hir_id = self.tcx.hir().node_id_to_hir_id(ex.id);
self.nest_tables(self.tcx.hir().local_def_id(hir_id), |v| {
v.process_formals(&decl.inputs, &id);
v.visit_expr(body)
});
Expand All @@ -1487,7 +1496,10 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
}
ast::ExprKind::Repeat(ref element, ref count) => {
self.visit_expr(element);
self.nest_tables(count.id, |v| v.visit_expr(&count.value));
let hir_id = self.tcx.hir().node_id_to_hir_id(count.id);
self.nest_tables(self.tcx.hir().local_def_id(hir_id), |v| {
v.visit_expr(&count.value)
});
}
// In particular, we take this branch for call and path expressions,
// where we'll index the idents involved just by continuing to walk.
Expand Down
7 changes: 0 additions & 7 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,13 +831,6 @@ fn primary_body_of(
}

fn has_typeck_tables(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
// FIXME(#71104) some `LocalDefId` do not seem to have a corresponding `HirId`.
if let Some(def_id) = def_id.as_local() {
if tcx.hir().opt_local_def_id_to_hir_id(def_id).is_none() {
return false;
}
}

// Closures' tables come from their outermost function,
// as they are part of the same "inference environment".
let outer_def_id = tcx.closure_base_def_id(def_id);
Expand Down

0 comments on commit e5a1be8

Please sign in to comment.