Skip to content

Commit dadbaa4

Browse files
committed
rustc_metadata: move opt_item_name to TyCtxt::item_name.
1 parent dc26a23 commit dadbaa4

File tree

7 files changed

+51
-55
lines changed

7 files changed

+51
-55
lines changed

src/librustc/hir/map/definitions.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_data_structures::fnv::FnvHashMap;
1414
use std::fmt::Write;
1515
use std::hash::{Hash, Hasher, SipHasher};
1616
use syntax::{ast, visit};
17-
use syntax::parse::token::InternedString;
17+
use syntax::parse::token::{self, InternedString};
1818
use ty::TyCtxt;
1919
use util::nodemap::NodeMap;
2020

@@ -326,6 +326,30 @@ impl Definitions {
326326
}
327327

328328
impl DefPathData {
329+
pub fn get_opt_name(&self) -> Option<ast::Name> {
330+
use self::DefPathData::*;
331+
match *self {
332+
TypeNs(ref name) |
333+
ValueNs(ref name) |
334+
Module(ref name) |
335+
MacroDef(ref name) |
336+
TypeParam(ref name) |
337+
LifetimeDef(ref name) |
338+
EnumVariant(ref name) |
339+
Binding(ref name) |
340+
Field(ref name) => Some(token::intern(name)),
341+
342+
Impl |
343+
CrateRoot |
344+
InlinedRoot(_) |
345+
Misc |
346+
ClosureExpr |
347+
StructCtor |
348+
Initializer |
349+
ImplTrait => None
350+
}
351+
}
352+
329353
pub fn as_interned_str(&self) -> InternedString {
330354
use self::DefPathData::*;
331355
match *self {

src/librustc/middle/cstore.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ pub trait CrateStore<'tcx> {
132132
fn item_type<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
133133
-> Ty<'tcx>;
134134
fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap<DefId>>;
135-
fn opt_item_name(&self, def: DefId) -> Option<ast::Name>;
136135
fn item_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
137136
-> ty::GenericPredicates<'tcx>;
138137
fn item_super_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
@@ -293,7 +292,6 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
293292
fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap<DefId>> {
294293
bug!("visible_parent_map")
295294
}
296-
fn opt_item_name(&self, def: DefId) -> Option<ast::Name> { bug!("opt_item_name") }
297295
fn item_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
298296
-> ty::GenericPredicates<'tcx> { bug!("item_predicates") }
299297
fn item_super_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
@@ -424,4 +422,4 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
424422

425423
pub trait MacroLoader {
426424
fn load_crate(&mut self, extern_crate: &ast::Item, allows_macros: bool) -> Vec<LoadedMacro>;
427-
}
425+
}

src/librustc/ty/item_path.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
138138
}
139139
}
140140

