1
Fork 0

rustc_metadata: Privatize all fields of CrateRoot

All of them are read-only
This commit is contained in:
Vadim Petrochenkov 2019-11-23 23:46:32 +03:00
parent 765133ac2e
commit a9cef4945f
4 changed files with 105 additions and 57 deletions

View file

@ -47,9 +47,9 @@ pub struct CrateLoader<'a> {
fn dump_crates(cstore: &CStore) { fn dump_crates(cstore: &CStore) {
info!("resolved crates:"); info!("resolved crates:");
cstore.iter_crate_data(|cnum, data| { cstore.iter_crate_data(|cnum, data| {
info!(" name: {}", data.root.name); info!(" name: {}", data.root.name());
info!(" cnum: {}", cnum); info!(" cnum: {}", cnum);
info!(" hash: {}", data.root.hash); info!(" hash: {}", data.root.hash());
info!(" reqd: {:?}", data.dep_kind()); info!(" reqd: {:?}", data.dep_kind());
let CrateSource { dylib, rlib, rmeta } = data.source(); let CrateSource { dylib, rlib, rmeta } = data.source();
dylib.as_ref().map(|dl| info!(" dylib: {}", dl.0.display())); dylib.as_ref().map(|dl| info!(" dylib: {}", dl.0.display()));
@ -101,10 +101,10 @@ impl<'a> CrateLoader<'a> {
-> Option<CrateNum> { -> Option<CrateNum> {
let mut ret = None; let mut ret = None;
self.cstore.iter_crate_data(|cnum, data| { self.cstore.iter_crate_data(|cnum, data| {
if data.root.name != name { return } if data.root.name() != name { return }
match hash { match hash {
Some(hash) if *hash == data.root.hash => { ret = Some(cnum); return } Some(hash) if *hash == data.root.hash() => { ret = Some(cnum); return }
Some(..) => return, Some(..) => return,
None => {} None => {}
} }
@ -152,26 +152,26 @@ impl<'a> CrateLoader<'a> {
span: Span, span: Span,
root: &CrateRoot<'_>) { root: &CrateRoot<'_>) {
// Check for (potential) conflicts with the local crate // Check for (potential) conflicts with the local crate
if self.local_crate_name == root.name && if self.local_crate_name == root.name() &&
self.sess.local_crate_disambiguator() == root.disambiguator { self.sess.local_crate_disambiguator() == root.disambiguator() {
span_fatal!(self.sess, span, E0519, span_fatal!(self.sess, span, E0519,
"the current crate is indistinguishable from one of its \ "the current crate is indistinguishable from one of its \
dependencies: it has the same crate-name `{}` and was \ dependencies: it has the same crate-name `{}` and was \
compiled with the same `-C metadata` arguments. This \ compiled with the same `-C metadata` arguments. This \
will result in symbol conflicts between the two.", will result in symbol conflicts between the two.",
root.name) root.name())
} }
// Check for conflicts with any crate loaded so far // Check for conflicts with any crate loaded so far
self.cstore.iter_crate_data(|_, other| { self.cstore.iter_crate_data(|_, other| {
if other.root.name == root.name && // same crate-name if other.root.name() == root.name() && // same crate-name
other.root.disambiguator == root.disambiguator && // same crate-disambiguator other.root.disambiguator() == root.disambiguator() && // same crate-disambiguator
other.root.hash != root.hash { // but different SVH other.root.hash() != root.hash() { // but different SVH
span_fatal!(self.sess, span, E0523, span_fatal!(self.sess, span, E0523,
"found two different crates with name `{}` that are \ "found two different crates with name `{}` that are \
not distinguished by differing `-C metadata`. This \ not distinguished by differing `-C metadata`. This \
will result in symbol conflicts between the two.", will result in symbol conflicts between the two.",
root.name) root.name())
} }
}); });
} }
@ -189,14 +189,14 @@ impl<'a> CrateLoader<'a> {
let Library { source, metadata } = lib; let Library { source, metadata } = lib;
let crate_root = metadata.get_root(); let crate_root = metadata.get_root();
let host_hash = host_lib.as_ref().map(|lib| lib.metadata.get_root().hash); let host_hash = host_lib.as_ref().map(|lib| lib.metadata.get_root().hash());
self.verify_no_symbol_conflicts(span, &crate_root); self.verify_no_symbol_conflicts(span, &crate_root);
let private_dep = self.sess.opts.externs.get(&name.as_str()) let private_dep = self.sess.opts.externs.get(&name.as_str())
.map(|e| e.is_private_dep) .map(|e| e.is_private_dep)
.unwrap_or(false); .unwrap_or(false);
info!("register crate `{}` (private_dep = {})", crate_root.name, private_dep); info!("register crate `{}` (private_dep = {})", crate_root.name(), private_dep);
// Claim this crate number and cache it // Claim this crate number and cache it
let cnum = self.cstore.alloc_new_crate_num(); let cnum = self.cstore.alloc_new_crate_num();
@ -207,7 +207,7 @@ impl<'a> CrateLoader<'a> {
let root = if let Some(root) = root { let root = if let Some(root) = root {
root root
} else { } else {
crate_paths = CratePaths::new(crate_root.name, source.clone()); crate_paths = CratePaths::new(crate_root.name(), source.clone());
&crate_paths &crate_paths
}; };
@ -221,7 +221,7 @@ impl<'a> CrateLoader<'a> {
None => (&source, &crate_root), None => (&source, &crate_root),
}; };
let dlsym_dylib = dlsym_source.dylib.as_ref().expect("no dylib for a proc-macro crate"); 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, span)) Some(self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.disambiguator(), span))
} else { } else {
None None
}; };
@ -378,7 +378,7 @@ impl<'a> CrateLoader<'a> {
if locator.triple == self.sess.opts.target_triple { if locator.triple == self.sess.opts.target_triple {
let mut result = LoadResult::Loaded(library); let mut result = LoadResult::Loaded(library);
self.cstore.iter_crate_data(|cnum, data| { self.cstore.iter_crate_data(|cnum, data| {
if data.root.name == root.name && root.hash == data.root.hash { if data.root.name() == root.name() && root.hash() == data.root.hash() {
assert!(locator.hash.is_none()); assert!(locator.hash.is_none());
info!("load success, going to previous cnum: {}", cnum); info!("load success, going to previous cnum: {}", cnum);
result = LoadResult::Previous(cnum); result = LoadResult::Previous(cnum);
@ -494,13 +494,12 @@ impl<'a> CrateLoader<'a> {
sym::needs_panic_runtime); sym::needs_panic_runtime);
self.cstore.iter_crate_data(|cnum, data| { self.cstore.iter_crate_data(|cnum, data| {
needs_panic_runtime = needs_panic_runtime || needs_panic_runtime = needs_panic_runtime || data.needs_panic_runtime();
data.root.needs_panic_runtime; if data.is_panic_runtime() {
if data.root.panic_runtime {
// Inject a dependency from all #![needs_panic_runtime] to this // Inject a dependency from all #![needs_panic_runtime] to this
// #![panic_runtime] crate. // #![panic_runtime] crate.
self.inject_dependency_if(cnum, "a panic runtime", self.inject_dependency_if(cnum, "a panic runtime",
&|data| data.root.needs_panic_runtime); &|data| data.needs_panic_runtime());
runtime_found = runtime_found || data.dep_kind() == DepKind::Explicit; runtime_found = runtime_found || data.dep_kind() == DepKind::Explicit;
} }
}); });
@ -536,11 +535,11 @@ impl<'a> CrateLoader<'a> {
// Sanity check the loaded crate to ensure it is indeed a panic runtime // Sanity check the loaded crate to ensure it is indeed a panic runtime
// and the panic strategy is indeed what we thought it was. // and the panic strategy is indeed what we thought it was.
if !data.root.panic_runtime { if !data.is_panic_runtime() {
self.sess.err(&format!("the crate `{}` is not a panic runtime", self.sess.err(&format!("the crate `{}` is not a panic runtime",
name)); name));
} }
if data.root.panic_strategy != desired_strategy { if data.panic_strategy() != desired_strategy {
self.sess.err(&format!("the crate `{}` does not have the panic \ self.sess.err(&format!("the crate `{}` does not have the panic \
strategy `{}`", strategy `{}`",
name, desired_strategy.desc())); name, desired_strategy.desc()));
@ -548,7 +547,7 @@ impl<'a> CrateLoader<'a> {
self.cstore.injected_panic_runtime = Some(cnum); self.cstore.injected_panic_runtime = Some(cnum);
self.inject_dependency_if(cnum, "a panic runtime", self.inject_dependency_if(cnum, "a panic runtime",
&|data| data.root.needs_panic_runtime); &|data| data.needs_panic_runtime());
} }
fn inject_sanitizer_runtime(&mut self) { fn inject_sanitizer_runtime(&mut self) {
@ -622,7 +621,7 @@ impl<'a> CrateLoader<'a> {
let mut uses_std = false; let mut uses_std = false;
self.cstore.iter_crate_data(|_, data| { self.cstore.iter_crate_data(|_, data| {
if data.root.name == sym::std { if data.root.name() == sym::std {
uses_std = true; uses_std = true;
} }
}); });
@ -640,7 +639,7 @@ impl<'a> CrateLoader<'a> {
let data = self.cstore.get_crate_data(cnum); let data = self.cstore.get_crate_data(cnum);
// Sanity check the loaded crate to ensure it is indeed a sanitizer runtime // Sanity check the loaded crate to ensure it is indeed a sanitizer runtime
if !data.root.sanitizer_runtime { if !data.is_sanitizer_runtime() {
self.sess.err(&format!("the crate `{}` is not a sanitizer runtime", self.sess.err(&format!("the crate `{}` is not a sanitizer runtime",
name)); name));
} }
@ -661,7 +660,7 @@ impl<'a> CrateLoader<'a> {
let data = self.cstore.get_crate_data(cnum); let data = self.cstore.get_crate_data(cnum);
// Sanity check the loaded crate to ensure it is indeed a profiler runtime // Sanity check the loaded crate to ensure it is indeed a profiler runtime
if !data.root.profiler_runtime { if !data.is_profiler_runtime() {
self.sess.err(&format!("the crate `profiler_builtins` is not \ self.sess.err(&format!("the crate `profiler_builtins` is not \
a profiler runtime")); a profiler runtime"));
} }
@ -687,7 +686,7 @@ impl<'a> CrateLoader<'a> {
let mut needs_allocator = attr::contains_name(&krate.attrs, let mut needs_allocator = attr::contains_name(&krate.attrs,
sym::needs_allocator); sym::needs_allocator);
self.cstore.iter_crate_data(|_, data| { self.cstore.iter_crate_data(|_, data| {
needs_allocator = needs_allocator || data.root.needs_allocator; needs_allocator = needs_allocator || data.needs_allocator();
}); });
if !needs_allocator { if !needs_allocator {
self.cstore.allocator_kind = None; self.cstore.allocator_kind = None;
@ -723,7 +722,7 @@ impl<'a> CrateLoader<'a> {
None None
}; };
self.cstore.iter_crate_data(|_, data| { self.cstore.iter_crate_data(|_, data| {
if !data.root.has_global_allocator { if !data.has_global_allocator() {
return return
} }
match global_allocator { match global_allocator {
@ -732,14 +731,14 @@ impl<'a> CrateLoader<'a> {
conflicts with this global \ conflicts with this global \
allocator in: {}", allocator in: {}",
other_crate, other_crate,
data.root.name)); data.root.name()));
} }
Some(None) => { Some(None) => {
self.sess.err(&format!("the `#[global_allocator]` in this \ self.sess.err(&format!("the `#[global_allocator]` in this \
crate conflicts with global \ crate conflicts with global \
allocator in: {}", data.root.name)); allocator in: {}", data.root.name()));
} }
None => global_allocator = Some(Some(data.root.name)), None => global_allocator = Some(Some(data.root.name())),
} }
}); });
if global_allocator.is_some() { if global_allocator.is_some() {
@ -753,7 +752,7 @@ impl<'a> CrateLoader<'a> {
// attribute. // attribute.
let mut has_default = attr::contains_name(&krate.attrs, sym::default_lib_allocator); let mut has_default = attr::contains_name(&krate.attrs, sym::default_lib_allocator);
self.cstore.iter_crate_data(|_, data| { self.cstore.iter_crate_data(|_, data| {
if data.root.has_default_lib_allocator { if data.has_default_lib_allocator() {
has_default = true; has_default = true;
} }
}); });
@ -787,9 +786,9 @@ impl<'a> CrateLoader<'a> {
self.sess.err(&format!("the crate `{}` cannot depend \ self.sess.err(&format!("the crate `{}` cannot depend \
on a crate that needs {}, but \ on a crate that needs {}, but \
it depends on `{}`", it depends on `{}`",
self.cstore.get_crate_data(krate).root.name, self.cstore.get_crate_data(krate).root.name(),
what, what,
data.root.name)); data.root.name()));
} }
} }

View file

@ -597,7 +597,7 @@ impl<'a> CrateLocator<'a> {
"multiple matching crates for `{}`", "multiple matching crates for `{}`",
self.crate_name); self.crate_name);
let candidates = libraries.iter().filter_map(|(_, lib)| { let candidates = libraries.iter().filter_map(|(_, lib)| {
let crate_name = &lib.metadata.get_root().name.as_str(); let crate_name = &lib.metadata.get_root().name().as_str();
match &(&lib.source.dylib, &lib.source.rlib) { match &(&lib.source.dylib, &lib.source.rlib) {
&(&Some((ref pd, _)), &Some((ref pr, _))) => { &(&Some((ref pd, _)), &Some((ref pr, _))) => {
Some(format!("\ncrate `{}`: {}\n{:>padding$}", Some(format!("\ncrate `{}`: {}\n{:>padding$}",
@ -774,35 +774,36 @@ impl<'a> CrateLocator<'a> {
} }
if self.exact_paths.is_empty() { if self.exact_paths.is_empty() {
if self.crate_name != root.name { if self.crate_name != root.name() {
info!("Rejecting via crate name"); info!("Rejecting via crate name");
return None; return None;
} }
} }
if root.triple != self.triple { if root.triple() != &self.triple {
info!("Rejecting via crate triple: expected {} got {}", info!("Rejecting via crate triple: expected {} got {}",
self.triple, self.triple,
root.triple); root.triple());
self.rejected_via_triple.push(CrateMismatch { self.rejected_via_triple.push(CrateMismatch {
path: libpath.to_path_buf(), path: libpath.to_path_buf(),
got: root.triple.to_string(), got: root.triple().to_string(),
}); });
return None; return None;
} }
if let Some(myhash) = self.hash { let hash = root.hash();
if *myhash != root.hash { if let Some(&expected_hash) = self.hash {
info!("Rejecting via hash: expected {} got {}", *myhash, root.hash); if hash != expected_hash {
info!("Rejecting via hash: expected {} got {}", expected_hash, hash);
self.rejected_via_hash.push(CrateMismatch { self.rejected_via_hash.push(CrateMismatch {
path: libpath.to_path_buf(), path: libpath.to_path_buf(),
got: myhash.to_string(), got: hash.to_string(),
}); });
return None; return None;
} }
} }
Some(root.hash) Some(hash)
} }
@ -1021,7 +1022,7 @@ pub fn find_plugin_registrar(
match library.source.dylib { match library.source.dylib {
Some(dylib) => { Some(dylib) => {
Some((dylib.0, library.metadata.get_root().disambiguator)) Some((dylib.0, library.metadata.get_root().disambiguator()))
} }
None => { None => {
span_err!(sess, span, E0457, span_err!(sess, span, E0457,

View file

@ -558,6 +558,22 @@ impl CrateRoot<'_> {
self.proc_macro_data.is_some() self.proc_macro_data.is_some()
} }
crate fn name(&self) -> Symbol {
self.name
}
crate fn disambiguator(&self) -> CrateDisambiguator {
self.disambiguator
}
crate fn hash(&self) -> Svh {
self.hash
}
crate fn triple(&self) -> &TargetTriple {
&self.triple
}
crate fn decode_crate_deps( crate fn decode_crate_deps(
&self, &self,
metadata: &'a MetadataBlob, metadata: &'a MetadataBlob,
@ -1546,6 +1562,38 @@ impl<'a, 'tcx> CrateMetadata {
crate fn update_dep_kind(&self, f: impl FnOnce(DepKind) -> DepKind) { crate fn update_dep_kind(&self, f: impl FnOnce(DepKind) -> DepKind) {
self.dep_kind.with_lock(|dep_kind| *dep_kind = f(*dep_kind)) self.dep_kind.with_lock(|dep_kind| *dep_kind = f(*dep_kind))
} }
crate fn panic_strategy(&self) -> PanicStrategy {
self.root.panic_strategy
}
crate fn needs_panic_runtime(&self) -> bool {
self.root.needs_panic_runtime
}
crate fn is_panic_runtime(&self) -> bool {
self.root.panic_runtime
}
crate fn is_sanitizer_runtime(&self) -> bool {
self.root.sanitizer_runtime
}
crate fn is_profiler_runtime(&self) -> bool {
self.root.profiler_runtime
}
crate fn needs_allocator(&self) -> bool {
self.root.needs_allocator
}
crate fn has_global_allocator(&self) -> bool {
self.root.has_global_allocator
}
crate fn has_default_lib_allocator(&self) -> bool {
self.root.has_default_lib_allocator
}
} }
// Cannot be implemented on 'ProcMacro', as libproc_macro // Cannot be implemented on 'ProcMacro', as libproc_macro

View file

@ -173,16 +173,16 @@ macro_rules! Lazy {
#[derive(RustcEncodable, RustcDecodable)] #[derive(RustcEncodable, RustcDecodable)]
crate struct CrateRoot<'tcx> { crate struct CrateRoot<'tcx> {
pub name: Symbol, name: Symbol,
pub triple: TargetTriple, triple: TargetTriple,
extra_filename: String, extra_filename: String,
pub hash: Svh, hash: Svh,
pub disambiguator: CrateDisambiguator, disambiguator: CrateDisambiguator,
pub panic_strategy: PanicStrategy, panic_strategy: PanicStrategy,
edition: Edition, edition: Edition,
pub has_global_allocator: bool, has_global_allocator: bool,
has_panic_handler: bool, has_panic_handler: bool,
pub has_default_lib_allocator: bool, has_default_lib_allocator: bool,
plugin_registrar_fn: Option<DefIndex>, plugin_registrar_fn: Option<DefIndex>,
proc_macro_decls_static: Option<DefIndex>, proc_macro_decls_static: Option<DefIndex>,
proc_macro_stability: Option<attr::Stability>, proc_macro_stability: Option<attr::Stability>,
@ -207,12 +207,12 @@ crate struct CrateRoot<'tcx> {
proc_macro_data: Option<Lazy<[DefIndex]>>, proc_macro_data: Option<Lazy<[DefIndex]>>,
compiler_builtins: bool, compiler_builtins: bool,
pub needs_allocator: bool, needs_allocator: bool,
pub needs_panic_runtime: bool, needs_panic_runtime: bool,
no_builtins: bool, no_builtins: bool,
pub panic_runtime: bool, panic_runtime: bool,
pub profiler_runtime: bool, profiler_runtime: bool,
pub sanitizer_runtime: bool, sanitizer_runtime: bool,
symbol_mangling_version: SymbolManglingVersion, symbol_mangling_version: SymbolManglingVersion,
} }