From 555e024abcd8073d00939575a361417f24374443 Mon Sep 17 00:00:00 2001 From: marmeladema Date: Wed, 8 Apr 2020 15:09:38 +0100 Subject: [PATCH] librustc_middle: return LocalDefId instead of DefId in local_def_id_from_node_id --- src/librustc_middle/hir/map/mod.rs | 21 ++++++-------- src/librustc_save_analysis/dump_visitor.rs | 32 +++++++++++++-------- src/librustc_save_analysis/lib.rs | 33 +++++++++++++++------- src/librustdoc/clean/inline.rs | 6 +++- src/librustdoc/clean/mod.rs | 4 +-- 5 files changed, 59 insertions(+), 37 deletions(-) diff --git a/src/librustc_middle/hir/map/mod.rs b/src/librustc_middle/hir/map/mod.rs index c4180c3e9c7..6d6cbda5d5f 100644 --- a/src/librustc_middle/hir/map/mod.rs +++ b/src/librustc_middle/hir/map/mod.rs @@ -157,19 +157,16 @@ impl<'hir> Map<'hir> { self.tcx.definitions.def_path(def_id) } - // FIXME(eddyb) this function can and should return `LocalDefId`. #[inline] - pub fn local_def_id_from_node_id(&self, node: NodeId) -> DefId { - self.opt_local_def_id_from_node_id(node) - .unwrap_or_else(|| { - let hir_id = self.node_id_to_hir_id(node); - bug!( - "local_def_id_from_node_id: no entry for `{}`, which has a map of `{:?}`", - node, - self.find_entry(hir_id) - ) - }) - .to_def_id() + pub fn local_def_id_from_node_id(&self, node: NodeId) -> LocalDefId { + self.opt_local_def_id_from_node_id(node).unwrap_or_else(|| { + let hir_id = self.node_id_to_hir_id(node); + bug!( + "local_def_id_from_node_id: no entry for `{}`, which has a map of `{:?}`", + node, + self.find_entry(hir_id) + ) + }) } // FIXME(eddyb) this function can and should return `LocalDefId`. diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs index dc557fe3742..5b93c73e07c 100644 --- a/src/librustc_save_analysis/dump_visitor.rs +++ b/src/librustc_save_analysis/dump_visitor.rs @@ -107,7 +107,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { where F: FnOnce(&mut Self), { - let item_def_id = self.tcx.hir().local_def_id_from_node_id(item_id); + let item_def_id = self.tcx.hir().local_def_id_from_node_id(item_id).to_def_id(); let tables = if self.tcx.has_typeck_tables(item_def_id) { self.tcx.typeck_tables_of(item_def_id) @@ -423,8 +423,10 @@ 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))); + let qualname = format!( + "::{}", + self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(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); @@ -470,7 +472,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { 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)) + self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id).to_def_id()) ); let kind = match item.kind { @@ -670,7 +672,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { } 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)); + v.process_impl_item(impl_item, map.local_def_id_from_node_id(item.id).to_def_id()); } }); } @@ -685,7 +687,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { 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)) + self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id).to_def_id()) ); let mut val = name.clone(); if !generics.params.is_empty() { @@ -751,7 +753,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { self.process_generic_params(generics, &qualname, item.id); for method in methods { let map = &self.tcx.hir(); - self.process_trait_item(method, map.local_def_id_from_node_id(item.id)) + self.process_trait_item(method, map.local_def_id_from_node_id(item.id).to_def_id()) } } @@ -1030,7 +1032,9 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { let name = trait_item.ident.name.to_string(); let qualname = format!( "::{}", - self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(trait_item.id)) + self.tcx.def_path_str( + self.tcx.hir().local_def_id_from_node_id(trait_item.id).to_def_id() + ) ); if !self.span.filter_generated(trait_item.ident.span) { @@ -1173,7 +1177,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> { // Make a comma-separated list of names of imported modules. let def_id = self.tcx.hir().local_def_id_from_node_id(id); - let names = self.tcx.names_imported_by_glob_use(def_id); + let names = self.tcx.names_imported_by_glob_use(def_id.to_def_id()); let names: Vec<_> = names.iter().map(|n| n.to_string()).collect(); // Otherwise it's a span with wrong macro expansion info, which @@ -1227,8 +1231,10 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> { // only get called for the root module of a crate. assert_eq!(id, ast::CRATE_NODE_ID); - let qualname = - format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(id))); + let qualname = format!( + "::{}", + self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(id).to_def_id()) + ); let sm = self.tcx.sess.source_map(); let filename = sm.span_to_filename(span); @@ -1311,7 +1317,9 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> { TyAlias(_, ref ty_params, _, ref ty) => { let qualname = format!( "::{}", - self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id)) + self.tcx.def_path_str( + self.tcx.hir().local_def_id_from_node_id(item.id).to_def_id() + ) ); let value = match ty { Some(ty) => ty_to_string(&ty), diff --git a/src/librustc_save_analysis/lib.rs b/src/librustc_save_analysis/lib.rs index 717f3ac35e7..cb3032de83d 100644 --- a/src/librustc_save_analysis/lib.rs +++ b/src/librustc_save_analysis/lib.rs @@ -130,7 +130,7 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> { pub fn get_extern_item_data(&self, item: &ast::ForeignItem) -> Option { let qualname = format!( "::{}", - self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id)) + self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id).to_def_id()) ); match item.kind { ast::ForeignItemKind::Fn(_, ref sig, ref generics, _) => { @@ -183,7 +183,9 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> { ast::ItemKind::Fn(_, ref sig, .., ref generics, _) => { let qualname = format!( "::{}", - self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id)) + self.tcx.def_path_str( + self.tcx.hir().local_def_id_from_node_id(item.id).to_def_id() + ) ); filter!(self.span_utils, item.ident.span); Some(Data::DefData(Def { @@ -204,7 +206,9 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> { ast::ItemKind::Static(ref typ, ..) => { let qualname = format!( "::{}", - self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id)) + self.tcx.def_path_str( + self.tcx.hir().local_def_id_from_node_id(item.id).to_def_id() + ) ); filter!(self.span_utils, item.ident.span); @@ -230,7 +234,9 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> { ast::ItemKind::Const(_, ref typ, _) => { let qualname = format!( "::{}", - self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id)) + self.tcx.def_path_str( + self.tcx.hir().local_def_id_from_node_id(item.id).to_def_id() + ) ); filter!(self.span_utils, item.ident.span); @@ -255,7 +261,9 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> { ast::ItemKind::Mod(ref m) => { let qualname = format!( "::{}", - self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id)) + self.tcx.def_path_str( + self.tcx.hir().local_def_id_from_node_id(item.id).to_def_id() + ) ); let sm = self.tcx.sess.source_map(); @@ -282,7 +290,9 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> { 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)) + self.tcx.def_path_str( + self.tcx.hir().local_def_id_from_node_id(item.id).to_def_id() + ) ); filter!(self.span_utils, item.ident.span); let variants_str = @@ -363,11 +373,11 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> { let name = ident.to_string(); let qualname = format!( "::{}::{}", - self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(scope)), + self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(scope).to_def_id()), ident ); filter!(self.span_utils, ident.span); - let def_id = self.tcx.hir().local_def_id_from_node_id(field.id); + let def_id = self.tcx.hir().local_def_id_from_node_id(field.id).to_def_id(); let typ = self.tcx.type_of(def_id).to_string(); let id = id_from_node_id(field.id, self); @@ -399,7 +409,7 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> { // which the method is declared in, followed by the method's name. let (qualname, parent_scope, decl_id, docs, attributes) = match self .tcx - .impl_of_method(self.tcx.hir().local_def_id_from_node_id(id)) + .impl_of_method(self.tcx.hir().local_def_id_from_node_id(id).to_def_id()) { Some(impl_id) => match self.tcx.hir().get_if_local(impl_id) { Some(Node::Item(item)) => match item.kind { @@ -448,7 +458,10 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> { ); } }, - None => match self.tcx.trait_of_item(self.tcx.hir().local_def_id_from_node_id(id)) { + None => match self + .tcx + .trait_of_item(self.tcx.hir().local_def_id_from_node_id(id).to_def_id()) + { Some(def_id) => { let mut docs = String::new(); let mut attrs = vec![]; diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 0906d2f3845..a82015dea5f 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -451,7 +451,11 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet) name: None, attrs: clean::Attributes::default(), source: clean::Span::empty(), - def_id: cx.tcx.hir().local_def_id_from_node_id(ast::CRATE_NODE_ID), + def_id: cx + .tcx + .hir() + .local_def_id_from_node_id(ast::CRATE_NODE_ID) + .to_def_id(), visibility: clean::Public, stability: None, deprecation: None, diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 3ab906b807e..66df5eb45bb 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1554,7 +1554,7 @@ impl<'tcx> Clean for Ty<'tcx> { BareFunction(box BareFunctionDecl { unsafety: sig.unsafety(), generic_params: Vec::new(), - decl: (local_def_id, sig).clean(cx), + decl: (local_def_id.to_def_id(), sig).clean(cx), abi: sig.abi(), }) } @@ -2264,7 +2264,7 @@ impl Clean> for doctree::Import<'_> { name: None, attrs: self.attrs.clean(cx), source: self.whence.clean(cx), - def_id: cx.tcx.hir().local_def_id_from_node_id(ast::CRATE_NODE_ID), + def_id: cx.tcx.hir().local_def_id_from_node_id(ast::CRATE_NODE_ID).to_def_id(), visibility: self.vis.clean(cx), stability: None, deprecation: None,