141-
cur_path.push(self.sess.cstore.opt_item_name(cur_def).unwrap_or_else(||
141+
cur_path.push(self.sess.cstore.def_key(cur_def)
142+
.disambiguated_data.data.get_opt_name().unwrap_or_else(||
142143
token::intern("<unnamed>")));
143144
match visible_parent_map.get(&cur_def) {
144145
Some(&def) => cur_def = def,

src/librustc/ty/mod.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use dep_graph::{self, DepNode};
2121
use hir::map as ast_map;
2222
use middle;
2323
use hir::def::{Def, PathResolution, ExportMap};
24-
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
24+
use hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
2525
use middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
2626
use middle::region::{CodeExtent, ROOT_CODE_EXTENT};
2727
use traits;
@@ -43,7 +43,7 @@ use std::slice;
4343
use std::vec::IntoIter;
4444
use syntax::ast::{self, Name, NodeId};
4545
use syntax::attr;
46-
use syntax::parse::token::InternedString;
46+
use syntax::parse::token::{self, InternedString};
4747
use syntax_pos::{DUMMY_SP, Span};
4848

4949
use rustc_const_math::ConstInt;
@@ -2390,10 +2390,21 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
23902390
pub fn item_name(self, id: DefId) -> ast::Name {
23912391
if let Some(id) = self.map.as_local_node_id(id) {
23922392
self.map.name(id)
2393+
} else if id.index == CRATE_DEF_INDEX {
2394+
token::intern(&self.sess.cstore.original_crate_name(id.krate))
23932395
} else {
2394-
self.sess.cstore.opt_item_name(id).unwrap_or_else(|| {
2395-
bug!("item_name: no name for {:?}", self.def_path(id));
2396-
})
2396+
let def_key = self.sess.cstore.def_key(id);
2397+
// The name of a StructCtor is that of its struct parent.
2398+
if let ast_map::DefPathData::StructCtor = def_key.disambiguated_data.data {
2399+
self.item_name(DefId {
2400+
krate: id.krate,
2401+
index: def_key.parent.unwrap()
2402+
})
2403+
} else {
2404+
def_key.disambiguated_data.data.get_opt_name().unwrap_or_else(|| {
2405+
bug!("item_name: no name for {:?}", self.def_path(id));
2406+
})
2407+
}
23972408
}
23982409
}
23992410

src/librustc_metadata/csearch.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,6 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
127127
self.get_crate_data(did.krate).get_fn_arg_names(did.index)
128128
}
129129

130-
fn opt_item_name(&self, def: DefId) -> Option<ast::Name> {
131-
self.dep_graph.read(DepNode::MetaData(def));
132-
let cdata = self.get_crate_data(def.krate);
133-
if def.index == CRATE_DEF_INDEX {
134-
Some(token::intern(&cdata.name()))
135-
} else {
136-
cdata.maybe_get_item_name(def.index)
137-
}
138-
}
139-
140130
fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>
141131
{
142132
self.dep_graph.read(DepNode::MetaData(def_id));

src/librustc_metadata/decoder.rs

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque};
4343
use syntax::attr;
4444
use syntax::ast::{self, NodeId};
4545
use syntax::codemap;
46-
use syntax::parse::token;
4746
use syntax_pos::{self, Span, BytePos, Pos};
4847

4948
pub struct DecodeContext<'a, 'tcx: 'a> {
@@ -469,32 +468,6 @@ impl<'tcx> EntryKind<'tcx> {
469468
}
470469
}
471470

472-
fn def_key_name(def_key: &hir_map::DefKey) -> Option<ast::Name> {
473-
match def_key.disambiguated_data.data {
474-
DefPathData::TypeNs(ref name) |
475-
DefPathData::ValueNs(ref name) |
476-
DefPathData::Module(ref name) |
477-
DefPathData::MacroDef(ref name) |
478-
DefPathData::TypeParam(ref name) |
479-
DefPathData::LifetimeDef(ref name) |
480-
DefPathData::EnumVariant(ref name) |
481-
DefPathData::Field(ref name) |
482-
DefPathData::Binding(ref name) => {
483-
Some(token::intern(name))
484-
}
485-
486-
DefPathData::InlinedRoot(_) => bug!("unexpected DefPathData"),
487-
488-
DefPathData::CrateRoot |
489-
DefPathData::Misc |
490-
DefPathData::Impl |
491-
DefPathData::ClosureExpr |
492-
DefPathData::StructCtor |
493-
DefPathData::Initializer |
494-
DefPathData::ImplTrait => None
495-
}
496-
}
497-
498471
impl<'a, 'tcx> CrateMetadata {
499472
fn maybe_entry(&self, item_id: DefIndex) -> Option<Lazy<Entry<'tcx>>> {
500473
self.root.index.lookup(self.blob.raw_bytes(), item_id)
@@ -518,7 +491,8 @@ impl<'a, 'tcx> CrateMetadata {
518491
}
519492

520493
fn item_name(&self, item: &Entry<'tcx>) -> ast::Name {
521-
def_key_name(&item.def_key.decode(self)).expect("no name in item_name")
494+
item.def_key.decode(self).disambiguated_data.data.get_opt_name()
495+
.expect("no name in item_name")
522496
}
523497

524498
pub fn get_def(&self, index: DefIndex) -> Option<Def> {
@@ -708,7 +682,8 @@ impl<'a, 'tcx> CrateMetadata {
708682
_ => {}
709683
}
710684

711-
if let Some(name) = def_key_name(&child.def_key.decode(self)) {
685+
let def_key = child.def_key.decode(self);
686+
if let Some(name) = def_key.disambiguated_data.data.get_opt_name() {
712687
callback(def::Export {
713688
def_id: self.local_def_id(child_index),
714689
name: name
@@ -724,10 +699,6 @@ impl<'a, 'tcx> CrateMetadata {
724699
}
725700
}
726701

727-
pub fn maybe_get_item_name(&self, id: DefIndex) -> Option<ast::Name> {
728-
def_key_name(&self.entry(id).def_key.decode(self))
729-
}
730-
731702
pub fn maybe_get_item_ast(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, id: DefIndex)
732703
-> Option<&'tcx InlinedItem> {
733704
debug!("Looking up item: {:?}", id);
@@ -757,7 +728,7 @@ impl<'a, 'tcx> CrateMetadata {
757728
let parent_and_name = || {
758729
let def_key = item.def_key.decode(self);
759730
(self.local_def_id(def_key.parent.unwrap()),
760-
def_key_name(&def_key).unwrap())
731+
def_key.disambiguated_data.data.get_opt_name().unwrap())
761732
};
762733

763734
Some(match item.kind {

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,8 @@ impl<'b> Resolver<'b> {
436436
let trait_item_def_ids = self.session.cstore.impl_or_trait_items(def_id);
437437
for &trait_item_def in &trait_item_def_ids {
438438
let trait_item_name =
439-
self.session.cstore.opt_item_name(trait_item_def)
439+
self.session.cstore.def_key(trait_item_def)
440+
.disambiguated_data.data.get_opt_name()
440441
.expect("opt_item_name returned None for trait");
441442

442443
debug!("(building reduced graph for external crate) ... adding trait item \

0 commit comments

Comments
 (0)