Skip to content

Commit d017466

Browse files
committed
Use 128 bit instead of Symbol for crate disambiguator
1 parent 4c053db commit d017466

File tree

16 files changed

+44
-43
lines changed

16 files changed

+44
-43
lines changed

src/librustc/hir/map/collector.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use super::*;
1212

1313
use dep_graph::{DepGraph, DepKind, DepNodeIndex};
14+
use ich::Fingerprint;
1415
use hir::intravisit::{Visitor, NestedVisitorMap};
1516
use std::iter::repeat;
1617
use syntax::ast::{NodeId, CRATE_NODE_ID};
@@ -118,7 +119,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
118119
}
119120

120121
pub(super) fn finalize_and_compute_crate_hash(self,
121-
crate_disambiguator: &str)
122+
crate_disambiguator: &Fingerprint)
122123
-> Vec<MapEntry<'hir>> {
123124
let mut node_hashes: Vec<_> = self
124125
.hir_body_nodes

src/librustc/hir/map/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ pub fn map_crate<'hir>(sess: &::session::Session,
10141014
hcx);
10151015
intravisit::walk_crate(&mut collector, &forest.krate);
10161016

1017-
let crate_disambiguator = sess.local_crate_disambiguator().as_str();
1017+
let crate_disambiguator = sess.local_crate_disambiguator();
10181018
collector.finalize_and_compute_crate_hash(&crate_disambiguator)
10191019
};
10201020

