]> git.lizzy.rs Git - rust.git/commitdiff
rustc_metadata: move opt_item_name to TyCtxt::item_name.
authorEduard Burtescu <edy.burt@gmail.com>
Sat, 17 Sep 2016 10:34:55 +0000 (13:34 +0300)
committerEduard Burtescu <edy.burt@gmail.com>
Tue, 20 Sep 2016 17:08:06 +0000 (20:08 +0300)
src/librustc/hir/map/definitions.rs
src/librustc/middle/cstore.rs
src/librustc/ty/item_path.rs
src/librustc/ty/mod.rs
src/librustc_metadata/csearch.rs
src/librustc_metadata/decoder.rs
src/librustc_resolve/build_reduced_graph.rs

index c0c28939ab27f6f0d4cb9fe0d4d564041d6d1d2c..af751e51d931e1aff09c9b5aa92fb23cc75c2aa6 100644 (file)
@@ -14,7 +14,7 @@
 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 @@ pub fn create_def_with_parent(&mut self,
 }
 
 impl DefPathData {
+    pub fn get_opt_name(&self) -> Option<ast::Name> {
+        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 {
index 658825d417e4ca3cd4b3249977fa720c906eac4a..2ebf7ba6d53d24f740b312b79d640d9a721ba244 100644 (file)
@@ -132,7 +132,6 @@ fn closure_ty<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
     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<DefId>>;
-    fn opt_item_name(&self, def: DefId) -> Option<ast::Name>;
     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 @@ fn item_type<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
     fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap<DefId>> {
         bug!("visible_parent_map")
     }
-    fn opt_item_name(&self, def: DefId) -> Option<ast::Name> { 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 @@ fn metadata_encoding_version(&self) -> &[u8] { bug!("metadata_encoding_version")
 
 pub trait MacroLoader {
      fn load_crate(&mut self, extern_crate: &ast::Item, allows_macros: bool) -> Vec<LoadedMacro>;
-}
\ No newline at end of file
+}
index 5f121b568c3af7ccd56037a02fe3ff260db3d86c..ca12dde73b84b3316adc67a2a2397028c95c56c6 100644 (file)
@@ -138,7 +138,8 @@ pub fn try_push_visible_item_path<T>(self, buffer: &mut T, external_def_id: DefI
                 }
             }
 
-            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("<unnamed>")));
             match visible_parent_map.get(&cur_def) {
                 Some(&def) => cur_def = def,
index 8a9b2846ac666487efd2a6469fa0a34bf06d2ba4..d7076ddf0445d33b0d3dfd73fceefdadc6dac685 100644 (file)
@@ -21,7 +21,7 @@
 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::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 @@ pub fn opt_def_path(self, id: DefId) -> Option<ast_map::DefPath> {
     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));
+                })
+            }
         }
     }
 
index f508c5dc9cfb0f85e516340cd841df1d99aabc15..7013720224a6407b507d31c09a74041bad4bf2dc 100644 (file)
@@ -127,16 +127,6 @@ fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name>
         self.get_crate_data(did.krate).get_fn_arg_names(did.index)
     }
 
-    fn opt_item_name(&self, def: DefId) -> Option<ast::Name> {
-        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<DefId>
     {
         self.dep_graph.read(DepNode::MetaData(def_id));
index 973ef6030a3e98820a7fd8fc65e9237c30977d5b..d98e8d62c2d157e3dc8d152d94c1fc37ba7de9d2 100644 (file)
@@ -43,7 +43,6 @@
 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 @@ fn to_def(&self, did: DefId) -> Option<Def> {
     }
 }
 
-fn def_key_name(def_key: &hir_map::DefKey) -> Option<ast::Name> {
-    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<Lazy<Entry<'tcx>>> {
         self.root.index.lookup(self.blob.raw_bytes(), item_id)
@@ -518,7 +491,8 @@ fn local_def_id(&self, index: DefIndex) -> DefId {
     }
 
     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<Def> {
@@ -708,7 +682,8 @@ pub fn each_child_of_item<F>(&self, id: DefIndex, mut callback: F)
                     _ => {}
                 }
 
-                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 @@ pub fn each_child_of_item<F>(&self, id: DefIndex, mut callback: F)
         }
     }
 
-    pub fn maybe_get_item_name(&self, id: DefIndex) -> Option<ast::Name> {
-        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 @@ pub fn get_impl_or_trait_item(&self, id: DefIndex, tcx: TyCtxt<'a, 'tcx, 'tcx>)
         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 {
index 1714398f2fed08636473759235c6a0d2bffd3d3a..3c5e9e6cd3a99ab679c420a17d2d041e4de95261 100644 (file)
@@ -436,7 +436,8 @@ fn build_reduced_graph_for_external_crate_def(&mut self, parent: Module<'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 \