Skip to content

Commit

Permalink
trans: force absolute item paths within symbols.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed May 24, 2016
1 parent 14133d3 commit a6a5e48
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
36 changes: 33 additions & 3 deletions src/librustc/ty/item_path.rs
Expand Up @@ -14,12 +14,38 @@ use hir::def_id::{DefId, CRATE_DEF_INDEX};
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()
}
Expand Down Expand Up @@ -75,7 +101,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
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));
}
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/librustc_trans/back/symbol_names.rs
Expand Up @@ -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));

Expand Down Expand Up @@ -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[..]))
}
Expand Down

0 comments on commit a6a5e48

Please sign in to comment.