Skip to content

Commit

Permalink
Auto merge of rust-lang#85891 - bjorn3:revert_merge_crate_disambiguat…
Browse files Browse the repository at this point in the history
…or, r=Mark-Simulacrum

Revert "Merge CrateDisambiguator into StableCrateId"

This reverts rust-lang#85804
  • Loading branch information
bors committed Jun 7, 2021
2 parents 022720b + 8176ab8 commit 2312ff1
Show file tree
Hide file tree
Showing 69 changed files with 307 additions and 224 deletions.
4 changes: 3 additions & 1 deletion compiler/rustc_hir/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_data_structures::unhash::UnhashMap;
use rustc_index::vec::IndexVec;
use rustc_span::crate_disambiguator::CrateDisambiguator;
use rustc_span::hygiene::ExpnId;
use rustc_span::symbol::{kw, sym, Symbol};

Expand Down Expand Up @@ -338,7 +339,7 @@ impl Definitions {
}

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

let stable_crate_id = StableCrateId::new(crate_name, crate_disambiguator);
let parent_hash = DefPathHash::new(stable_crate_id, 0);
let def_path_hash = key.compute_stable_hash(parent_hash);

Expand Down
13 changes: 8 additions & 5 deletions compiler/rustc_hir/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::definitions::{DefKey, DefPathData, DisambiguatedDefPathData};
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_span::crate_disambiguator::CrateDisambiguator;
use rustc_span::def_id::{DefPathHash, StableCrateId};

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

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

let h0 = mk_test_hash(id0);
let h1 = mk_test_hash(id1);
let h0 = mk_test_hash("foo", d0);
let h1 = mk_test_hash("foo", d1);

assert_ne!(h0.stable_crate_id(), h1.stable_crate_id());
assert_ne!(h0.local_hash(), h1.local_hash());

