Skip to content

Commit 2312ff1

Browse files
committed
Auto merge of #85891 - bjorn3:revert_merge_crate_disambiguator, r=Mark-Simulacrum
Revert "Merge CrateDisambiguator into StableCrateId" This reverts #85804
2 parents 022720b + 8176ab8 commit 2312ff1

File tree

69 files changed

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

+307
-224
lines changed

compiler/rustc_hir/src/definitions.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ 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;
1718
use rustc_span::hygiene::ExpnId;
1819
use rustc_span::symbol::{kw, sym, Symbol};
1920

@@ -338,7 +339,7 @@ impl Definitions {
338339
}
339340

340341
/// Adds a root definition (no parent) and a few other reserved definitions.
341-
pub fn new(stable_crate_id: StableCrateId) -> Definitions {
342+
pub fn new(crate_name: &str, crate_disambiguator: CrateDisambiguator) -> Definitions {
342343
let key = DefKey {
343344
parent: None,
344345
disambiguated_data: DisambiguatedDefPathData {
@@ -347,6 +348,7 @@ impl Definitions {
347348
},
348349
};
349350

351+
let stable_crate_id = StableCrateId::new(crate_name, crate_disambiguator);
350352
let parent_hash = DefPathHash::new(stable_crate_id, 0);
351353
let def_path_hash = key.compute_stable_hash(parent_hash);
352354

compiler/rustc_hir/src/tests.rs

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

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

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

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

2022
assert_ne!(h0.stable_crate_id(), h1.stable_crate_id());
2123
assert_ne!(h0.local_hash(), h1.local_hash());
2224

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

2629
let key = DefKey {

compiler/rustc_incremental/src/persist/fs.rs

+13-6
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::{Session, StableCrateId};
111+
use rustc_session::{CrateDisambiguator, Session};
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-
stable_crate_id: StableCrateId,
192+
crate_disambiguator: CrateDisambiguator,
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, stable_crate_id);
203+
let crate_dir = crate_path(sess, crate_name, crate_disambiguator);
204204
debug!("crate-dir: {}", crate_dir.display());
205205
create_dir(sess, &crate_dir, "crate")?;
206206

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

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

654-
let stable_crate_id = base_n::encode(stable_crate_id.to_u64() as u128, INT_ENCODE_BASE);
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);
655662

656-
let crate_name = format!("{}-{}", crate_name, stable_crate_id);
663+
let crate_name = format!("{}-{}", crate_name, crate_disambiguator);
657664
incr_dir.join(crate_name)
658665
}
659666

compiler/rustc_interface/src/passes.rs

+4-8
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::{StableCrateId, LOCAL_CRATE};
15+
use rustc_hir::def_id::LOCAL_CRATE;
1616
use rustc_hir::Crate;
1717
use rustc_lint::LintStore;
1818
use rustc_metadata::creader::CStore;
@@ -170,13 +170,9 @@ 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 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)?;
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)?;
180176

