Skip to content

Commit 25bdd61

Browse files
committed
store krate information more uniformly
make DefPath store krate and enable uniform access to crate_name/crate_disambiguator
1 parent 49d3636 commit 25bdd61

File tree

11 files changed

+91
-70
lines changed

11 files changed

+91
-70
lines changed

src/librustc/front/map/definitions.rs

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,54 @@ pub struct DefData {
5959
pub node_id: ast::NodeId,
6060
}
6161

62-
pub type DefPath = Vec<DisambiguatedDefPathData>;
62+
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
63+
pub struct DefPath {
64+
/// the path leading from the crate root to the item
65+
pub data: Vec<DisambiguatedDefPathData>,
66+
67+
/// what krate root is this path relative to?
68+
pub krate: ast::CrateNum,
69+
}
70+
71+
impl DefPath {
72+
pub fn is_local(&self) -> bool {
73+
self.krate == LOCAL_CRATE
74+
}
75+
76+
pub fn make<FN>(start_krate: ast::CrateNum,
77+
start_index: DefIndex,
78+
mut get_key: FN) -> DefPath
79+
where FN: FnMut(DefIndex) -> DefKey
80+
{
81+
let mut krate = start_krate;
82+
let mut data = vec![];
83+
let mut index = Some(start_index);
84+
loop {
85+
let p = index.unwrap();
86+
let key = get_key(p);
87+
match key.disambiguated_data.data {
88+
DefPathData::CrateRoot => {
89+
assert!(key.parent.is_none());
90+
break;
91+
}
92+
DefPathData::InlinedRoot(ref p) => {
93+
assert!(key.parent.is_none());
94+
assert!(!p.def_id.is_local());
95+
data.extend(p.data.iter().cloned().rev());
96+
krate = p.def_id.krate;
97+
break;
98+
}
99+
_ => {
100+
data.push(key.disambiguated_data);
101+
index = key.parent;
102+
}
103+
}
104+
}
105+
data.reverse();
106+
DefPath { data: data, krate: krate }
107+
}
108+
}
109+
63110
/// Root of an inlined item. We track the `DefPath` of the item within
64111
/// the original crate but also its def-id. This is kind of an
65112
/// augmented version of a `DefPath` that includes a `DefId`. This is
@@ -141,7 +188,7 @@ impl Definitions {
141188
/// will be the path of the item in the external crate (but the
142189
/// path will begin with the path to the external crate).
143190
pub fn def_path(&self, index: DefIndex) -> DefPath {
144-
make_def_path(index, |p| self.def_key(p))
191+
DefPath::make(LOCAL_CRATE, index, |p| self.def_key(p))
145192
}
146193

147194
pub fn opt_def_index(&self, node: ast::NodeId) -> Option<DefIndex> {
@@ -247,29 +294,3 @@ impl DefPathData {
247294
}
248295
}
249296

250-
pub fn make_def_path<FN>(start_index: DefIndex, mut get_key: FN) -> DefPath
251-
where FN: FnMut(DefIndex) -> DefKey
252-
{
253-
let mut result = vec![];
254-
let mut index = Some(start_index);
255-
while let Some(p) = index {
256-
let key = get_key(p);
257-
match key.disambiguated_data.data {
258-
DefPathData::CrateRoot => {
259-
assert!(key.parent.is_none());
260-
break;
261-
}
262-
DefPathData::InlinedRoot(ref p) => {
263-
assert!(key.parent.is_none());
264-
result.extend(p.iter().cloned().rev());
265-
break;
266-
}
267-
_ => {
268-
result.push(key.disambiguated_data);
269-
index = key.parent;
270-
}
271-
}
272-
}
273-
result.reverse();
274-
result
275-
}

src/librustc/middle/ty/context.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use front::map as ast_map;
1515
use session::Session;
1616
use lint;
1717
use middle;
18-
use middle::cstore::CrateStore;
18+
use middle::cstore::{CrateStore, LOCAL_CRATE};
1919
use middle::def::DefMap;
2020
use middle::def_id::DefId;
2121
use middle::free_region::FreeRegionMap;
@@ -422,6 +422,22 @@ pub struct TyCtxt<'tcx> {
422422
}
423423