src/librustc/middle/cstore.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ pub trait CrateStore {
267267
fn export_macros_untracked(&self, cnum: CrateNum);
268268
fn dep_kind_untracked(&self, cnum: CrateNum) -> DepKind;
269269
fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol;
270-
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> Symbol;
270+
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> ich::Fingerprint;
271271
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh;
272272
fn struct_field_names_untracked(&self, def: DefId) -> Vec<ast::Name>;
273273
fn item_children_untracked(&self, did: DefId, sess: &Session) -> Vec<def::Export>;
@@ -338,7 +338,7 @@ impl CrateStore for DummyCrateStore {
338338
fn dep_kind_untracked(&self, cnum: CrateNum) -> DepKind { bug!("is_explicitly_linked") }
339339
fn export_macros_untracked(&self, cnum: CrateNum) { bug!("export_macros") }
340340
fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol { bug!("crate_name") }
341-
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> Symbol {
341+
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> ich::Fingerprint {
342342
bug!("crate_disambiguator")
343343
}
344344
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh { bug!("crate_hash") }

src/librustc/session/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub use self::code_stats::{CodeStats, DataTypeKind, FieldInfo};
1212
pub use self::code_stats::{SizeKind, TypeSizeInfo, VariantInfo};
1313

1414
use hir::def_id::{CrateNum, DefIndex};
15+
use ich::Fingerprint;
1516

1617
use lint;
1718
use middle::allocator::AllocatorKind;
@@ -29,7 +30,6 @@ use syntax::json::JsonEmitter;
2930
use syntax::feature_gate;
3031
use syntax::parse;
3132
use syntax::parse::ParseSess;
32-
use syntax::symbol::Symbol;
3333
use syntax::{ast, codemap};
3434
use syntax::feature_gate::AttributeType;
3535
use syntax_pos::{Span, MultiSpan};
@@ -88,7 +88,7 @@ pub struct Session {
8888
/// forms a unique global identifier for the crate. It is used to allow
8989
/// multiple crates with the same name to coexist. See the
9090
/// trans::back::symbol_names module for more information.
91-
pub crate_disambiguator: RefCell<Option<Symbol>>,
91+
pub crate_disambiguator: RefCell<Option<Fingerprint>>,
9292
pub features: RefCell<feature_gate::Features>,
9393

9494
/// The maximum recursion limit for potentially infinitely recursive
@@ -165,7 +165,7 @@ enum DiagnosticBuilderMethod {
165165
}
166166

167167
impl Session {
168-
pub fn local_crate_disambiguator(&self) -> Symbol {
168+
pub fn local_crate_disambiguator(&self) -> Fingerprint {
169169
match *self.crate_disambiguator.borrow() {
170170
Some(sym) => sym,
171171
None => bug!("accessing disambiguator before initialization"),
@@ -471,14 +471,17 @@ impl Session {
471471

472472
/// Returns the symbol name for the registrar function,
473473
/// given the crate Svh and the function DefIndex.
474-
pub fn generate_plugin_registrar_symbol(&self, disambiguator: Symbol, index: DefIndex)
474+
pub fn generate_plugin_registrar_symbol(&self, disambiguator: Fingerprint,
475+
index: DefIndex)
475476
-> String {
476-
format!("__rustc_plugin_registrar__{}_{}", disambiguator, index.as_usize())
477+
format!("__rustc_plugin_registrar__{}_{}", disambiguator.to_hex(),
478+
index.as_usize())
477479
}
478480

479-
pub fn generate_derive_registrar_symbol(&self, disambiguator: Symbol, index: DefIndex)
481+
pub fn generate_derive_registrar_symbol(&self, disambiguator: Fingerprint, index: DefIndex)
480482
-> String {
481-
format!("__rustc_derive_registrar__{}_{}", disambiguator, index.as_usize())
483+
format!("__rustc_derive_registrar__{}_{}", disambiguator.to_hex(),
484+
index.as_usize())
482485
}
483486

484487
pub fn sysroot<'a>(&'a self) -> &'a Path {

src/librustc/ty/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12511251
crate_name,
12521252
// Don't print the whole crate disambiguator. That's just
12531253
// annoying in debug output.
1254-
&(crate_disambiguator.as_str())[..4],
1254+
&(crate_disambiguator.to_hex())[..4],
12551255
self.def_path(def_id).to_string_no_crate())
12561256
}
12571257

src/librustc/ty/maps/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use dep_graph::{DepConstructor, DepNode};
1212
use errors::DiagnosticBuilder;
13+
use ich::Fingerprint;
1314
use hir::def_id::{CrateNum, DefId, DefIndex};
1415
use hir::def::{Def, Export};
1516
use hir::{self, TraitCandidate, ItemLocalId};
@@ -283,7 +284,7 @@ define_maps! { <'tcx>
283284
[] fn native_libraries: NativeLibraries(CrateNum) -> Rc<Vec<NativeLibrary>>,
284285
[] fn plugin_registrar_fn: PluginRegistrarFn(CrateNum) -> Option<DefId>,
285286
[] fn derive_registrar_fn: DeriveRegistrarFn(CrateNum) -> Option<DefId>,
286-
[] fn crate_disambiguator: CrateDisambiguator(CrateNum) -> Symbol,
287+
[] fn crate_disambiguator: CrateDisambiguator(CrateNum) -> Fingerprint,
287288
[] fn crate_hash: CrateHash(CrateNum) -> Svh,
288289
[] fn original_crate_name: OriginalCrateName(CrateNum) -> Symbol,
289290

src/librustc/ty/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use hir::{map as hir_map, FreevarMap, TraitMap};
1919
use hir::def::{Def, CtorKind, ExportMap};
2020
use hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
2121
use hir::map::DefPathData;
22-
use ich::StableHashingContext;
22+
use ich::{Fingerprint, StableHashingContext};
2323
use middle::const_val::ConstVal;
2424
use middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
2525
use middle::privacy::AccessLevels;
@@ -2562,7 +2562,7 @@ fn param_env<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
25622562
}
25632563

25642564
fn crate_disambiguator<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
2565-
crate_num: CrateNum) -> Symbol {
2565+
crate_num: CrateNum) -> Fingerprint {
25662566
assert_eq!(crate_num, LOCAL_CRATE);
25672567
tcx.sess.local_crate_disambiguator()
25682568
}

src/librustc_driver/driver.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ use syntax::{ast, diagnostics, visit};
5858
use syntax::attr;
5959
use syntax::ext::base::ExtCtxt;
6060
use syntax::parse::{self, PResult};
61-
use syntax::symbol::Symbol;
6261
use syntax::util::node_count::NodeCounter;
6362
use syntax;
6463
use syntax_ext;
@@ -633,12 +632,12 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
633632

634633
*sess.crate_types.borrow_mut() = collect_crate_types(sess, &krate.attrs);
635634

636-
let disambiguator = Symbol::intern(&compute_crate_disambiguator(sess));
635+
let disambiguator = compute_crate_disambiguator(sess);
637636
*sess.crate_disambiguator.borrow_mut() = Some(disambiguator);
638637
rustc_incremental::prepare_session_directory(
639638
sess,
640639
&crate_name,
641-
&disambiguator.as_str(),
640+
&disambiguator,
642641
);
643642

644643
let dep_graph = if sess.opts.build_dep_graph() {
@@ -1312,16 +1311,13 @@ pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<c
13121311
.collect()
13131312
}
13141313

1315-
pub fn compute_crate_disambiguator(session: &Session) -> String {
1314+
pub fn compute_crate_disambiguator(session: &Session) -> Fingerprint {
13161315
use std::hash::Hasher;
13171316

13181317
// The crate_disambiguator is a 128 bit hash. The disambiguator is fed
13191318
// into various other hashes quite a bit (symbol hashes, incr. comp. hashes,
13201319
// debuginfo type IDs, etc), so we don't want it to be too wide. 128 bits
13211320
// should still be safe enough to avoid collisions in practice.
1322-
// FIXME(mw): It seems that the crate_disambiguator is used everywhere as
1323-
// a hex-string instead of raw bytes. We should really use the
1324-
// smaller representation.
13251321
let mut hasher = StableHasher::<Fingerprint>::new();
13261322

13271323
let mut metadata = session.opts.cg.metadata.clone();
@@ -1340,11 +1336,13 @@ pub fn compute_crate_disambiguator(session: &Session) -> String {
13401336
hasher.write(s.as_bytes());
13411337
}
13421338

1343-
// If this is an executable, add a special suffix, so that we don't get
1344-
// symbol conflicts when linking against a library of the same name.
1339+
// Also incorporate crate type, so that we don't get symbol conflicts when
1340+
// linking against a library of the same name, if this is an executable.
13451341
let is_exe = session.crate_types.borrow().contains(&config::CrateTypeExecutable);
1342+
hasher.write(if is_exe { b"exe" } else { b"lib" });
1343+
1344+
hasher.finish()
13461345

1347-
format!("{}{}", hasher.finish().to_hex(), if is_exe { "-exe" } else {""})
13481346
}
13491347

13501348
pub fn build_output_filenames(input: &Input,

src/librustc_incremental/persist/fs.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
//! unsupported file system and emit a warning in that case. This is not yet
115115
//! implemented.
116116
117+
use rustc::ich::Fingerprint;
117118
use rustc::hir::svh::Svh;
118119
use rustc::session::Session;
119120
use rustc::util::fs as fs_util;
@@ -188,7 +189,7 @@ pub fn in_incr_comp_dir(incr_comp_session_dir: &Path, file_name: &str) -> PathBu
188189
/// The garbage collection will take care of it.
189190
pub fn prepare_session_directory(sess: &Session,
190191
crate_name: &str,
191-
crate_disambiguator: &str) {
192+
crate_disambiguator: &Fingerprint) {
192193
if sess.opts.incremental.is_none() {
193194
return
194195
}
@@ -614,21 +615,15 @@ fn string_to_timestamp(s: &str) -> Result<SystemTime, ()> {
614615

615616
fn crate_path(sess: &Session,
616617
crate_name: &str,
617-
crate_disambiguator: &str)
618+
crate_disambiguator: &Fingerprint)
618619
-> PathBuf {
619-
use std::hash::{Hasher, Hash};
620-
use std::collections::hash_map::DefaultHasher;
621620

622621
let incr_dir = sess.opts.incremental.as_ref().unwrap().clone();
623622

624-
// The full crate disambiguator is really long. A hash of it should be
625-
// sufficient.
626-
let mut hasher = DefaultHasher::new();
627-
crate_disambiguator.hash(&mut hasher);
628-
623+
let crate_disambiguator = crate_disambiguator.to_smaller_hash();
629624
let crate_name = format!("{}-{}",
630625
crate_name,
631-
base_n::encode(hasher.finish(), INT_ENCODE_BASE));
626+
base_n::encode(crate_disambiguator, INT_ENCODE_BASE));
632627
incr_dir.join(crate_name)
633628
}
634629

src/librustc_metadata/creader.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use locator::{self, CratePaths};
1515
use native_libs::relevant_lib;
1616
use schema::CrateRoot;
1717

18+
use rustc::ich::Fingerprint;
1819
use rustc::hir::def_id::{CrateNum, DefIndex, CRATE_DEF_INDEX};
1920
use rustc::hir::svh::Svh;
2021
use rustc::middle::allocator::AllocatorKind;
@@ -626,7 +627,7 @@ impl<'a> CrateLoader<'a> {
626627
pub fn find_plugin_registrar(&mut self,
627628
span: Span,
628629
name: &str)
629-
-> Option<(PathBuf, Symbol, DefIndex)> {
630+
-> Option<(PathBuf, Fingerprint, DefIndex)> {
630631
let ekrate = self.read_extension_crate(span, &ExternCrateInfo {
631632
name: Symbol::intern(name),
632633
ident: Symbol::intern(name),

src/librustc_metadata/cstore.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use schema;
1515

16+
use rustc::ich::Fingerprint;
1617
use rustc::hir::def_id::{CRATE_DEF_INDEX, CrateNum, DefIndex};
1718
use rustc::hir::map::definitions::DefPathTable;
1819
use rustc::hir::svh::Svh;
@@ -171,7 +172,7 @@ impl CrateMetadata {
171172
pub fn hash(&self) -> Svh {
172173
self.root.hash
173174
}
174-
pub fn disambiguator(&self) -> Symbol {
175+
pub fn disambiguator(&self) -> Fingerprint {
175176
self.root.disambiguator
176177
}
177178

src/librustc_metadata/cstore_impl.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc::middle::cstore::{CrateStore, DepKind,
2020
LoadedMacro, EncodedMetadata,
2121
EncodedMetadataHashes, NativeLibraryKind};
2222
use rustc::middle::stability::DeprecationEntry;
23+
use rustc::ich::Fingerprint;
2324
use rustc::hir::def;
2425
use rustc::session::Session;
2526
use rustc::ty::{self, TyCtxt};
@@ -384,7 +385,7 @@ impl CrateStore for cstore::CStore {
384385
self.get_crate_data(cnum).name
385386
}
386387

387-
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> Symbol
388+
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> Fingerprint
388389
{
389390
self.get_crate_data(cnum).disambiguator()
390391
}

src/librustc_metadata/schema.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use index;
1414
use rustc::hir;
1515
use rustc::hir::def::{self, CtorKind};
1616
use rustc::hir::def_id::{DefIndex, DefId, CrateNum};
17-
use rustc::ich::StableHashingContext;
17+
use rustc::ich::{Fingerprint, StableHashingContext};
1818
use rustc::middle::cstore::{DepKind, LinkagePreference, NativeLibrary};
1919
use rustc::middle::lang_items;
2020
use rustc::mir;
@@ -191,7 +191,7 @@ pub struct CrateRoot {
191191
pub name: Symbol,
192192
pub triple: String,
193193
pub hash: hir::svh::Svh,
194-
pub disambiguator: Symbol,
194+
pub disambiguator: Fingerprint,
195195
pub panic_strategy: PanicStrategy,
196196
pub has_global_allocator: bool,
197197
pub has_default_lib_allocator: bool,

src/librustc_resolve/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ impl<'a> Resolver<'a> {
14171417

14181418
let mut definitions = Definitions::new();
14191419
DefCollector::new(&mut definitions, Mark::root())
1420-
.collect_root(crate_name, &session.local_crate_disambiguator().as_str());
1420+
.collect_root(crate_name, &session.local_crate_disambiguator().to_hex());
14211421

14221422
let mut invocations = FxHashMap();
14231423
invocations.insert(Mark::root(),

src/librustc_trans/back/symbol_export.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn threshold(tcx: TyCtxt) -> SymbolExportLevel {
3434
pub fn metadata_symbol_name(tcx: TyCtxt) -> String {
3535
format!("rust_metadata_{}_{}",
3636
tcx.crate_name(LOCAL_CRATE),
37-
tcx.crate_disambiguator(LOCAL_CRATE))
37+
tcx.crate_disambiguator(LOCAL_CRATE).to_hex())
3838
}
3939

4040
fn crate_export_threshold(crate_type: config::CrateType) -> SymbolExportLevel {

src/librustc_trans/back/symbol_names.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ fn get_symbol_hash<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
220220

221221
if avoid_cross_crate_conflicts {
222222
hasher.hash(tcx.crate_name.as_str());
223-
hasher.hash(tcx.sess.local_crate_disambiguator().as_str());
223+
hasher.hash(tcx.sess.local_crate_disambiguator());
224224
}
225225
});
226226

0 commit comments

Comments
 (0)