Skip to content

Commit 489ad8b

Browse files
committed
Revert "Revert "Merge CrateDisambiguator into StableCrateId""
This reverts commit 8176ab8.
1 parent 9a27044 commit 489ad8b

File tree

28 files changed

+116
-193
lines changed

28 files changed

+116
-193
lines changed

compiler/rustc_hir/src/definitions.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use rustc_data_structures::fx::FxHashMap;
1414
use rustc_data_structures::stable_hasher::StableHasher;
1515
use rustc_data_structures::unhash::UnhashMap;
1616
use rustc_index::vec::IndexVec;
17-
use rustc_span::crate_disambiguator::CrateDisambiguator;
1817
use rustc_span::hygiene::ExpnId;
1918
use rustc_span::symbol::{kw, sym, Symbol};
2019

@@ -330,7 +329,7 @@ impl Definitions {
330329
}
331330

332331
/// Adds a root definition (no parent) and a few other reserved definitions.
333-
pub fn new(crate_name: &str, crate_disambiguator: CrateDisambiguator) -> Definitions {
332+
pub fn new(stable_crate_id: StableCrateId) -> Definitions {
334333
let key = DefKey {
335334
parent: None,
336335
disambiguated_data: DisambiguatedDefPathData {
@@ -339,7 +338,6 @@ impl Definitions {
339338
},
340339
};
341340

342-
let stable_crate_id = StableCrateId::new(crate_name, crate_disambiguator);
343341
let parent_hash = DefPathHash::new(stable_crate_id, 0);
344342
let def_path_hash = key.compute_stable_hash(parent_hash);
345343

compiler/rustc_incremental/src/persist/fs.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ use rustc_data_structures::svh::Svh;
108108
use rustc_data_structures::{base_n, flock};
109109
use rustc_errors::ErrorReported;
110110
use rustc_fs_util::{link_or_copy, LinkOrCopy};
111-
use rustc_session::{CrateDisambiguator, Session};
111+
use rustc_session::{Session, StableCrateId};
112112

113113
use std::fs as std_fs;
114114
use std::io;
@@ -189,7 +189,7 @@ pub fn in_incr_comp_dir(incr_comp_session_dir: &Path, file_name: &str) -> PathBu
189189
pub fn prepare_session_directory(
190190
sess: &Session,
191191
crate_name: &str,
192-
crate_disambiguator: CrateDisambiguator,
192+
stable_crate_id: StableCrateId,
193193
) -> Result<(), ErrorReported> {
194194
if sess.opts.incremental.is_none() {
195195
return Ok(());
@@ -200,7 +200,7 @@ pub fn prepare_session_directory(
200200
debug!("prepare_session_directory");
201201

202202
// {incr-comp-dir}/{crate-name-and-disambiguator}
203-
let crate_dir = crate_path(sess, crate_name, crate_disambiguator);
203+
let crate_dir = crate_path(sess, crate_name, stable_crate_id);
204204
debug!("crate-dir: {}", crate_dir.display());
205205
create_dir(sess, &crate_dir, "crate")?;
206206

@@ -648,19 +648,12 @@ fn string_to_timestamp(s: &str) -> Result<SystemTime, ()> {
648648
Ok(UNIX_EPOCH + duration)
649649
}
650650

651-
fn crate_path(
652-
sess: &Session,
653-
crate_name: &str,
654-
crate_disambiguator: CrateDisambiguator,
655-
) -> PathBuf {
651+
fn crate_path(sess: &Session, crate_name: &str, stable_crate_id: StableCrateId) -> PathBuf {
656652
let incr_dir = sess.opts.incremental.as_ref().unwrap().clone();
657653

658-
// The full crate disambiguator is really long. 64 bits of it should be
659-
// sufficient.
660-
let crate_disambiguator = crate_disambiguator.to_fingerprint().to_smaller_hash();
661-
let crate_disambiguator = base_n::encode(crate_disambiguator as u128, INT_ENCODE_BASE);
654+
let stable_crate_id = base_n::encode(stable_crate_id.to_u64() as u128, INT_ENCODE_BASE);
662655

663-
let crate_name = format!("{}-{}", crate_name, crate_disambiguator);
656+
let crate_name = format!("{}-{}", crate_name, stable_crate_id);
664657
incr_dir.join(crate_name)
665658
}
666659

compiler/rustc_interface/src/passes.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_data_structures::sync::{par_iter, Lrc, OnceCell, ParallelIterator, Wor
1111
use rustc_data_structures::temp_dir::MaybeTempDir;
1212
use rustc_errors::{ErrorReported, PResult};
1313
use rustc_expand::base::ExtCtxt;
14-
use rustc_hir::def_id::LOCAL_CRATE;
14+
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
1515
use rustc_hir::Crate;
1616
use rustc_lint::LintStore;
1717
use rustc_metadata::creader::CStore;
@@ -195,9 +195,13 @@ pub fn register_plugins<'a>(
195195
let crate_types = util::collect_crate_types(sess, &krate.attrs);
196196
sess.init_crate_types(crate_types);
197197

198-
let disambiguator = util::compute_crate_disambiguator(sess);
199-
sess.crate_disambiguator.set(disambiguator).expect("not yet initialized");
200-
rustc_incremental::prepare_session_directory(sess, &crate_name, disambiguator)?;
198+
let stable_crate_id = StableCrateId::new(
199+
crate_name,
200+
sess.crate_types().contains(&CrateType::Executable),
201+
sess.opts.cg.metadata.clone(),
202+
);
203+
sess.stable_crate_id.set(stable_crate_id).expect("not yet initialized");
204+
rustc_incremental::prepare_session_directory(sess, &crate_name, stable_crate_id)?;
201205

202206
if sess.opts.incremental.is_some() {
203207
sess.time("incr_comp_garbage_collect_session_directories", || {

compiler/rustc_interface/src/util.rs

-36
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ use rustc_ast::mut_visit::{visit_clobber, MutVisitor, *};
22
use rustc_ast::ptr::P;
33
use rustc_ast::{self as ast, AttrVec, BlockCheckMode};
44
use rustc_codegen_ssa::traits::CodegenBackend;
5-
use rustc_data_structures::fingerprint::Fingerprint;
65
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
76
#[cfg(parallel_compiler)]
87
use rustc_data_structures::jobserver;
9-
use rustc_data_structures::stable_hasher::StableHasher;
108
use rustc_data_structures::sync::Lrc;
119
use rustc_errors::registry::Registry;
1210
use rustc_metadata::dynamic_lib::DynamicLibrary;
@@ -18,7 +16,6 @@ use rustc_session::config::{self, CrateType};
1816
use rustc_session::config::{ErrorOutputType, Input, OutputFilenames};
1917
use rustc_session::lint::{self, BuiltinLintDiagnostics, LintBuffer};
2018
use rustc_session::parse::CrateConfig;
21-
use rustc_session::CrateDisambiguator;
2219
use rustc_session::{early_error, filesearch, output, DiagnosticOutput, Session};
2320
use rustc_span::edition::Edition;
2421
use rustc_span::lev_distance::find_best_match_for_name;
@@ -487,39 +484,6 @@ pub fn get_codegen_sysroot(
487484
}
488485
}
489486

490-
pub(crate) fn compute_crate_disambiguator(session: &Session) -> CrateDisambiguator {
491-
use std::hash::Hasher;
492-
493-
// The crate_disambiguator is a 128 bit hash. The disambiguator is fed
494-
// into various other hashes quite a bit (symbol hashes, incr. comp. hashes,
495-
// debuginfo type IDs, etc), so we don't want it to be too wide. 128 bits
496-
// should still be safe enough to avoid collisions in practice.
497-
let mut hasher = StableHasher::new();
498-
499-
let mut metadata = session.opts.cg.metadata.clone();
500-
// We don't want the crate_disambiguator to dependent on the order
501-
// -C metadata arguments, so sort them:
502-
metadata.sort();
503-
// Every distinct -C metadata value is only incorporated once:
504-
metadata.dedup();
505-
506-
hasher.write(b"metadata");
507-
for s in &metadata {
508-
// Also incorporate the length of a metadata string, so that we generate
509-
// different values for `-Cmetadata=ab -Cmetadata=c` and
510-
// `-Cmetadata=a -Cmetadata=bc`
511-
hasher.write_usize(s.len());
512-
hasher.write(s.as_bytes());
513-
}
514-
515-
// Also incorporate crate type, so that we don't get symbol conflicts when
516-
// linking against a library of the same name, if this is an executable.
517-
let is_exe = session.crate_types().contains(&CrateType::Executable);
518-
hasher.write(if is_exe { b"exe" } else { b"lib" });
519-
520-
CrateDisambiguator::from(hasher.finish::<Fingerprint>())
521-
}
522-
523487
pub(crate) fn check_attr_crate_type(
524488
sess: &Session,
525489
attrs: &[ast::Attribute],

compiler/rustc_metadata/src/creader.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_session::config::{self, CrateType, ExternLocation};
2121
use rustc_session::lint::{self, BuiltinLintDiagnostics, ExternDepSpec};
2222
use rustc_session::output::validate_crate_name;
2323
use rustc_session::search_paths::PathKind;
24-
use rustc_session::{CrateDisambiguator, Session};
24+
use rustc_session::Session;
2525
use rustc_span::edition::Edition;
2626
use rustc_span::symbol::{sym, Symbol};
2727
use rustc_span::{Span, DUMMY_SP};
@@ -222,10 +222,8 @@ impl<'a> CrateLoader<'a> {
222222
metadata_loader: Box<MetadataLoaderDyn>,
223223
local_crate_name: &str,
224224
) -> Self {
225-
let local_crate_stable_id =
226-
StableCrateId::new(local_crate_name, sess.local_crate_disambiguator());
227225
let mut stable_crate_ids = FxHashMap::default();
228-
stable_crate_ids.insert(local_crate_stable_id, LOCAL_CRATE);
226+
stable_crate_ids.insert(sess.local_stable_crate_id(), LOCAL_CRATE);
229227

230228
CrateLoader {
231229
sess,
@@ -327,17 +325,14 @@ impl<'a> CrateLoader<'a> {
327325

328326
fn verify_no_symbol_conflicts(&self, root: &CrateRoot<'_>) -> Result<(), CrateError> {
329327
// Check for (potential) conflicts with the local crate
330-
if self.local_crate_name == root.name()
331-
&& self.sess.local_crate_disambiguator() == root.disambiguator()
332-
{
328+
if self.sess.local_stable_crate_id() == root.stable_crate_id() {
333329
return Err(CrateError::SymbolConflictsCurrent(root.name()));
334330
}
335331

336332
// Check for conflicts with any crate loaded so far
337333
let mut res = Ok(());
338334
self.cstore.iter_crate_data(|_, other| {
339-
if other.name() == root.name() && // same crate-name
340-
other.disambiguator() == root.disambiguator() && // same crate-disambiguator
335+
if other.stable_crate_id() == root.stable_crate_id() && // same stable crate id
341336
other.hash() != root.hash()
342337
{
343338
// but different SVH
@@ -411,7 +406,7 @@ impl<'a> CrateLoader<'a> {
411406
None => (&source, &crate_root),
412407
};
413408
let dlsym_dylib = dlsym_source.dylib.as_ref().expect("no dylib for a proc-macro crate");
414-
Some(self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.disambiguator())?)
409+
Some(self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.stable_crate_id())?)
415410
} else {
416411
None
417412
};
@@ -664,7 +659,7 @@ impl<'a> CrateLoader<'a> {
664659
fn dlsym_proc_macros(
665660
&self,
666661
path: &Path,
667-
disambiguator: CrateDisambiguator,
662+
stable_crate_id: StableCrateId,
668663
) -> Result<&'static [ProcMacro], CrateError> {
669664
// Make sure the path contains a / or the linker will search for it.
670665
let path = env::current_dir().unwrap().join(path);
@@ -673,7 +668,7 @@ impl<'a> CrateLoader<'a> {
673668
Err(s) => return Err(CrateError::DlOpen(s)),
674669
};
675670

676-
let sym = self.sess.generate_proc_macro_decls_symbol(disambiguator);
671+
let sym = self.sess.generate_proc_macro_decls_symbol(stable_crate_id);
677672
let decls = unsafe {
678673
let sym = match lib.symbol(&sym) {
679674
Ok(f) => f,

compiler/rustc_metadata/src/locator.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ use rustc_session::config::{self, CrateType};
226226
use rustc_session::filesearch::{FileDoesntMatch, FileMatches, FileSearch};
227227
use rustc_session::search_paths::PathKind;
228228
use rustc_session::utils::CanonicalizedPath;
229-
use rustc_session::{CrateDisambiguator, Session};
229+
use rustc_session::{Session, StableCrateId};
230230
use rustc_span::symbol::{sym, Symbol};
231231
use rustc_span::Span;
232232
use rustc_target::spec::{Target, TargetTriple};
@@ -787,7 +787,7 @@ pub fn find_plugin_registrar(
787787
metadata_loader: &dyn MetadataLoader,
788788
span: Span,
789789
name: Symbol,
790-
) -> (PathBuf, CrateDisambiguator) {
790+
) -> (PathBuf, StableCrateId) {
791791
match find_plugin_registrar_impl(sess, metadata_loader, name) {
792792
Ok(res) => res,
793793
// `core` is always available if we got as far as loading plugins.
@@ -799,7 +799,7 @@ fn find_plugin_registrar_impl<'a>(
799799
sess: &'a Session,
800800
metadata_loader: &dyn MetadataLoader,
801801
name: Symbol,
802-
) -> Result<(PathBuf, CrateDisambiguator), CrateError> {
802+
) -> Result<(PathBuf, StableCrateId), CrateError> {
803803
info!("find plugin registrar `{}`", name);
804804
let mut locator = CrateLocator::new(
805805
sess,
@@ -816,7 +816,7 @@ fn find_plugin_registrar_impl<'a>(
816816

817817
match locator.maybe_load_library_crate()? {
818818
Some(library) => match library.source.dylib {
819-
Some(dylib) => Ok((dylib.0, library.metadata.get_root().disambiguator())),
819+
Some(dylib) => Ok((dylib.0, library.metadata.get_root().stable_crate_id())),
820820
None => Err(CrateError::NonDylibPlugin(name)),
821821
},
822822
None => Err(locator.into_error()),

compiler/rustc_metadata/src/rmeta/decoder.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,6 @@ impl CrateRoot<'_> {
633633
self.name
634634
}
635635

636-
crate fn disambiguator(&self) -> CrateDisambiguator {
637-
self.disambiguator
638-
}
639-
640636
crate fn hash(&self) -> Svh {
641637
self.hash
642638
}
@@ -1940,8 +1936,8 @@ impl CrateMetadata {
19401936
self.root.name
19411937
}
19421938

1943-
crate fn disambiguator(&self) -> CrateDisambiguator {
1944-
self.root.disambiguator
1939+
crate fn stable_crate_id(&self) -> StableCrateId {
1940+
self.root.stable_crate_id
19451941
}
19461942

19471943
crate fn hash(&self) -> Svh {

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_middle::middle::stability::DeprecationEntry;
1818
use rustc_middle::ty::query::Providers;
1919
use rustc_middle::ty::{self, TyCtxt, Visibility};
2020
use rustc_session::utils::NativeLibKind;
21-
use rustc_session::{CrateDisambiguator, Session};
21+
use rustc_session::{Session, StableCrateId};
2222
use rustc_span::source_map::{Span, Spanned};
2323
use rustc_span::symbol::Symbol;
2424

@@ -185,7 +185,6 @@ provide! { <'tcx> tcx, def_id, other, cdata,
185185
}
186186
native_libraries => { Lrc::new(cdata.get_native_libraries(tcx.sess)) }
187187
foreign_modules => { cdata.get_foreign_modules(tcx) }
188-
crate_disambiguator => { cdata.root.disambiguator }
189188
crate_hash => { cdata.root.hash }
190189
crate_host_hash => { cdata.host_hash }
191190
crate_name => { cdata.root.name }
@@ -489,8 +488,8 @@ impl CrateStore for CStore {
489488
self.get_crate_data(cnum).root.name
490489
}
491490

492-
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator {
493-
self.get_crate_data(cnum).root.disambiguator
491+
fn stable_crate_id_untracked(&self, cnum: CrateNum) -> StableCrateId {
492+
self.get_crate_data(cnum).root.stable_crate_id
494493
}
495494

496495
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh {

compiler/rustc_metadata/src/rmeta/encoder.rs

-1
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
671671
extra_filename: tcx.sess.opts.cg.extra_filename.clone(),
672672
triple: tcx.sess.opts.target_triple.clone(),
673673
hash: tcx.crate_hash(LOCAL_CRATE),
674-
disambiguator: tcx.sess.local_crate_disambiguator(),
675674
stable_crate_id: tcx.def_path_hash(LOCAL_CRATE.as_def_id()).stable_crate_id(),
676675
panic_strategy: tcx.sess.panic_strategy(),
677676
edition: tcx.sess.edition(),

compiler/rustc_metadata/src/rmeta/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use rustc_middle::mir;
1818
use rustc_middle::ty::{self, ReprOptions, Ty};
1919
use rustc_serialize::opaque::Encoder;
2020
use rustc_session::config::SymbolManglingVersion;
21-
use rustc_session::CrateDisambiguator;
2221
use rustc_span::edition::Edition;
2322
use rustc_span::hygiene::MacroKind;
2423
use rustc_span::symbol::{Ident, Symbol};
@@ -202,7 +201,6 @@ crate struct CrateRoot<'tcx> {
202201
triple: TargetTriple,
203202
extra_filename: String,
204203
hash: Svh,
205-
disambiguator: CrateDisambiguator,
206204
stable_crate_id: StableCrateId,
207205
panic_strategy: PanicStrategy,
208206
edition: Edition,

compiler/rustc_middle/src/hir/map/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_hir::intravisit::Visitor;
1515
use rustc_hir::itemlikevisit::ItemLikeVisitor;
1616
use rustc_hir::*;
1717
use rustc_index::vec::Idx;
18+
use rustc_span::def_id::StableCrateId;
1819
use rustc_span::hygiene::MacroKind;
1920
use rustc_span::source_map::Spanned;
2021
use rustc_span::symbol::{kw, Ident, Symbol};
@@ -990,25 +991,24 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
990991
upstream_crates.hash_stable(&mut hcx, &mut stable_hasher);
991992
source_file_names.hash_stable(&mut hcx, &mut stable_hasher);
992993
tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);
993-
tcx.sess.local_crate_disambiguator().to_fingerprint().hash_stable(&mut hcx, &mut stable_hasher);
994+
tcx.sess.local_stable_crate_id().hash_stable(&mut hcx, &mut stable_hasher);
994995
tcx.untracked_crate.non_exported_macro_attrs.hash_stable(&mut hcx, &mut stable_hasher);
995996

996997
let crate_hash: Fingerprint = stable_hasher.finish();
997998
Svh::new(crate_hash.to_smaller_hash())
998999
}
9991000

1000-
fn upstream_crates(cstore: &dyn CrateStore) -> Vec<(Symbol, Fingerprint, Svh)> {
1001+
fn upstream_crates(cstore: &dyn CrateStore) -> Vec<(StableCrateId, Svh)> {
10011002
let mut upstream_crates: Vec<_> = cstore
10021003
.crates_untracked()
10031004
.iter()
10041005
.map(|&cnum| {
1005-
let name = cstore.crate_name_untracked(cnum);
1006-
let disambiguator = cstore.crate_disambiguator_untracked(cnum).to_fingerprint();
1006+
let stable_crate_id = cstore.stable_crate_id_untracked(cnum);
10071007
let hash = cstore.crate_hash_untracked(cnum);
1008-
(name, disambiguator, hash)
1008+
(stable_crate_id, hash)
10091009
})
10101010
.collect();
1011-
upstream_crates.sort_unstable_by_key(|&(name, dis, _)| (name.as_str(), dis));
1011+
upstream_crates.sort_unstable_by_key(|&(stable_crate_id, _)| stable_crate_id);
10121012
upstream_crates
10131013
}
10141014

compiler/rustc_middle/src/middle/cstore.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
1313
use rustc_macros::HashStable;
1414
use rustc_session::search_paths::PathKind;
1515
use rustc_session::utils::NativeLibKind;
16-
use rustc_session::CrateDisambiguator;
16+
use rustc_session::StableCrateId;
1717
use rustc_span::symbol::Symbol;
1818
use rustc_span::Span;
1919
use rustc_target::spec::Target;
@@ -185,7 +185,7 @@ pub trait CrateStore {
185185

186186
// "queries" used in resolve that aren't tracked for incremental compilation
187187
fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol;
188-
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator;
188+
fn stable_crate_id_untracked(&self, cnum: CrateNum) -> StableCrateId;
189189
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh;
190190

191191
// This is basically a 1-based range of ints, which is a little

0 commit comments

Comments
 (0)