Skip to content

Commit 5957990

Browse files
committed
Auto merge of #85804 - bjorn3:merge_crate_disambiguator, r=cjgillot
Merge CrateDisambiguator into StableCrateId This simplifies the code and potentially improves performance by reducing the amount of hashed data. Fixes #85795
2 parents 2023cc3 + e0e0cfa commit 5957990

File tree

69 files changed

+222
-307
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+222
-307
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

@@ -338,7 +337,7 @@ impl Definitions {
338337
}
339338

340339
/// Adds a root definition (no parent) and a few other reserved definitions.
341-
pub fn new(crate_name: &str, crate_disambiguator: CrateDisambiguator) -> Definitions {
340+
pub fn new(stable_crate_id: StableCrateId) -> Definitions {
342341
let key = DefKey {
343342
parent: None,
344343
disambiguated_data: DisambiguatedDefPathData {
@@ -347,7 +346,6 @@ impl Definitions {
347346
},
348347
};
349348

350-
let stable_crate_id = StableCrateId::new(crate_name, crate_disambiguator);
351349
let parent_hash = DefPathHash::new(stable_crate_id, 0);
352350
let def_path_hash = key.compute_stable_hash(parent_hash);
353351

compiler/rustc_hir/src/tests.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use crate::definitions::{DefKey, DefPathData, DisambiguatedDefPathData};
2-
use rustc_data_structures::fingerprint::Fingerprint;
3-
use rustc_span::crate_disambiguator::CrateDisambiguator;
42
use rustc_span::def_id::{DefPathHash, StableCrateId};
53

64
#[test]
@@ -13,17 +11,16 @@ fn def_path_hash_depends_on_crate_id() {
1311
// the crate by changing the crate disambiguator (e.g. via bumping the
1412
// crate's version number).
1513

16-
let d0 = CrateDisambiguator::from(Fingerprint::new(12, 34));
17-
let d1 = CrateDisambiguator::from(Fingerprint::new(56, 78));
14+
let id0 = StableCrateId::new("foo", false, vec!["1".to_string()]);
15+
let id1 = StableCrateId::new("foo", false, vec!["2".to_string()]);
1816

19-
let h0 = mk_test_hash("foo", d0);
20-
let h1 = mk_test_hash("foo", d1);
17+
let h0 = mk_test_hash(id0);
18+
let h1 = mk_test_hash(id1);
2119

2220
assert_ne!(h0.stable_crate_id(), h1.stable_crate_id());
2321
assert_ne!(h0.local_hash(), h1.local_hash());
2422

25-
fn mk_test_hash(crate_name: &str, crate_disambiguator: CrateDisambiguator) -> DefPathHash {
26-
let stable_crate_id = StableCrateId::new(crate_name, crate_disambiguator);
23+
fn mk_test_hash(stable_crate_id: StableCrateId) -> DefPathHash {
2724
let parent_hash = DefPathHash::new(stable_crate_id, 0);
2825

2926
let key = DefKey {

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
@@ -12,7 +12,7 @@ use rustc_data_structures::temp_dir::MaybeTempDir;
1212
use rustc_data_structures::{box_region_allow_access, declare_box_region_type, parallel};
1313
use rustc_errors::{ErrorReported, PResult};
1414
use rustc_expand::base::ExtCtxt;
15-
use rustc_hir::def_id::LOCAL_CRATE;
15+
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
1616
use rustc_hir::Crate;
1717
use rustc_lint::LintStore;
1818
use rustc_metadata::creader::CStore;
@@ -170,9 +170,13 @@ pub fn register_plugins<'a>(
170170
let crate_types = util::collect_crate_types(sess, &krate.attrs);
171171
sess.init_crate_types(crate_types);
172172

173-
let disambiguator = util::compute_crate_disambiguator(sess);
174-
sess.crate_disambiguator.set(disambiguator).expect("not yet initialized");
175-
rustc_incremental::prepare_session_directory(sess, &crate_name, disambiguator)?;
173+
let stable_crate_id = StableCrateId::new(
174+
crate_name,
175+
sess.crate_types().contains(&CrateType::Executable),
176+
sess.opts.cg.metadata.clone(),
177+
);
178+
sess.stable_crate_id.set(stable_crate_id).expect("not yet initialized");
179+
rustc_incremental::prepare_session_directory(sess, &crate_name, stable_crate_id)?;
176180

177181
if sess.opts.incremental.is_some() {
178182
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;
@@ -496,39 +493,6 @@ pub fn get_codegen_sysroot(
496493
}
497494
}
498495

499-
pub(crate) fn compute_crate_disambiguator(session: &Session) -> CrateDisambiguator {
500-
use std::hash::Hasher;
501-
502-
// The crate_disambiguator is a 128 bit hash. The disambiguator is fed
503-
// into various other hashes quite a bit (symbol hashes, incr. comp. hashes,
504-
// debuginfo type IDs, etc), so we don't want it to be too wide. 128 bits
505-
// should still be safe enough to avoid collisions in practice.
506-
let mut hasher = StableHasher::new();
507-
508-
let mut metadata = session.opts.cg.metadata.clone();
509-
// We don't want the crate_disambiguator to dependent on the order
510-
// -C metadata arguments, so sort them:
511-
metadata.sort();
512-
// Every distinct -C metadata value is only incorporated once:
513-
metadata.dedup();
514-
515-
hasher.write(b"metadata");
516-
for s in &metadata {
517-
// Also incorporate the length of a metadata string, so that we generate
518-
// different values for `-Cmetadata=ab -Cmetadata=c` and
519-
// `-Cmetadata=a -Cmetadata=bc`
520-
hasher.write_usize(s.len());
521-
hasher.write(s.as_bytes());
522-
}
523-
524-
// Also incorporate crate type, so that we don't get symbol conflicts when
525-
// linking against a library of the same name, if this is an executable.
526-
let is_exe = session.crate_types().contains(&CrateType::Executable);
527-
hasher.write(if is_exe { b"exe" } else { b"lib" });
528-
529-
CrateDisambiguator::from(hasher.finish::<Fingerprint>())
530-
}
531-
532496
pub(crate) fn check_attr_crate_type(
533497
sess: &Session,
534498
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: &'a 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
@@ -620,10 +620,6 @@ impl CrateRoot<'_> {
620620
self.name
621621
}
622622

623-
crate fn disambiguator(&self) -> CrateDisambiguator {
624-
self.disambiguator
625-
}
626-
627623
crate fn hash(&self) -> Svh {
628624
self.hash
629625
}
@@ -1927,8 +1923,8 @@ impl CrateMetadata {
19271923
self.root.name
19281924
}
19291925

1930-
crate fn disambiguator(&self) -> CrateDisambiguator {
1931-
self.root.disambiguator
1926+
crate fn stable_crate_id(&self) -> StableCrateId {
1927+
self.root.stable_crate_id
19321928
}
19331929

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

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

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

@@ -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
original_crate_name => { cdata.root.name }
@@ -482,8 +481,8 @@ impl CrateStore for CStore {
482481
self.get_crate_data(cnum).private_dep
483482
}
484483

485-
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator {
486-
self.get_crate_data(cnum).root.disambiguator
484+
fn stable_crate_id_untracked(&self, cnum: CrateNum) -> StableCrateId {
485+
self.get_crate_data(cnum).root.stable_crate_id
487486
}
488487

489488
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/dep_graph/dep_node.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ pub type DepNode = rustc_query_system::dep_graph::DepNode<DepKind>;
285285
// required that their size stay the same, but we don't want to change
286286
// it inadvertently. This assert just ensures we're aware of any change.
287287
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
288-
static_assert_size!(DepNode, 18);
288+
static_assert_size!(DepNode, 17);
289289

290290
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
291291
static_assert_size!(DepNode, 24);

0 commit comments

Comments
 (0)