424424
impl<'tcx> TyCtxt<'tcx> {
425+
pub fn crate_name(&self, cnum: ast::CrateNum) -> token::InternedString {
426+
if cnum == LOCAL_CRATE {
427+
self.crate_name.clone()
428+
} else {
429+
self.sess.cstore.crate_name(cnum)
430+
}
431+
}
432+
433+
pub fn crate_disambiguator(&self, cnum: ast::CrateNum) -> token::InternedString {
434+
if cnum == LOCAL_CRATE {
435+
self.sess.crate_disambiguator.get().as_str()
436+
} else {
437+
self.sess.cstore.crate_name(cnum)
438+
}
439+
}
440+
425441
pub fn type_parameter_def(&self,
426442
node_id: NodeId)
427443
-> ty::TypeParameterDef<'tcx>

src/librustc/session/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use syntax::diagnostics;
2424
use syntax::feature_gate;
2525
use syntax::parse;
2626
use syntax::parse::ParseSess;
27+
use syntax::parse::token;
2728
use syntax::{ast, codemap};
2829
use syntax::feature_gate::AttributeType;
2930

@@ -69,7 +70,7 @@ pub struct Session {
6970
// forms a unique global identifier for the crate. It is used to allow
7071
// multiple crates with the same name to coexist. See the
7172
// trans::back::symbol_names module for more information.
72-
pub crate_disambiguator: RefCell<String>,
73+
pub crate_disambiguator: Cell<ast::Name>,
7374
pub features: RefCell<feature_gate::Features>,
7475

7576
/// The maximum recursion limit for potentially infinitely recursive
@@ -486,7 +487,7 @@ pub fn build_session_(sopts: config::Options,
486487
plugin_attributes: RefCell::new(Vec::new()),
487488
crate_types: RefCell::new(Vec::new()),
488489
dependency_formats: RefCell::new(FnvHashMap()),
489-
crate_disambiguator: RefCell::new(String::new()),
490+
crate_disambiguator: Cell::new(token::intern("")),
490491
features: RefCell::new(feature_gate::Features::new()),
491492
recursion_limit: Cell::new(64),
492493
next_node_id: Cell::new(1),

src/librustc_driver/driver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ pub fn phase_2_configure_and_expand(sess: &Session,
501501
})?;
502502

503503
*sess.crate_types.borrow_mut() = collect_crate_types(sess, &krate.attrs);
504-
*sess.crate_disambiguator.borrow_mut() = compute_crate_disambiguator(sess);
504+
sess.crate_disambiguator.set(token::intern(&compute_crate_disambiguator(sess)));
505505

506506
time(time_passes, "recursion limit", || {
507507
middle::recursion_limit::update_recursion_limit(sess, &krate);

src/librustc_metadata/creader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ impl<'a> CrateReader<'a> {
284284

285285
// Check for (potential) conflicts with the local crate
286286
if self.local_crate_name == crate_name &&
287-
&self.sess.crate_disambiguator.borrow()[..] == disambiguator {
287+
self.sess.crate_disambiguator.get().as_str() == disambiguator {
288288
span_fatal!(self.sess, span, E0519,
289289
"the current crate is indistinguishable from one of its \
290290
dependencies: it has the same crate-name `{}` and was \

src/librustc_metadata/decoder.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,9 +1779,5 @@ fn def_key(item_doc: rbml::Doc) -> hir_map::DefKey {
17791779

17801780
pub fn def_path(cdata: Cmd, id: DefIndex) -> hir_map::DefPath {
17811781
debug!("def_path(id={:?})", id);
1782-
hir_map::definitions::make_def_path(id, |parent| {
1783-
debug!("def_path: parent={:?}", parent);
1784-
let parent_doc = cdata.lookup_item(parent);
1785-
def_key(parent_doc)
1786-
})
1782+
hir_map::DefPath::make(cdata.cnum, id, |parent| def_key(cdata, parent))
17871783
}

src/librustc_metadata/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1991,7 +1991,7 @@ fn encode_metadata_inner(rbml_w: &mut Encoder,
19911991
encode_crate_name(rbml_w, &ecx.link_meta.crate_name);
19921992
encode_crate_triple(rbml_w, &ecx.tcx.sess.opts.target_triple);
19931993
encode_hash(rbml_w, &ecx.link_meta.crate_hash);
1994-
encode_crate_disambiguator(rbml_w, &ecx.tcx.sess.crate_disambiguator.borrow());
1994+
encode_crate_disambiguator(rbml_w, &ecx.tcx.sess.crate_disambiguator.get().as_str());
19951995
encode_dylib_dependency_formats(rbml_w, &ecx);
19961996

19971997
let mut i = rbml_w.writer.seek(SeekFrom::Current(0)).unwrap();

src/librustc_trans/back/link.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ pub fn build_link_meta(sess: &Session,
130130
-> LinkMeta {
131131
let r = LinkMeta {
132132
crate_name: name.to_owned(),
133-
crate_hash: Svh::calculate(&sess.crate_disambiguator.borrow()[..], krate),
133+
crate_hash: Svh::calculate(&sess.crate_disambiguator.get().as_str(), krate),
134134
};
135135
info!("{:?}", r);
136136
return r;

src/librustc_trans/back/symbol_names.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,18 @@ use syntax::parse::token::{self, InternedString};
111111
use serialize::hex::ToHex;
112112

113113
pub fn def_id_to_string<'tcx>(tcx: &ty::TyCtxt<'tcx>, def_id: DefId) -> String {
114-
115114
let def_path = tcx.def_path(def_id);
116-
let mut s = String::with_capacity(def_path.len() * 16);
115+
def_path_to_string(tcx, &def_path)
116+
}
117117

118-
let def_path = if def_id.is_local() {
119-
s.push_str(&tcx.crate_name[..]);
120-
s.push_str("/");
121-
s.push_str(&tcx.sess.crate_disambiguator.borrow()[..]);
122-
&def_path[..]
123-
} else {
124-
s.push_str(&tcx.sess.cstore.crate_name(def_id.krate)[..]);
125-
s.push_str("/");
126-
s.push_str(&tcx.sess.cstore.crate_disambiguator(def_id.krate));
127-
&def_path[1..]
128-
};
118+
pub fn def_path_to_string<'tcx>(tcx: &ty::TyCtxt<'tcx>, def_path: &DefPath) -> String {
119+
let mut s = String::with_capacity(def_path.data.len() * 16);
129120

130-
for component in def_path {
121+
s.push_str(&tcx.crate_name(def_path.krate));
122+
s.push_str("/");
123+
s.push_str(&tcx.crate_disambiguator(def_path.krate));
124+
125+
for component in &def_path.data {
131126
write!(s,
132127
"::{}[{}]",
133128
component.data.as_interned_str(),

src/librustc_trans/trans/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2444,7 +2444,7 @@ pub fn exported_name<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
24442444
if attr::contains_name(attrs, "no_mangle") {
24452445
// Don't mangle
24462446
let path = ccx.tcx().map.def_path_from_id(id);
2447-
path.last().unwrap().data.to_string()
2447+
path.data.last().unwrap().data.to_string()
24482448
} else {
24492449
match weak_lang_items::link_name(attrs) {
24502450
Some(name) => name.to_string(),

src/librustc_trans/trans/collector.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,28 +1268,20 @@ pub fn push_unique_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
12681268
fn push_item_name(ccx: &CrateContext,
12691269
def_id: DefId,
12701270
output: &mut String) {
1271-
if def_id.is_local() {
1272-
let node_id = ccx.tcx().map.as_local_node_id(def_id).unwrap();
1273-
let inlined_from = ccx.external_srcs()
1274-
.borrow()
1275-
.get(&node_id)
1276-
.map(|def_id| *def_id);
1277-
1278-
if let Some(extern_def_id) = inlined_from {
1279-
push_item_name(ccx, extern_def_id, output);
1280-
return;
1281-
}
1271+
let def_path = ccx.tcx().def_path(def_id);
12821272

1283-
output.push_str(&ccx.link_meta().crate_name);
1284-
output.push_str("::");
1285-
}
1273+
// some_crate::
1274+
output.push_str(&ccx.tcx().crate_name(def_path.krate));
1275+
output.push_str("::");
12861276

1287-
for part in ccx.tcx().def_path(def_id) {
1277+
// foo::bar::ItemName::
1278+
for part in ccx.tcx().def_path(def_id).data {
12881279
output.push_str(&format!("{}[{}]::",
12891280
part.data.as_interned_str(),
12901281
part.disambiguator));
12911282
}
12921283

1284+
// remove final "::"
12931285
output.pop();
12941286
output.pop();
12951287
}

0 commit comments

Comments
 (0)