trans: force absolute item paths within symbols.

This commit is contained in:
Eduard Burtescu 2016-05-12 01:25:20 +03:00
parent 14133d33bc
commit a6a5e4884a
2 changed files with 42 additions and 5 deletions

View File

@ -14,12 +14,38 @@
use ty::{self, Ty, TyCtxt};
use syntax::ast;
use std::cell::Cell;
thread_local! {
static FORCE_ABSOLUTE: Cell<bool> = Cell::new(false)
}
/// Enforces that item_path_str always returns an absolute path.
/// This is useful when building symbols that contain types,
/// where we want the crate name to be part of the symbol.
pub fn with_forced_absolute_paths<F: FnOnce() -> R, R>(f: F) -> R {
FORCE_ABSOLUTE.with(|force| {
let old = force.get();
force.set(true);
let result = f();
force.set(old);
result
})
}
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
/// Returns a string identifying this def-id. This string is
/// suitable for user output. It is relative to the current crate
/// root.
/// root, unless with_forced_absolute_paths was used.
pub fn item_path_str(self, def_id: DefId) -> String {
let mut buffer = LocalPathBuffer::new(RootMode::Local);
let mode = FORCE_ABSOLUTE.with(|force| {
if force.get() {
RootMode::Absolute
} else {
RootMode::Local
}
});
let mut buffer = LocalPathBuffer::new(mode);
self.push_item_path(&mut buffer, def_id);
buffer.into_string()
}
@ -75,7 +101,11 @@ pub fn push_krate_path<T>(self, buffer: &mut T, cnum: ast::CrateNum)
RootMode::Absolute => {
// In absolute mode, just write the crate name
// unconditionally.
buffer.push(&self.crate_name(cnum));
if cnum == LOCAL_CRATE {
buffer.push(&self.crate_name(cnum));
} else {
buffer.push(&self.sess.cstore.original_crate_name(cnum));
}
}
}
}

View File

@ -120,7 +120,11 @@ pub fn def_id_to_string<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) ->
fn def_path_to_string<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_path: &DefPath) -> String {
let mut s = String::with_capacity(def_path.data.len() * 16);
s.push_str(&tcx.crate_name(def_path.krate));
if def_path.krate == cstore::LOCAL_CRATE {
s.push_str(&tcx.crate_name(def_path.krate));
} else {
s.push_str(&tcx.sess.cstore.original_crate_name(def_path.krate));
}
s.push_str("/");
s.push_str(&tcx.crate_disambiguator(def_path.krate));
@ -265,7 +269,10 @@ pub fn exported_name<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
let mut buffer = SymbolPathBuffer {
names: Vec::with_capacity(def_path.data.len())
};
ccx.tcx().push_item_path(&mut buffer, def_id);
item_path::with_forced_absolute_paths(|| {
scx.tcx().push_item_path(&mut buffer, def_id);
});
mangle(buffer.names.into_iter(), Some(&hash[..]))
}