From dadbaa48ac5a86810aea17accd2f85194dc8cfd3 Mon Sep 17 00:00:00 2001 From: Eduard Burtescu Date: Sat, 17 Sep 2016 13:34:55 +0300 Subject: [PATCH] rustc_metadata: move opt_item_name to TyCtxt::item_name. --- src/librustc/hir/map/definitions.rs | 26 +++++++++++++- src/librustc/middle/cstore.rs | 4 +-- src/librustc/ty/item_path.rs | 3 +- src/librustc/ty/mod.rs | 21 ++++++++--- src/librustc_metadata/csearch.rs | 10 ------ src/librustc_metadata/decoder.rs | 39 +++------------------ src/librustc_resolve/build_reduced_graph.rs | 3 +- 7 files changed, 51 insertions(+), 55 deletions(-) diff --git a/src/librustc/hir/map/definitions.rs b/src/librustc/hir/map/definitions.rs index c0c28939ab27..af751e51d931 100644 --- a/src/librustc/hir/map/definitions.rs +++ b/src/librustc/hir/map/definitions.rs @@ -14,7 +14,7 @@ use rustc_data_structures::fnv::FnvHashMap; use std::fmt::Write; use std::hash::{Hash, Hasher, SipHasher}; use syntax::{ast, visit}; -use syntax::parse::token::InternedString; +use syntax::parse::token::{self, InternedString}; use ty::TyCtxt; use util::nodemap::NodeMap; @@ -326,6 +326,30 @@ impl Definitions { } impl DefPathData { + pub fn get_opt_name(&self) -> Option { + use self::DefPathData::*; + match *self { + TypeNs(ref name) | + ValueNs(ref name) | + Module(ref name) | + MacroDef(ref name) | + TypeParam(ref name) | + LifetimeDef(ref name) | + EnumVariant(ref name) | + Binding(ref name) | + Field(ref name) => Some(token::intern(name)), + + Impl | + CrateRoot | + InlinedRoot(_) | + Misc | + ClosureExpr | + StructCtor | + Initializer | + ImplTrait => None + } + } + pub fn as_interned_str(&self) -> InternedString { use self::DefPathData::*; match *self { diff --git a/src/librustc/middle/cstore.rs b/src/librustc/middle/cstore.rs index 658825d417e4..2ebf7ba6d53d 100644 --- a/src/librustc/middle/cstore.rs +++ b/src/librustc/middle/cstore.rs @@ -132,7 +132,6 @@ pub trait CrateStore<'tcx> { fn item_type<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> Ty<'tcx>; fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap>; - fn opt_item_name(&self, def: DefId) -> Option; fn item_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::GenericPredicates<'tcx>; fn item_super_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) @@ -293,7 +292,6 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore { fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap> { bug!("visible_parent_map") } - fn opt_item_name(&self, def: DefId) -> Option { bug!("opt_item_name") } fn item_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::GenericPredicates<'tcx> { bug!("item_predicates") } fn item_super_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) @@ -424,4 +422,4 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore { pub trait MacroLoader { fn load_crate(&mut self, extern_crate: &ast::Item, allows_macros: bool) -> Vec; -} \ No newline at end of file +} diff --git a/src/librustc/ty/item_path.rs b/src/librustc/ty/item_path.rs index 5f121b568c3a..ca12dde73b84 100644 --- a/src/librustc/ty/item_path.rs +++ b/src/librustc/ty/item_path.rs @@ -138,7 +138,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { } } - cur_path.push(self.sess.cstore.opt_item_name(cur_def).unwrap_or_else(|| + cur_path.push(self.sess.cstore.def_key(cur_def) + .disambiguated_data.data.get_opt_name().unwrap_or_else(|| token::intern(""))); match visible_parent_map.get(&cur_def) { Some(&def) => cur_def = def, diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index 8a9b2846ac66..d7076ddf0445 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -21,7 +21,7 @@ use dep_graph::{self, DepNode}; use hir::map as ast_map; use middle; use hir::def::{Def, PathResolution, ExportMap}; -use hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; +use hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE}; use middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem}; use middle::region::{CodeExtent, ROOT_CODE_EXTENT}; use traits; @@ -43,7 +43,7 @@ use std::slice; use std::vec::IntoIter; use syntax::ast::{self, Name, NodeId}; use syntax::attr; -use syntax::parse::token::InternedString; +use syntax::parse::token::{self, InternedString}; use syntax_pos::{DUMMY_SP, Span}; use rustc_const_math::ConstInt; @@ -2390,10 +2390,21 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { pub fn item_name(self, id: DefId) -> ast::Name { if let Some(id) = self.map.as_local_node_id(id) { self.map.name(id) + } else if id.index == CRATE_DEF_INDEX { + token::intern(&self.sess.cstore.original_crate_name(id.krate)) } else { - self.sess.cstore.opt_item_name(id).unwrap_or_else(|| { - bug!("item_name: no name for {:?}", self.def_path(id)); - }) + let def_key = self.sess.cstore.def_key(id); + // The name of a StructCtor is that of its struct parent. + if let ast_map::DefPathData::StructCtor = def_key.disambiguated_data.data { + self.item_name(DefId { + krate: id.krate, + index: def_key.parent.unwrap() + }) + } else { + def_key.disambiguated_data.data.get_opt_name().unwrap_or_else(|| { + bug!("item_name: no name for {:?}", self.def_path(id)); + }) + } } } diff --git a/src/librustc_metadata/csearch.rs b/src/librustc_metadata/csearch.rs index f508c5dc9cfb..7013720224a6 100644 --- a/src/librustc_metadata/csearch.rs +++ b/src/librustc_metadata/csearch.rs @@ -127,16 +127,6 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore { self.get_crate_data(did.krate).get_fn_arg_names(did.index) } - fn opt_item_name(&self, def: DefId) -> Option { - self.dep_graph.read(DepNode::MetaData(def)); - let cdata = self.get_crate_data(def.krate); - if def.index == CRATE_DEF_INDEX { - Some(token::intern(&cdata.name())) - } else { - cdata.maybe_get_item_name(def.index) - } - } - fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec { self.dep_graph.read(DepNode::MetaData(def_id)); diff --git a/src/librustc_metadata/decoder.rs b/src/librustc_metadata/decoder.rs index 973ef6030a3e..d98e8d62c2d1 100644 --- a/src/librustc_metadata/decoder.rs +++ b/src/librustc_metadata/decoder.rs @@ -43,7 +43,6 @@ use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque}; use syntax::attr; use syntax::ast::{self, NodeId}; use syntax::codemap; -use syntax::parse::token; use syntax_pos::{self, Span, BytePos, Pos}; pub struct DecodeContext<'a, 'tcx: 'a> { @@ -469,32 +468,6 @@ impl<'tcx> EntryKind<'tcx> { } } -fn def_key_name(def_key: &hir_map::DefKey) -> Option { - match def_key.disambiguated_data.data { - DefPathData::TypeNs(ref name) | - DefPathData::ValueNs(ref name) | - DefPathData::Module(ref name) | - DefPathData::MacroDef(ref name) | - DefPathData::TypeParam(ref name) | - DefPathData::LifetimeDef(ref name) | - DefPathData::EnumVariant(ref name) | - DefPathData::Field(ref name) | - DefPathData::Binding(ref name) => { - Some(token::intern(name)) - } - - DefPathData::InlinedRoot(_) => bug!("unexpected DefPathData"), - - DefPathData::CrateRoot | - DefPathData::Misc | - DefPathData::Impl | - DefPathData::ClosureExpr | - DefPathData::StructCtor | - DefPathData::Initializer | - DefPathData::ImplTrait => None - } -} - impl<'a, 'tcx> CrateMetadata { fn maybe_entry(&self, item_id: DefIndex) -> Option>> { self.root.index.lookup(self.blob.raw_bytes(), item_id) @@ -518,7 +491,8 @@ impl<'a, 'tcx> CrateMetadata { } fn item_name(&self, item: &Entry<'tcx>) -> ast::Name { - def_key_name(&item.def_key.decode(self)).expect("no name in item_name") + item.def_key.decode(self).disambiguated_data.data.get_opt_name() + .expect("no name in item_name") } pub fn get_def(&self, index: DefIndex) -> Option { @@ -708,7 +682,8 @@ impl<'a, 'tcx> CrateMetadata { _ => {} } - if let Some(name) = def_key_name(&child.def_key.decode(self)) { + let def_key = child.def_key.decode(self); + if let Some(name) = def_key.disambiguated_data.data.get_opt_name() { callback(def::Export { def_id: self.local_def_id(child_index), name: name @@ -724,10 +699,6 @@ impl<'a, 'tcx> CrateMetadata { } } - pub fn maybe_get_item_name(&self, id: DefIndex) -> Option { - def_key_name(&self.entry(id).def_key.decode(self)) - } - pub fn maybe_get_item_ast(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, id: DefIndex) -> Option<&'tcx InlinedItem> { debug!("Looking up item: {:?}", id); @@ -757,7 +728,7 @@ impl<'a, 'tcx> CrateMetadata { let parent_and_name = || { let def_key = item.def_key.decode(self); (self.local_def_id(def_key.parent.unwrap()), - def_key_name(&def_key).unwrap()) + def_key.disambiguated_data.data.get_opt_name().unwrap()) }; Some(match item.kind { diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index 1714398f2fed..3c5e9e6cd3a9 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -436,7 +436,8 @@ impl<'b> Resolver<'b> { let trait_item_def_ids = self.session.cstore.impl_or_trait_items(def_id); for &trait_item_def in &trait_item_def_ids { let trait_item_name = - self.session.cstore.opt_item_name(trait_item_def) + self.session.cstore.def_key(trait_item_def) + .disambiguated_data.data.get_opt_name() .expect("opt_item_name returned None for trait"); debug!("(building reduced graph for external crate) ... adding trait item \