181177
if sess.opts.incremental.is_some() {
182178
sess.time("incr_comp_garbage_collect_session_directories", || {

compiler/rustc_interface/src/util.rs

+36
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ 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;
56
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
67
#[cfg(parallel_compiler)]
78
use rustc_data_structures::jobserver;
9+
use rustc_data_structures::stable_hasher::StableHasher;
810
use rustc_data_structures::sync::Lrc;
911
use rustc_errors::registry::Registry;
1012
use rustc_metadata::dynamic_lib::DynamicLibrary;
@@ -16,6 +18,7 @@ use rustc_session::config::{self, CrateType};
1618
use rustc_session::config::{ErrorOutputType, Input, OutputFilenames};
1719
use rustc_session::lint::{self, BuiltinLintDiagnostics, LintBuffer};
1820
use rustc_session::parse::CrateConfig;
21+
use rustc_session::CrateDisambiguator;
1922
use rustc_session::{early_error, filesearch, output, DiagnosticOutput, Session};
2023
use rustc_span::edition::Edition;
2124
use rustc_span::lev_distance::find_best_match_for_name;
@@ -484,6 +487,39 @@ pub fn get_codegen_sysroot(
484487
}
485488
}
486489

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+
487523
pub(crate) fn check_attr_crate_type(
488524
sess: &Session,
489525
attrs: &[ast::Attribute],

compiler/rustc_metadata/src/creader.rs

+12-7
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::Session;
24+
use rustc_session::{CrateDisambiguator, Session};
2525
use rustc_span::edition::Edition;
2626
use rustc_span::symbol::{sym, Symbol};
2727
use rustc_span::{Span, DUMMY_SP};
@@ -222,8 +222,10 @@ 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());
225227
let mut stable_crate_ids = FxHashMap::default();
226-
stable_crate_ids.insert(sess.local_stable_crate_id(), LOCAL_CRATE);
228+
stable_crate_ids.insert(local_crate_stable_id, LOCAL_CRATE);
227229

228230
CrateLoader {
229231
sess,
@@ -325,14 +327,17 @@ impl<'a> CrateLoader<'a> {
325327

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

332336
// Check for conflicts with any crate loaded so far
333337
let mut res = Ok(());
334338
self.cstore.iter_crate_data(|_, other| {
335-
if other.stable_crate_id() == root.stable_crate_id() && // same stable crate id
339+
if other.name() == root.name() && // same crate-name
340+
other.disambiguator() == root.disambiguator() && // same crate-disambiguator
336341
other.hash() != root.hash()
337342
{
338343
// but different SVH
@@ -406,7 +411,7 @@ impl<'a> CrateLoader<'a> {
406411
None => (&source, &crate_root),
407412
};
408413
let dlsym_dylib = dlsym_source.dylib.as_ref().expect("no dylib for a proc-macro crate");
409-
Some(self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.stable_crate_id())?)
414+
Some(self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.disambiguator())?)
410415
} else {
411416
None
412417
};
@@ -659,7 +664,7 @@ impl<'a> CrateLoader<'a> {
659664
fn dlsym_proc_macros(
660665
&self,
661666
path: &Path,
662-
stable_crate_id: StableCrateId,
667+
disambiguator: CrateDisambiguator,
663668
) -> Result<&'static [ProcMacro], CrateError> {
664669
// Make sure the path contains a / or the linker will search for it.
665670
let path = env::current_dir().unwrap().join(path);
@@ -668,7 +673,7 @@ impl<'a> CrateLoader<'a> {
668673
Err(s) => return Err(CrateError::DlOpen(s)),
669674
};
670675

671-
let sym = self.sess.generate_proc_macro_decls_symbol(stable_crate_id);
676+
let sym = self.sess.generate_proc_macro_decls_symbol(disambiguator);
672677
let decls = unsafe {
673678
let sym = match lib.symbol(&sym) {
674679
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::{Session, StableCrateId};
229+
use rustc_session::{CrateDisambiguator, Session};
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, StableCrateId) {
790+
) -> (PathBuf, CrateDisambiguator) {
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, StableCrateId), CrateError> {
802+
) -> Result<(PathBuf, CrateDisambiguator), 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().stable_crate_id())),
819+
Some(dylib) => Ok((dylib.0, library.metadata.get_root().disambiguator())),
820820
None => Err(CrateError::NonDylibPlugin(name)),
821821
},
822822
None => Err(locator.into_error()),

compiler/rustc_metadata/src/rmeta/decoder.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,10 @@ impl CrateRoot<'_> {
620620
self.name
621621
}
622622

623+
crate fn disambiguator(&self) -> CrateDisambiguator {
624+
self.disambiguator
625+
}
626+
623627
crate fn hash(&self) -> Svh {
624628
self.hash
625629
}
@@ -1923,8 +1927,8 @@ impl CrateMetadata {
19231927
self.root.name
19241928
}
19251929

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

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

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

+4-3
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::{Session, StableCrateId};
22+
use rustc_session::{CrateDisambiguator, Session};
2323
use rustc_span::source_map::{Span, Spanned};
2424
use rustc_span::symbol::Symbol;
2525

@@ -186,6 +186,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
186186
}
187187
native_libraries => { Lrc::new(cdata.get_native_libraries(tcx.sess)) }
188188
foreign_modules => { cdata.get_foreign_modules(tcx) }
189+
crate_disambiguator => { cdata.root.disambiguator }
189190
crate_hash => { cdata.root.hash }
190191
crate_host_hash => { cdata.host_hash }
191192
crate_name => { cdata.root.name }
@@ -488,8 +489,8 @@ impl CrateStore for CStore {
488489
self.get_crate_data(cnum).root.name
489490
}
490491

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

495496
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,6 +671,7 @@ 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(),
674675
stable_crate_id: tcx.def_path_hash(LOCAL_CRATE.as_def_id()).stable_crate_id(),
675676
panic_strategy: tcx.sess.panic_strategy(),
676677
edition: tcx.sess.edition(),

compiler/rustc_metadata/src/rmeta/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ 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;
2122
use rustc_span::edition::Edition;
2223
use rustc_span::hygiene::MacroKind;
2324
use rustc_span::symbol::{Ident, Symbol};
@@ -201,6 +202,7 @@ crate struct CrateRoot<'tcx> {
201202
triple: TargetTriple,
202203
extra_filename: String,
203204
hash: Svh,
205+
disambiguator: CrateDisambiguator,
204206
stable_crate_id: StableCrateId,
205207
panic_strategy: PanicStrategy,
206208
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, 17);
288+
static_assert_size!(DepNode, 18);
289289

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

0 commit comments

Comments
 (0)