Skip to content

Commit

Permalink
Merge CrateDisambiguator into StableCrateId
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed May 30, 2021
1 parent d93b6a4 commit d0ec85d
Show file tree
Hide file tree
Showing 30 changed files with 125 additions and 207 deletions.
4 changes: 1 addition & 3 deletions compiler/rustc_hir/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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 +337,7 @@ impl Definitions {
}

/// Adds a root definition (no parent) and a few other reserved definitions.
pub fn new(crate_name: &str, crate_disambiguator: CrateDisambiguator) -> Definitions {
pub fn new(stable_crate_id: StableCrateId) -> Definitions {
let key = DefKey {
parent: None,
disambiguated_data: DisambiguatedDefPathData {
Expand All @@ -347,7 +346,6 @@ 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
19 changes: 6 additions & 13 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::{CrateDisambiguator, Session};
use rustc_session::{Session, StableCrateId};

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,
crate_disambiguator: CrateDisambiguator,
stable_crate_id: StableCrateId,
) -> 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, crate_disambiguator);
let crate_dir = crate_path(sess, crate_name, stable_crate_id);
debug!("crate-dir: {}", crate_dir.display());
create_dir(sess, &crate_dir, "crate")?;

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

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

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

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

Expand Down
12 changes: 8 additions & 4 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::LOCAL_CRATE;
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
use rustc_hir::Crate;
use rustc_lint::LintStore;
use rustc_metadata::creader::CStore;
Expand Down Expand Up @@ -170,9 +170,13 @@ pub fn register_plugins<'a>(
let crate_types = util::collect_crate_types(sess, &krate.attrs);
sess.init_crate_types(crate_types);

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)?;
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)?;

if sess.opts.incremental.is_some() {
sess.time("incr_comp_garbage_collect_session_directories", || {
Expand Down
36 changes: 0 additions & 36 deletions compiler/rustc_interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ 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 @@ -18,7 +16,6 @@ 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 @@ -496,39 +493,6 @@ 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: 7 additions & 12 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::{CrateDisambiguator, Session};
use rustc_session::Session;
use rustc_span::edition::Edition;
use rustc_span::symbol::{sym, Symbol};
use rustc_span::{Span, DUMMY_SP};
Expand Down Expand Up @@ -222,10 +222,8 @@ 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(local_crate_stable_id, LOCAL_CRATE);
stable_crate_ids.insert(sess.local_stable_crate_id(), LOCAL_CRATE);

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

fn verify_no_symbol_conflicts(&self, root: &CrateRoot<'_>) -> Result<(), CrateError> {
// Check for (potential) conflicts with the local crate
if self.local_crate_name == root.name()
&& self.sess.local_crate_disambiguator() == root.disambiguator()
{
if self.sess.local_stable_crate_id() == root.stable_crate_id() {
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.name() == root.name() && // same crate-name
other.disambiguator() == root.disambiguator() && // same crate-disambiguator
if other.stable_crate_id() == root.stable_crate_id() && // same stable crate id
other.hash() != root.hash()
{
// but different SVH
Expand Down Expand Up @@ -411,7 +406,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.disambiguator())?)
Some(self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.stable_crate_id())?)
} else {
None
};
Expand Down Expand Up @@ -664,7 +659,7 @@ impl<'a> CrateLoader<'a> {
fn dlsym_proc_macros(
&self,
path: &Path,
disambiguator: CrateDisambiguator,
stable_crate_id: StableCrateId,
) -> 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 @@ -673,7 +668,7 @@ impl<'a> CrateLoader<'a> {
Err(s) => return Err(CrateError::DlOpen(s)),
};

let sym = self.sess.generate_proc_macro_decls_symbol(disambiguator);
let sym = self.sess.generate_proc_macro_decls_symbol(stable_crate_id);
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::{CrateDisambiguator, Session};
use rustc_session::{Session, StableCrateId};
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, CrateDisambiguator) {
) -> (PathBuf, StableCrateId) {
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, CrateDisambiguator), CrateError> {
) -> Result<(PathBuf, StableCrateId), 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().disambiguator())),
Some(dylib) => Ok((dylib.0, library.metadata.get_root().stable_crate_id())),
None => Err(CrateError::NonDylibPlugin(name)),
},
None => Err(locator.into_error()),
Expand Down
8 changes: 2 additions & 6 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,6 @@ impl CrateRoot<'_> {
self.name
}

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

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

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

crate fn hash(&self) -> Svh {
Expand Down
7 changes: 3 additions & 4 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::{CrateDisambiguator, Session};
use rustc_session::{Session, StableCrateId};
use rustc_span::source_map::{Span, Spanned};
use rustc_span::symbol::Symbol;

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

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

fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh {
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,6 @@ 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: 0 additions & 2 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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 @@ -202,7 +201,6 @@ 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, 18);
static_assert_size!(DepNode, 17);

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
static_assert_size!(DepNode, 24);
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use rustc_hir::intravisit::Visitor;
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::*;
use rustc_index::vec::Idx;
use rustc_span::def_id::StableCrateId;
use rustc_span::hygiene::MacroKind;
use rustc_span::source_map::Spanned;
use rustc_span::symbol::{kw, Ident, Symbol};
Expand Down Expand Up @@ -990,25 +991,24 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
upstream_crates.hash_stable(&mut hcx, &mut stable_hasher);
source_file_names.hash_stable(&mut hcx, &mut stable_hasher);
tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);
tcx.sess.local_crate_disambiguator().to_fingerprint().hash_stable(&mut hcx, &mut stable_hasher);
tcx.sess.local_stable_crate_id().hash_stable(&mut hcx, &mut stable_hasher);
tcx.untracked_crate.non_exported_macro_attrs.hash_stable(&mut hcx, &mut stable_hasher);

let crate_hash: Fingerprint = stable_hasher.finish();
Svh::new(crate_hash.to_smaller_hash())
}

fn upstream_crates(cstore: &dyn CrateStore) -> Vec<(Symbol, Fingerprint, Svh)> {
fn upstream_crates(cstore: &dyn CrateStore) -> Vec<(StableCrateId, Svh)> {
let mut upstream_crates: Vec<_> = cstore
.crates_untracked()
.iter()
.map(|&cnum| {
let name = cstore.crate_name_untracked(cnum);
let disambiguator = cstore.crate_disambiguator_untracked(cnum).to_fingerprint();
let stable_crate_id = cstore.stable_crate_id_untracked(cnum);
let hash = cstore.crate_hash_untracked(cnum);
(name, disambiguator, hash)
(stable_crate_id, hash)
})
.collect();
upstream_crates.sort_unstable_by_key(|&(name, dis, _)| (name.as_str(), dis));
upstream_crates.sort_unstable_by_key(|&(stable_crate_id, _)| stable_crate_id);
upstream_crates
}

Expand Down
Loading

0 comments on commit d0ec85d

Please sign in to comment.