fn mk_test_hash(stable_crate_id: StableCrateId) -> DefPathHash {
fn mk_test_hash(crate_name: &str, crate_disambiguator: CrateDisambiguator) -> DefPathHash {
let stable_crate_id = StableCrateId::new(crate_name, crate_disambiguator);
let parent_hash = DefPathHash::new(stable_crate_id, 0);

let key = DefKey {
Expand Down
19 changes: 13 additions & 6 deletions compiler/rustc_incremental/src/persist/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ use rustc_data_structures::svh::Svh;
use rustc_data_structures::{base_n, flock};
use rustc_errors::ErrorReported;
use rustc_fs_util::{link_or_copy, LinkOrCopy};
use rustc_session::{Session, StableCrateId};
use rustc_session::{CrateDisambiguator, Session};

use std::fs as std_fs;
use std::io;
Expand Down Expand Up @@ -189,7 +189,7 @@ pub fn in_incr_comp_dir(incr_comp_session_dir: &Path, file_name: &str) -> PathBu
pub fn prepare_session_directory(
sess: &Session,
crate_name: &str,
stable_crate_id: StableCrateId,
crate_disambiguator: CrateDisambiguator,
) -> Result<(), ErrorReported> {
if sess.opts.incremental.is_none() {
return Ok(());
Expand All @@ -200,7 +200,7 @@ pub fn prepare_session_directory(
debug!("prepare_session_directory");

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

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

fn crate_path(sess: &Session, crate_name: &str, stable_crate_id: StableCrateId) -> PathBuf {
fn crate_path(
sess: &Session,
crate_name: &str,
crate_disambiguator: CrateDisambiguator,
) -> PathBuf {
let incr_dir = sess.opts.incremental.as_ref().unwrap().clone();

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

let crate_name = format!("{}-{}", crate_name, stable_crate_id);
let crate_name = format!("{}-{}", crate_name, crate_disambiguator);
incr_dir.join(crate_name)
}

Expand Down
12 changes: 4 additions & 8 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_data_structures::temp_dir::MaybeTempDir;
use rustc_data_structures::{box_region_allow_access, declare_box_region_type, parallel};
use rustc_errors::{ErrorReported, PResult};
use rustc_expand::base::ExtCtxt;
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_hir::Crate;
use rustc_lint::LintStore;
use rustc_metadata::creader::CStore;
Expand Down Expand Up @@ -170,13 +170,9 @@ pub fn register_plugins<'a>(
let crate_types = util::collect_crate_types(sess, &krate.attrs);
sess.init_crate_types(crate_types);

let stable_crate_id = StableCrateId::new(
crate_name,
sess.crate_types().contains(&CrateType::Executable),
sess.opts.cg.metadata.clone(),
);
sess.stable_crate_id.set(stable_crate_id).expect("not yet initialized");
rustc_incremental::prepare_session_directory(sess, &crate_name, stable_crate_id)?;
let disambiguator = util::compute_crate_disambiguator(sess);
sess.crate_disambiguator.set(disambiguator).expect("not yet initialized");
rustc_incremental::prepare_session_directory(sess, &crate_name, disambiguator)?;

if sess.opts.incremental.is_some() {
sess.time("incr_comp_garbage_collect_session_directories", || {
Expand Down
36 changes: 36 additions & 0 deletions compiler/rustc_interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use rustc_ast::mut_visit::{visit_clobber, MutVisitor, *};
use rustc_ast::ptr::P;
use rustc_ast::{self as ast, AttrVec, BlockCheckMode};
use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
#[cfg(parallel_compiler)]
use rustc_data_structures::jobserver;
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_data_structures::sync::Lrc;
use rustc_errors::registry::Registry;
use rustc_metadata::dynamic_lib::DynamicLibrary;
Expand All @@ -16,6 +18,7 @@ use rustc_session::config::{self, CrateType};
use rustc_session::config::{ErrorOutputType, Input, OutputFilenames};
use rustc_session::lint::{self, BuiltinLintDiagnostics, LintBuffer};
use rustc_session::parse::CrateConfig;
use rustc_session::CrateDisambiguator;
use rustc_session::{early_error, filesearch, output, DiagnosticOutput, Session};
use rustc_span::edition::Edition;
use rustc_span::lev_distance::find_best_match_for_name;
Expand Down Expand Up @@ -484,6 +487,39 @@ pub fn get_codegen_sysroot(
}
}

pub(crate) fn compute_crate_disambiguator(session: &Session) -> CrateDisambiguator {
use std::hash::Hasher;

// The crate_disambiguator is a 128 bit hash. The disambiguator is fed
// into various other hashes quite a bit (symbol hashes, incr. comp. hashes,
// debuginfo type IDs, etc), so we don't want it to be too wide. 128 bits
// should still be safe enough to avoid collisions in practice.
let mut hasher = StableHasher::new();

let mut metadata = session.opts.cg.metadata.clone();
// We don't want the crate_disambiguator to dependent on the order
// -C metadata arguments, so sort them:
metadata.sort();
// Every distinct -C metadata value is only incorporated once:
metadata.dedup();

hasher.write(b"metadata");
for s in &metadata {
// Also incorporate the length of a metadata string, so that we generate
// different values for `-Cmetadata=ab -Cmetadata=c` and
// `-Cmetadata=a -Cmetadata=bc`
hasher.write_usize(s.len());
hasher.write(s.as_bytes());
}

// Also incorporate crate type, so that we don't get symbol conflicts when
// linking against a library of the same name, if this is an executable.
let is_exe = session.crate_types().contains(&CrateType::Executable);
hasher.write(if is_exe { b"exe" } else { b"lib" });

CrateDisambiguator::from(hasher.finish::<Fingerprint>())
}

pub(crate) fn check_attr_crate_type(
sess: &Session,
attrs: &[ast::Attribute],
Expand Down
19 changes: 12 additions & 7 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use rustc_session::config::{self, CrateType, ExternLocation};
use rustc_session::lint::{self, BuiltinLintDiagnostics, ExternDepSpec};
use rustc_session::output::validate_crate_name;
use rustc_session::search_paths::PathKind;
use rustc_session::Session;
use rustc_session::{CrateDisambiguator, Session};
use rustc_span::edition::Edition;
use rustc_span::symbol::{sym, Symbol};
use rustc_span::{Span, DUMMY_SP};
Expand Down Expand Up @@ -222,8 +222,10 @@ impl<'a> CrateLoader<'a> {
metadata_loader: &'a MetadataLoaderDyn,
local_crate_name: &str,
) -> Self {
let local_crate_stable_id =
StableCrateId::new(local_crate_name, sess.local_crate_disambiguator());
let mut stable_crate_ids = FxHashMap::default();
stable_crate_ids.insert(sess.local_stable_crate_id(), LOCAL_CRATE);
stable_crate_ids.insert(local_crate_stable_id, LOCAL_CRATE);

CrateLoader {
sess,
Expand Down Expand Up @@ -325,14 +327,17 @@ impl<'a> CrateLoader<'a> {

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

// Check for conflicts with any crate loaded so far
let mut res = Ok(());
self.cstore.iter_crate_data(|_, other| {
if other.stable_crate_id() == root.stable_crate_id() && // same stable crate id
if other.name() == root.name() && // same crate-name
other.disambiguator() == root.disambiguator() && // same crate-disambiguator
other.hash() != root.hash()
{
// but different SVH
Expand Down Expand Up @@ -406,7 +411,7 @@ impl<'a> CrateLoader<'a> {
None => (&source, &crate_root),
};
let dlsym_dylib = dlsym_source.dylib.as_ref().expect("no dylib for a proc-macro crate");
Some(self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.stable_crate_id())?)
Some(self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.disambiguator())?)
} else {
None
};
Expand Down Expand Up @@ -659,7 +664,7 @@ impl<'a> CrateLoader<'a> {
fn dlsym_proc_macros(
&self,
path: &Path,
stable_crate_id: StableCrateId,
disambiguator: CrateDisambiguator,
) -> Result<&'static [ProcMacro], CrateError> {
// Make sure the path contains a / or the linker will search for it.
let path = env::current_dir().unwrap().join(path);
Expand All @@ -668,7 +673,7 @@ impl<'a> CrateLoader<'a> {
Err(s) => return Err(CrateError::DlOpen(s)),
};

let sym = self.sess.generate_proc_macro_decls_symbol(stable_crate_id);
let sym = self.sess.generate_proc_macro_decls_symbol(disambiguator);
let decls = unsafe {
let sym = match lib.symbol(&sym) {
Ok(f) => f,
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_metadata/src/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ use rustc_session::config::{self, CrateType};
use rustc_session::filesearch::{FileDoesntMatch, FileMatches, FileSearch};
use rustc_session::search_paths::PathKind;
use rustc_session::utils::CanonicalizedPath;
use rustc_session::{Session, StableCrateId};
use rustc_session::{CrateDisambiguator, Session};
use rustc_span::symbol::{sym, Symbol};
use rustc_span::Span;
use rustc_target::spec::{Target, TargetTriple};
Expand Down Expand Up @@ -787,7 +787,7 @@ pub fn find_plugin_registrar(
metadata_loader: &dyn MetadataLoader,
span: Span,
name: Symbol,
) -> (PathBuf, StableCrateId) {
) -> (PathBuf, CrateDisambiguator) {
match find_plugin_registrar_impl(sess, metadata_loader, name) {
Ok(res) => res,
// `core` is always available if we got as far as loading plugins.
Expand All @@ -799,7 +799,7 @@ fn find_plugin_registrar_impl<'a>(
sess: &'a Session,
metadata_loader: &dyn MetadataLoader,
name: Symbol,
) -> Result<(PathBuf, StableCrateId), CrateError> {
) -> Result<(PathBuf, CrateDisambiguator), CrateError> {
info!("find plugin registrar `{}`", name);
let mut locator = CrateLocator::new(
sess,
Expand All @@ -816,7 +816,7 @@ fn find_plugin_registrar_impl<'a>(

match locator.maybe_load_library_crate()? {
Some(library) => match library.source.dylib {
Some(dylib) => Ok((dylib.0, library.metadata.get_root().stable_crate_id())),
Some(dylib) => Ok((dylib.0, library.metadata.get_root().disambiguator())),
None => Err(CrateError::NonDylibPlugin(name)),
},
None => Err(locator.into_error()),
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,10 @@ impl CrateRoot<'_> {
self.name
}

crate fn disambiguator(&self) -> CrateDisambiguator {
self.disambiguator
}

crate fn hash(&self) -> Svh {
self.hash
}
Expand Down Expand Up @@ -1923,8 +1927,8 @@ impl CrateMetadata {
self.root.name
}

crate fn stable_crate_id(&self) -> StableCrateId {
self.root.stable_crate_id
crate fn disambiguator(&self) -> CrateDisambiguator {
self.root.disambiguator
}

crate fn hash(&self) -> Svh {
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rustc_middle::middle::stability::DeprecationEntry;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, TyCtxt, Visibility};
use rustc_session::utils::NativeLibKind;
use rustc_session::{Session, StableCrateId};
use rustc_session::{CrateDisambiguator, Session};
use rustc_span::source_map::{Span, Spanned};
use rustc_span::symbol::Symbol;

Expand Down Expand Up @@ -186,6 +186,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
}
native_libraries => { Lrc::new(cdata.get_native_libraries(tcx.sess)) }
foreign_modules => { cdata.get_foreign_modules(tcx) }
crate_disambiguator => { cdata.root.disambiguator }
crate_hash => { cdata.root.hash }
crate_host_hash => { cdata.host_hash }
crate_name => { cdata.root.name }
Expand Down Expand Up @@ -488,8 +489,8 @@ impl CrateStore for CStore {
self.get_crate_data(cnum).root.name
}

fn stable_crate_id_untracked(&self, cnum: CrateNum) -> StableCrateId {
self.get_crate_data(cnum).root.stable_crate_id
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator {
self.get_crate_data(cnum).root.disambiguator
}

fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
extra_filename: tcx.sess.opts.cg.extra_filename.clone(),
triple: tcx.sess.opts.target_triple.clone(),
hash: tcx.crate_hash(LOCAL_CRATE),
disambiguator: tcx.sess.local_crate_disambiguator(),
stable_crate_id: tcx.def_path_hash(LOCAL_CRATE.as_def_id()).stable_crate_id(),
panic_strategy: tcx.sess.panic_strategy(),
edition: tcx.sess.edition(),
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use rustc_middle::mir;
use rustc_middle::ty::{self, ReprOptions, Ty};
use rustc_serialize::opaque::Encoder;
use rustc_session::config::SymbolManglingVersion;
use rustc_session::CrateDisambiguator;
use rustc_span::edition::Edition;
use rustc_span::hygiene::MacroKind;
use rustc_span::symbol::{Ident, Symbol};
Expand Down Expand Up @@ -201,6 +202,7 @@ crate struct CrateRoot<'tcx> {
triple: TargetTriple,
extra_filename: String,
hash: Svh,
disambiguator: CrateDisambiguator,
stable_crate_id: StableCrateId,
panic_strategy: PanicStrategy,
edition: Edition,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ pub type DepNode = rustc_query_system::dep_graph::DepNode<DepKind>;
// required that their size stay the same, but we don't want to change
// it inadvertently. This assert just ensures we're aware of any change.
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
static_assert_size!(DepNode, 17);
static_assert_size!(DepNode, 18);

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
static_assert_size!(DepNode, 24);
Expand Down
Loading

0 comments on commit 2312ff1

Please sign in to comment.