Skip to content

Commit a6a5e48

Browse files
committed
trans: force absolute item paths within symbols.
1 parent 14133d3 commit a6a5e48

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/librustc/ty/item_path.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,38 @@ use hir::def_id::{DefId, CRATE_DEF_INDEX};
1414
use ty::{self, Ty, TyCtxt};
1515
use syntax::ast;
1616

17+
use std::cell::Cell;
18+
19+
thread_local! {
20+
static FORCE_ABSOLUTE: Cell<bool> = Cell::new(false)
21+
}
22+
23+
/// Enforces that item_path_str always returns an absolute path.
24+
/// This is useful when building symbols that contain types,
25+
/// where we want the crate name to be part of the symbol.
26+
pub fn with_forced_absolute_paths<F: FnOnce() -> R, R>(f: F) -> R {
27+
FORCE_ABSOLUTE.with(|force| {
28+
let old = force.get();
29+
force.set(true);
30+
let result = f();
31+
force.set(old);
32+
result
33+
})
34+
}
35+
1736
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
1837
/// Returns a string identifying this def-id. This string is
1938
/// suitable for user output. It is relative to the current crate
20-
/// root.
39+
/// root, unless with_forced_absolute_paths was used.
2140
pub fn item_path_str(self, def_id: DefId) -> String {
22-
let mut buffer = LocalPathBuffer::new(RootMode::Local);
41+
let mode = FORCE_ABSOLUTE.with(|force| {
42+
if force.get() {
43+
RootMode::Absolute
44+
} else {
45+
RootMode::Local
46+
}
47+
});
48+
let mut buffer = LocalPathBuffer::new(mode);
2349
self.push_item_path(&mut buffer, def_id);
2450
buffer.into_string()
2551
}
@@ -75,7 +101,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
75101
RootMode::Absolute => {
76102
// In absolute mode, just write the crate name
77103
// unconditionally.
78-
buffer.push(&self.crate_name(cnum));
104+
if cnum == LOCAL_CRATE {
105+
buffer.push(&self.crate_name(cnum));
106+
} else {
107+
buffer.push(&self.sess.cstore.original_crate_name(cnum));
108+
}
79109
}
80110
}
81111
}

src/librustc_trans/back/symbol_names.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ pub fn def_id_to_string<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) ->
120120
fn def_path_to_string<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_path: &DefPath) -> String {
121121
let mut s = String::with_capacity(def_path.data.len() * 16);
122122

123-
s.push_str(&tcx.crate_name(def_path.krate));
123+
if def_path.krate == cstore::LOCAL_CRATE {
124+
s.push_str(&tcx.crate_name(def_path.krate));
125+
} else {
126+
s.push_str(&tcx.sess.cstore.original_crate_name(def_path.krate));
127+
}
124128
s.push_str("/");
125129
s.push_str(&tcx.crate_disambiguator(def_path.krate));
126130

@@ -265,7 +269,10 @@ pub fn exported_name<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
265269
let mut buffer = SymbolPathBuffer {
266270
names: Vec::with_capacity(def_path.data.len())
267271
};
268-
ccx.tcx().push_item_path(&mut buffer, def_id);
272+
273+
item_path::with_forced_absolute_paths(|| {
274+
scx.tcx().push_item_path(&mut buffer, def_id);
275+
});
269276

270277
mangle(buffer.names.into_iter(), Some(&hash[..]))
271278
}

0 commit comments

Comments
 (0)