From c80ffcbc28555b40516c35d4f1ed71accab520a7 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 29 Sep 2019 18:51:09 +0300 Subject: [PATCH 1/8] metadata: Remove `CrateMetadata::imported_name` It's entirely irrelevant to crate loading --- src/librustc_metadata/creader.rs | 2 -- src/librustc_metadata/cstore.rs | 4 ---- src/librustc_metadata/cstore_impl.rs | 3 +-- 3 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index 682835d81a62..69e9c6eb83b7 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -207,7 +207,6 @@ impl<'a> CrateLoader<'a> { info!("register crate `extern crate {} as {}` (private_dep = {})", crate_root.name, ident, private_dep); - // Claim this crate number and cache it let cnum = self.cstore.alloc_new_crate_num(); @@ -255,7 +254,6 @@ impl<'a> CrateLoader<'a> { let cmeta = cstore::CrateMetadata { name: crate_root.name, - imported_name: ident, extern_crate: Lock::new(None), def_path_table: Lrc::new(def_path_table), trait_impls, diff --git a/src/librustc_metadata/cstore.rs b/src/librustc_metadata/cstore.rs index d3619b2f5de9..ed43aad38be0 100644 --- a/src/librustc_metadata/cstore.rs +++ b/src/librustc_metadata/cstore.rs @@ -49,10 +49,6 @@ pub struct CrateMetadata { /// Original name of the crate. pub name: Symbol, - /// Name of the crate as imported. I.e., if imported with - /// `extern crate foo as bar;` this will be `bar`. - pub imported_name: Symbol, - /// Information about the extern crate that caused this crate to /// be loaded. If this is `None`, then the crate was injected /// (e.g., by the allocator) diff --git a/src/librustc_metadata/cstore_impl.rs b/src/librustc_metadata/cstore_impl.rs index edb6f594fdd0..b5366769fff6 100644 --- a/src/librustc_metadata/cstore_impl.rs +++ b/src/librustc_metadata/cstore_impl.rs @@ -453,8 +453,7 @@ impl cstore::CStore { } let def = data.get_macro(id.index); - let macro_full_name = data.def_path(id.index) - .to_string_friendly(|_| data.imported_name); + let macro_full_name = data.def_path(id.index).to_string_friendly(|_| data.name); let source_name = FileName::Macros(macro_full_name); let source_file = sess.parse_sess.source_map().new_source_file(source_name, def.body); From 45d168a7a086479528581dfdff7911a0a4f211ec Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 29 Sep 2019 19:34:51 +0300 Subject: [PATCH 2/8] metadata: Do not pass crate name after renaming to `register_crate` It's entirely irrelevant to crate loading --- src/librustc_metadata/creader.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index 69e9c6eb83b7..83727d6714d7 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -191,7 +191,6 @@ impl<'a> CrateLoader<'a> { &mut self, host_lib: Option, root: &Option, - ident: Symbol, span: Span, lib: Library, dep_kind: DepKind, @@ -204,8 +203,7 @@ impl<'a> CrateLoader<'a> { .map(|e| e.is_private_dep) .unwrap_or(false); - info!("register crate `extern crate {} as {}` (private_dep = {})", - crate_root.name, ident, private_dep); + info!("register crate `{}` (private_dep = {})", crate_root.name, private_dep); // Claim this crate number and cache it let cnum = self.cstore.alloc_new_crate_num(); @@ -213,7 +211,7 @@ impl<'a> CrateLoader<'a> { // Stash paths for top-most crate locally if necessary. let crate_paths = if root.is_none() { Some(CratePaths { - ident: ident.to_string(), + ident: crate_root.name.to_string(), dylib: lib.dylib.clone().map(|p| p.0), rlib: lib.rlib.clone().map(|p| p.0), rmeta: lib.rmeta.clone().map(|p| p.0), @@ -391,7 +389,7 @@ impl<'a> CrateLoader<'a> { Ok((cnum, data)) } (LoadResult::Loaded(library), host_library) => { - Ok(self.register_crate(host_library, root, ident, span, library, dep_kind, name)) + Ok(self.register_crate(host_library, root, span, library, dep_kind, name)) } _ => panic!() } From cea24620d37a8cf3b386b010ad8e4a3c39896567 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 29 Sep 2019 20:16:23 +0300 Subject: [PATCH 3/8] metadata: Remove `locator::Context::ident` It's a crate name after renaming, so it's entirely irrelevant to crate loading --- src/librustc_metadata/creader.rs | 28 ++++++++++-------------- src/librustc_metadata/locator.rs | 21 +++++++++--------- src/test/ui/use/use-meta-mismatch.rs | 2 +- src/test/ui/use/use-meta-mismatch.stderr | 2 +- 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index 83727d6714d7..64af9557ea49 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -337,7 +337,6 @@ impl<'a> CrateLoader<'a> { fn resolve_crate<'b>( &'b mut self, root: &'b Option, - ident: Symbol, name: Symbol, hash: Option<&'b Svh>, extra_filename: Option<&'b str>, @@ -345,7 +344,7 @@ impl<'a> CrateLoader<'a> { path_kind: PathKind, mut dep_kind: DepKind, ) -> Result<(CrateNum, Lrc), LoadError<'b>> { - info!("resolving crate `extern crate {} as {}`", name, ident); + info!("resolving crate `{}`", name); let result = if let Some(cnum) = self.existing_match(name, hash, path_kind) { (LoadResult::Previous(cnum), None) } else { @@ -353,7 +352,6 @@ impl<'a> CrateLoader<'a> { let mut locate_ctxt = locator::Context { sess: self.sess, span, - ident, crate_name: name, hash, extra_filename, @@ -493,16 +491,15 @@ impl<'a> CrateLoader<'a> { _ => dep.kind, }; let (local_cnum, ..) = self.resolve_crate( - root, dep.name, dep.name, Some(&dep.hash), Some(&dep.extra_filename), span, + root, dep.name, Some(&dep.hash), Some(&dep.extra_filename), span, PathKind::Dependency, dep_kind, ).unwrap_or_else(|err| err.report()); local_cnum })).collect() } - fn read_extension_crate(&mut self, span: Span, orig_name: Symbol, rename: Symbol) - -> ExtensionCrate { - info!("read extension crate `extern crate {} as {}`", orig_name, rename); + fn read_extension_crate(&mut self, name: Symbol, span: Span) -> ExtensionCrate { + info!("read extension crate `{}`", name); let target_triple = self.sess.opts.target_triple.clone(); let host_triple = TargetTriple::from_triple(config::host_triple()); let is_cross = target_triple != host_triple; @@ -510,8 +507,7 @@ impl<'a> CrateLoader<'a> { let mut locate_ctxt = locator::Context { sess: self.sess, span, - ident: orig_name, - crate_name: rename, + crate_name: name, hash: None, extra_filename: None, filesearch: self.sess.host_filesearch(PathKind::Crate), @@ -606,7 +602,7 @@ impl<'a> CrateLoader<'a> { span: Span, name: Symbol) -> Option<(PathBuf, CrateDisambiguator)> { - let ekrate = self.read_extension_crate(span, name, name); + let ekrate = self.read_extension_crate(name, span); if ekrate.target_only { // Need to abort before syntax expansion. @@ -699,7 +695,7 @@ impl<'a> CrateLoader<'a> { let dep_kind = DepKind::Implicit; let (cnum, data) = - self.resolve_crate(&None, name, name, None, None, DUMMY_SP, PathKind::Crate, dep_kind) + self.resolve_crate(&None, name, None, None, DUMMY_SP, PathKind::Crate, dep_kind) .unwrap_or_else(|err| err.report()); // Sanity check the loaded crate to ensure it is indeed a panic runtime @@ -807,7 +803,7 @@ impl<'a> CrateLoader<'a> { let symbol = Symbol::intern(name); let dep_kind = DepKind::Explicit; let (_, data) = - self.resolve_crate(&None, symbol, symbol, None, None, DUMMY_SP, + self.resolve_crate(&None, symbol, None, None, DUMMY_SP, PathKind::Crate, dep_kind) .unwrap_or_else(|err| err.report()); @@ -831,7 +827,7 @@ impl<'a> CrateLoader<'a> { let symbol = Symbol::intern("profiler_builtins"); let dep_kind = DepKind::Implicit; let (_, data) = - self.resolve_crate(&None, symbol, symbol, None, None, DUMMY_SP, + self.resolve_crate(&None, symbol, None, None, DUMMY_SP, PathKind::Crate, dep_kind) .unwrap_or_else(|err| err.report()); @@ -1015,7 +1011,7 @@ impl<'a> CrateLoader<'a> { }; let (cnum, ..) = self.resolve_crate( - &None, item.ident.name, orig_name, None, None, + &None, orig_name, None, None, item.span, PathKind::Crate, dep_kind, ).unwrap_or_else(|err| err.report()); @@ -1044,7 +1040,7 @@ impl<'a> CrateLoader<'a> { span: Span, ) -> CrateNum { let cnum = self.resolve_crate( - &None, name, name, None, None, span, PathKind::Crate, DepKind::Explicit + &None, name, None, None, span, PathKind::Crate, DepKind::Explicit ).unwrap_or_else(|err| err.report()).0; self.update_extern_crate( @@ -1068,7 +1064,7 @@ impl<'a> CrateLoader<'a> { span: Span, ) -> Option { let cnum = self.resolve_crate( - &None, name, name, None, None, span, PathKind::Crate, DepKind::Explicit + &None, name, None, None, span, PathKind::Crate, DepKind::Explicit ).ok()?.0; self.update_extern_crate( diff --git a/src/librustc_metadata/locator.rs b/src/librustc_metadata/locator.rs index ceba7cf0fe03..f8b277740334 100644 --- a/src/librustc_metadata/locator.rs +++ b/src/librustc_metadata/locator.rs @@ -254,7 +254,6 @@ pub struct CrateMismatch { pub struct Context<'a> { pub sess: &'a Session, pub span: Span, - pub ident: Symbol, pub crate_name: Symbol, pub hash: Option<&'a Svh>, pub extra_filename: Option<&'a str>, @@ -332,12 +331,12 @@ impl<'a> Context<'a> { self.span, E0460, "found possibly newer version of crate `{}`{}", - self.ident, + self.crate_name, add); err.note("perhaps that crate needs to be recompiled?"); let mismatches = self.rejected_via_hash.iter(); for &CrateMismatch { ref path, .. } in mismatches { - msg.push_str(&format!("\ncrate `{}`: {}", self.ident, path.display())); + msg.push_str(&format!("\ncrate `{}`: {}", self.crate_name, path.display())); } match self.root { &None => {} @@ -355,13 +354,13 @@ impl<'a> Context<'a> { E0461, "couldn't find crate `{}` \ with expected target triple {}{}", - self.ident, + self.crate_name, self.triple, add); let mismatches = self.rejected_via_triple.iter(); for &CrateMismatch { ref path, ref got } in mismatches { msg.push_str(&format!("\ncrate `{}`, target triple {}: {}", - self.ident, + self.crate_name, got, path.display())); } @@ -372,12 +371,12 @@ impl<'a> Context<'a> { self.span, E0462, "found staticlib `{}` instead of rlib or dylib{}", - self.ident, + self.crate_name, add); err.help("please recompile that crate using --crate-type lib"); let mismatches = self.rejected_via_kind.iter(); for &CrateMismatch { ref path, .. } in mismatches { - msg.push_str(&format!("\ncrate `{}`: {}", self.ident, path.display())); + msg.push_str(&format!("\ncrate `{}`: {}", self.crate_name, path.display())); } err.note(&msg); err @@ -387,14 +386,14 @@ impl<'a> Context<'a> { E0514, "found crate `{}` compiled by an incompatible version \ of rustc{}", - self.ident, + self.crate_name, add); err.help(&format!("please recompile that crate using this compiler ({})", rustc_version())); let mismatches = self.rejected_via_version.iter(); for &CrateMismatch { ref path, ref got } in mismatches { msg.push_str(&format!("\ncrate `{}` compiled by {}: {}", - self.ident, + self.crate_name, got, path.display())); } @@ -405,10 +404,10 @@ impl<'a> Context<'a> { self.span, E0463, "can't find crate for `{}`{}", - self.ident, + self.crate_name, add); - if (self.ident == sym::std || self.ident == sym::core) + if (self.crate_name == sym::std || self.crate_name == sym::core) && self.triple != TargetTriple::from_triple(config::host_triple()) { err.note(&format!("the `{}` target may not be installed", self.triple)); } diff --git a/src/test/ui/use/use-meta-mismatch.rs b/src/test/ui/use/use-meta-mismatch.rs index 459216a17e4f..975209efb0c1 100644 --- a/src/test/ui/use/use-meta-mismatch.rs +++ b/src/test/ui/use/use-meta-mismatch.rs @@ -1,4 +1,4 @@ -// error-pattern:can't find crate for `extra` +// error-pattern:can't find crate for `fake_crate` extern crate fake_crate as extra; diff --git a/src/test/ui/use/use-meta-mismatch.stderr b/src/test/ui/use/use-meta-mismatch.stderr index 877ac464de78..62b71fe8e12f 100644 --- a/src/test/ui/use/use-meta-mismatch.stderr +++ b/src/test/ui/use/use-meta-mismatch.stderr @@ -1,4 +1,4 @@ -error[E0463]: can't find crate for `extra` +error[E0463]: can't find crate for `fake_crate` --> $DIR/use-meta-mismatch.rs:3:1 | LL | extern crate fake_crate as extra; From 2293bddb0075caf555b0d8011232237fef41ccd2 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 29 Sep 2019 21:14:29 +0300 Subject: [PATCH 4/8] metadata: Simplify interface of `resolve_crate` --- src/librustc/middle/cstore.rs | 4 +- src/librustc_metadata/creader.rs | 92 ++++++++++++++------------------ src/librustc_metadata/locator.rs | 10 ++-- 3 files changed, 46 insertions(+), 60 deletions(-) diff --git a/src/librustc/middle/cstore.rs b/src/librustc/middle/cstore.rs index ddf6262b7382..510787998ad0 100644 --- a/src/librustc/middle/cstore.rs +++ b/src/librustc/middle/cstore.rs @@ -148,9 +148,7 @@ pub enum ExternCrateSource { /// such ids DefId, ), - // Crate is loaded by `use`. - Use, - /// Crate is implicitly loaded by an absolute path. + /// Crate is implicitly loaded by a path resolving through extern prelude. Path, } diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index 64af9557ea49..ea1b746dc07f 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -2,7 +2,7 @@ use crate::cstore::{self, CStore, CrateSource, MetadataBlob}; use crate::locator::{self, CratePaths}; -use crate::schema::{CrateRoot}; +use crate::schema::{CrateRoot, CrateDep}; use rustc_data_structures::sync::{Lrc, RwLock, Lock}; use rustc::hir::def_id::CrateNum; @@ -190,7 +190,7 @@ impl<'a> CrateLoader<'a> { fn register_crate( &mut self, host_lib: Option, - root: &Option, + root: Option<&CratePaths>, span: Span, lib: Library, dep_kind: DepKind, @@ -208,19 +208,20 @@ impl<'a> CrateLoader<'a> { // Claim this crate number and cache it let cnum = self.cstore.alloc_new_crate_num(); + // Maintain a reference to the top most crate. // Stash paths for top-most crate locally if necessary. - let crate_paths = if root.is_none() { - Some(CratePaths { + let crate_paths; + let root = if let Some(root) = root { + root + } else { + crate_paths = CratePaths { ident: crate_root.name.to_string(), dylib: lib.dylib.clone().map(|p| p.0), rlib: lib.rlib.clone().map(|p| p.0), rmeta: lib.rmeta.clone().map(|p| p.0), - }) - } else { - None + }; + &crate_paths }; - // Maintain a reference to the top most crate. - let root = if root.is_some() { root } else { &crate_paths }; let Library { dylib, rlib, rmeta, metadata } = lib; let cnum_map = self.resolve_crate_deps(root, &crate_root, &metadata, cnum, span, dep_kind); @@ -336,15 +337,27 @@ impl<'a> CrateLoader<'a> { fn resolve_crate<'b>( &'b mut self, - root: &'b Option, name: Symbol, - hash: Option<&'b Svh>, - extra_filename: Option<&'b str>, span: Span, - path_kind: PathKind, + dep_kind: DepKind, + dep: Option<(&'b CratePaths, &'b CrateDep)>, + ) -> (CrateNum, Lrc) { + self.maybe_resolve_crate(name, span, dep_kind, dep).unwrap_or_else(|err| err.report()) + } + + fn maybe_resolve_crate<'b>( + &'b mut self, + name: Symbol, + span: Span, mut dep_kind: DepKind, + dep: Option<(&'b CratePaths, &'b CrateDep)>, ) -> Result<(CrateNum, Lrc), LoadError<'b>> { info!("resolving crate `{}`", name); + let (root, hash, extra_filename, path_kind) = match dep { + Some((root, dep)) => + (Some(root), Some(&dep.hash), Some(&dep.extra_filename[..]), PathKind::Dependency), + None => (None, None, None, PathKind::Crate), + }; let result = if let Some(cnum) = self.existing_match(name, hash, path_kind) { (LoadResult::Previous(cnum), None) } else { @@ -463,7 +476,7 @@ impl<'a> CrateLoader<'a> { // Go through the crate metadata and load any crates that it references fn resolve_crate_deps(&mut self, - root: &Option, + root: &CratePaths, crate_root: &CrateRoot<'_>, metadata: &MetadataBlob, krate: CrateNum, @@ -478,9 +491,7 @@ impl<'a> CrateLoader<'a> { // The map from crate numbers in the crate we're resolving to local crate numbers. // We map 0 and all other holes in the map to our parent crate. The "additional" // self-dependencies should be harmless. - std::iter::once(krate).chain(crate_root.crate_deps - .decode(metadata) - .map(|dep| { + std::iter::once(krate).chain(crate_root.crate_deps.decode(metadata).map(|dep| { info!("resolving dep crate {} hash: `{}` extra filename: `{}`", dep.name, dep.hash, dep.extra_filename); if dep.kind == DepKind::UnexportedMacrosOnly { @@ -490,11 +501,7 @@ impl<'a> CrateLoader<'a> { DepKind::MacrosOnly => DepKind::MacrosOnly, _ => dep.kind, }; - let (local_cnum, ..) = self.resolve_crate( - root, dep.name, Some(&dep.hash), Some(&dep.extra_filename), span, - PathKind::Dependency, dep_kind, - ).unwrap_or_else(|err| err.report()); - local_cnum + self.resolve_crate(dep.name, span, dep_kind, Some((root, &dep))).0 })).collect() } @@ -513,7 +520,7 @@ impl<'a> CrateLoader<'a> { filesearch: self.sess.host_filesearch(PathKind::Crate), target: &self.sess.host, triple: host_triple, - root: &None, + root: None, rejected_via_hash: vec![], rejected_via_triple: vec![], rejected_via_kind: vec![], @@ -693,10 +700,7 @@ impl<'a> CrateLoader<'a> { }; info!("panic runtime not found -- loading {}", name); - let dep_kind = DepKind::Implicit; - let (cnum, data) = - self.resolve_crate(&None, name, None, None, DUMMY_SP, PathKind::Crate, dep_kind) - .unwrap_or_else(|err| err.report()); + let (cnum, data) = self.resolve_crate(name, DUMMY_SP, DepKind::Implicit, None); // Sanity check the loaded crate to ensure it is indeed a panic runtime // and the panic strategy is indeed what we thought it was. @@ -792,20 +796,15 @@ impl<'a> CrateLoader<'a> { }); if uses_std { - let name = match *sanitizer { + let name = Symbol::intern(match sanitizer { Sanitizer::Address => "rustc_asan", Sanitizer::Leak => "rustc_lsan", Sanitizer::Memory => "rustc_msan", Sanitizer::Thread => "rustc_tsan", - }; + }); info!("loading sanitizer: {}", name); - let symbol = Symbol::intern(name); - let dep_kind = DepKind::Explicit; - let (_, data) = - self.resolve_crate(&None, symbol, None, None, DUMMY_SP, - PathKind::Crate, dep_kind) - .unwrap_or_else(|err| err.report()); + let data = self.resolve_crate(name, DUMMY_SP, DepKind::Explicit, None).1; // Sanity check the loaded crate to ensure it is indeed a sanitizer runtime if !data.root.sanitizer_runtime { @@ -824,12 +823,8 @@ impl<'a> CrateLoader<'a> { { info!("loading profiler"); - let symbol = Symbol::intern("profiler_builtins"); - let dep_kind = DepKind::Implicit; - let (_, data) = - self.resolve_crate(&None, symbol, None, None, DUMMY_SP, - PathKind::Crate, dep_kind) - .unwrap_or_else(|err| err.report()); + let name = Symbol::intern("profiler_builtins"); + let data = self.resolve_crate(name, DUMMY_SP, DepKind::Implicit, None).1; // Sanity check the loaded crate to ensure it is indeed a profiler runtime if !data.root.profiler_runtime { @@ -996,7 +991,7 @@ impl<'a> CrateLoader<'a> { ast::ItemKind::ExternCrate(orig_name) => { debug!("resolving extern crate stmt. ident: {} orig_name: {:?}", item.ident, orig_name); - let orig_name = match orig_name { + let name = match orig_name { Some(orig_name) => { crate::validate_crate_name(Some(self.sess), &orig_name.as_str(), Some(item.span)); @@ -1010,10 +1005,7 @@ impl<'a> CrateLoader<'a> { DepKind::Explicit }; - let (cnum, ..) = self.resolve_crate( - &None, orig_name, None, None, - item.span, PathKind::Crate, dep_kind, - ).unwrap_or_else(|err| err.report()); + let cnum = self.resolve_crate(name, item.span, dep_kind, None).0; let def_id = definitions.opt_local_def_id(item.id).unwrap(); let path_len = definitions.def_path(def_id.index).data.len(); @@ -1039,9 +1031,7 @@ impl<'a> CrateLoader<'a> { name: Symbol, span: Span, ) -> CrateNum { - let cnum = self.resolve_crate( - &None, name, None, None, span, PathKind::Crate, DepKind::Explicit - ).unwrap_or_else(|err| err.report()).0; + let cnum = self.resolve_crate(name, span, DepKind::Explicit, None).0; self.update_extern_crate( cnum, @@ -1063,9 +1053,7 @@ impl<'a> CrateLoader<'a> { name: Symbol, span: Span, ) -> Option { - let cnum = self.resolve_crate( - &None, name, None, None, span, PathKind::Crate, DepKind::Explicit - ).ok()?.0; + let cnum = self.maybe_resolve_crate(name, span, DepKind::Explicit, None).ok()?.0; self.update_extern_crate( cnum, diff --git a/src/librustc_metadata/locator.rs b/src/librustc_metadata/locator.rs index f8b277740334..8df236c41cfb 100644 --- a/src/librustc_metadata/locator.rs +++ b/src/librustc_metadata/locator.rs @@ -261,7 +261,7 @@ pub struct Context<'a> { pub target: &'a Target, pub triple: TargetTriple, pub filesearch: FileSearch<'a>, - pub root: &'a Option, + pub root: Option<&'a CratePaths>, pub rejected_via_hash: Vec, pub rejected_via_triple: Vec, pub rejected_via_kind: Vec, @@ -322,8 +322,8 @@ impl<'a> Context<'a> { pub fn report_errs(self) -> ! { let add = match self.root { - &None => String::new(), - &Some(ref r) => format!(" which `{}` depends on", r.ident), + None => String::new(), + Some(r) => format!(" which `{}` depends on", r.ident), }; let mut msg = "the following crate versions were found:".to_string(); let mut err = if !self.rejected_via_hash.is_empty() { @@ -339,8 +339,8 @@ impl<'a> Context<'a> { msg.push_str(&format!("\ncrate `{}`: {}", self.crate_name, path.display())); } match self.root { - &None => {} - &Some(ref r) => { + None => {} + Some(r) => { for path in r.paths().iter() { msg.push_str(&format!("\ncrate `{}`: {}", r.ident, path.display())); } From 61a75afa445ba8ba234899181940cef1fbc570a2 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 2 Oct 2019 02:47:36 +0300 Subject: [PATCH 5/8] metadata: Remove `CrateMetadata::host_lib` It was only used for retreiving edition, which was a bug. In case of dual proc macros the edition should be taken from the target crate version, like any other metadata. --- src/librustc_metadata/creader.rs | 1 - src/librustc_metadata/cstore.rs | 2 -- src/librustc_metadata/decoder.rs | 7 +------ 3 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index ea1b746dc07f..f2980941082f 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -271,7 +271,6 @@ impl<'a> CrateLoader<'a> { }, private_dep, span, - host_lib, raw_proc_macros }; diff --git a/src/librustc_metadata/cstore.rs b/src/librustc_metadata/cstore.rs index ed43aad38be0..d8bbe23b8ecb 100644 --- a/src/librustc_metadata/cstore.rs +++ b/src/librustc_metadata/cstore.rs @@ -28,7 +28,6 @@ pub use crate::cstore_impl::{provide, provide_extern}; pub type CrateNumMap = IndexVec; pub use rustc_data_structures::sync::MetadataRef; -use crate::creader::Library; use syntax_pos::Span; use proc_macro::bridge::client::ProcMacro; @@ -85,7 +84,6 @@ pub struct CrateMetadata { /// for purposes of the 'exported_private_dependencies' lint pub private_dep: bool, - pub host_lib: Option, pub span: Span, pub raw_proc_macros: Option<&'static [ProcMacro]>, diff --git a/src/librustc_metadata/decoder.rs b/src/librustc_metadata/decoder.rs index 9fcc31015459..6681cf3052f2 100644 --- a/src/librustc_metadata/decoder.rs +++ b/src/librustc_metadata/decoder.rs @@ -543,18 +543,13 @@ impl<'a, 'tcx> CrateMetadata { name, SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client })), Vec::new() ) }; - let edition = if sess.opts.debugging_opts.dual_proc_macros { - self.host_lib.as_ref().unwrap().metadata.get_root().edition - } else { - self.root.edition - }; SyntaxExtension::new( &sess.parse_sess, kind, self.get_span(id, sess), helper_attrs, - edition, + self.root.edition, Symbol::intern(name), &self.get_attributes(&self.entry(id), sess), ) From 498d908da583c680b42e43bce80ab347149e8842 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 2 Oct 2019 03:15:38 +0300 Subject: [PATCH 6/8] metadata: Remove `CrateMetadata::name` It duplicates `CrateRoot::name` --- src/librustc_metadata/creader.rs | 5 ++--- src/librustc_metadata/cstore.rs | 4 ---- src/librustc_metadata/cstore_impl.rs | 6 +++--- src/librustc_metadata/decoder.rs | 2 +- 4 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index f2980941082f..dcb0fb73aa72 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -112,7 +112,7 @@ impl<'a> CrateLoader<'a> { -> Option { let mut ret = None; self.cstore.iter_crate_data(|cnum, data| { - if data.name != name { return } + if data.root.name != name { return } match hash { Some(hash) if *hash == data.root.hash => { ret = Some(cnum); return } @@ -252,7 +252,6 @@ impl<'a> CrateLoader<'a> { }); let cmeta = cstore::CrateMetadata { - name: crate_root.name, extern_crate: Lock::new(None), def_path_table: Lrc::new(def_path_table), trait_impls, @@ -789,7 +788,7 @@ impl<'a> CrateLoader<'a> { let mut uses_std = false; self.cstore.iter_crate_data(|_, data| { - if data.name == sym::std { + if data.root.name == sym::std { uses_std = true; } }); diff --git a/src/librustc_metadata/cstore.rs b/src/librustc_metadata/cstore.rs index d8bbe23b8ecb..833c846573f6 100644 --- a/src/librustc_metadata/cstore.rs +++ b/src/librustc_metadata/cstore.rs @@ -12,7 +12,6 @@ use rustc::util::nodemap::{FxHashMap, NodeMap}; use rustc_data_structures::sync::{Lrc, RwLock, Lock}; use syntax::ast; use syntax::ext::base::SyntaxExtension; -use syntax::symbol::Symbol; use syntax_pos; pub use rustc::middle::cstore::{NativeLibrary, NativeLibraryKind, LinkagePreference}; @@ -45,9 +44,6 @@ pub struct ImportedSourceFile { } pub struct CrateMetadata { - /// Original name of the crate. - pub name: Symbol, - /// Information about the extern crate that caused this crate to /// be loaded. If this is `None`, then the crate was injected /// (e.g., by the allocator) diff --git a/src/librustc_metadata/cstore_impl.rs b/src/librustc_metadata/cstore_impl.rs index b5366769fff6..cd6b370ca318 100644 --- a/src/librustc_metadata/cstore_impl.rs +++ b/src/librustc_metadata/cstore_impl.rs @@ -220,7 +220,7 @@ provide! { <'tcx> tcx, def_id, other, cdata, let r = *cdata.dep_kind.lock(); r } - crate_name => { cdata.name } + crate_name => { cdata.root.name } item_children => { let mut result = SmallVec::<[_; 8]>::new(); cdata.each_child_of_item(def_id.index, |child| result.push(child), tcx.sess); @@ -453,7 +453,7 @@ impl cstore::CStore { } let def = data.get_macro(id.index); - let macro_full_name = data.def_path(id.index).to_string_friendly(|_| data.name); + let macro_full_name = data.def_path(id.index).to_string_friendly(|_| data.root.name); let source_name = FileName::Macros(macro_full_name); let source_file = sess.parse_sess.source_map().new_source_file(source_name, def.body); @@ -503,7 +503,7 @@ impl CrateStore for cstore::CStore { fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol { - self.get_crate_data(cnum).name + self.get_crate_data(cnum).root.name } fn crate_is_private_dep_untracked(&self, cnum: CrateNum) -> bool { diff --git a/src/librustc_metadata/decoder.rs b/src/librustc_metadata/decoder.rs index 6681cf3052f2..528d93722464 100644 --- a/src/librustc_metadata/decoder.rs +++ b/src/librustc_metadata/decoder.rs @@ -473,7 +473,7 @@ impl<'a, 'tcx> CrateMetadata { None => { bug!("entry: id not found: {:?} in crate {:?} with number {}", item_id, - self.name, + self.root.name, self.cnum) } Some(d) => d.decode(self), From 2d827b0779e396e080c5fc31fe279ad879951160 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 2 Oct 2019 22:31:11 +0300 Subject: [PATCH 7/8] metadata: Remove unused `Option` from `fn dlsym_proc_macros` --- src/librustc_metadata/creader.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index dcb0fb73aa72..042252bc13e6 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -20,7 +20,7 @@ use rustc::hir::map::Definitions; use rustc::hir::def_id::LOCAL_CRATE; use std::ops::Deref; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::{cmp, fs}; use syntax::ast; @@ -229,13 +229,14 @@ impl<'a> CrateLoader<'a> { let dependencies: Vec = cnum_map.iter().cloned().collect(); let raw_proc_macros = crate_root.proc_macro_data.map(|_| { - if self.sess.opts.debugging_opts.dual_proc_macros { - let host_lib = host_lib.as_ref().unwrap(); - self.dlsym_proc_macros(host_lib.dylib.as_ref().map(|p| p.0.clone()), - &host_lib.metadata.get_root(), span) - } else { - self.dlsym_proc_macros(dylib.clone().map(|p| p.0), &crate_root, span) - } + let temp_root; + let (dlsym_dylib, dlsym_root) = match &host_lib { + Some(host_lib) => + (&host_lib.dylib, { temp_root = host_lib.metadata.get_root(); &temp_root }), + None => (&dylib, &crate_root), + }; + let dlsym_dylib = dlsym_dylib.as_ref().expect("no dylib for a proc-macro crate"); + self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.disambiguator, span) }); let interpret_alloc_index: Vec = crate_root.interpret_alloc_index @@ -567,17 +568,13 @@ impl<'a> CrateLoader<'a> { } fn dlsym_proc_macros(&self, - dylib: Option, - root: &CrateRoot<'_>, + path: &Path, + disambiguator: CrateDisambiguator, span: Span ) -> &'static [ProcMacro] { use std::env; use crate::dynamic_lib::DynamicLibrary; - let path = match dylib { - Some(dylib) => dylib, - None => span_bug!(span, "proc-macro crate not dylib"), - }; // Make sure the path contains a / or the linker will search for it. let path = env::current_dir().unwrap().join(path); let lib = match DynamicLibrary::open(Some(&path)) { @@ -585,7 +582,7 @@ impl<'a> CrateLoader<'a> { Err(err) => self.sess.span_fatal(span, &err), }; - let sym = self.sess.generate_proc_macro_decls_symbol(root.disambiguator); + let sym = self.sess.generate_proc_macro_decls_symbol(disambiguator); let decls = unsafe { let sym = match lib.symbol(&sym) { Ok(f) => f, From 94525e5a0ab540583c7ebcf875875dcde61eb29f Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Thu, 3 Oct 2019 01:25:31 +0300 Subject: [PATCH 8/8] Dual proc macros: Use crate disambiguator from the target crate Not sure this is correct, but let's try. --- src/librustc_metadata/creader.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index 042252bc13e6..366e76f1692c 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -229,14 +229,12 @@ impl<'a> CrateLoader<'a> { let dependencies: Vec = cnum_map.iter().cloned().collect(); let raw_proc_macros = crate_root.proc_macro_data.map(|_| { - let temp_root; - let (dlsym_dylib, dlsym_root) = match &host_lib { - Some(host_lib) => - (&host_lib.dylib, { temp_root = host_lib.metadata.get_root(); &temp_root }), - None => (&dylib, &crate_root), + let dlsym_dylib = match &host_lib { + Some(host_lib) => &host_lib.dylib, + None => &dylib, }; let dlsym_dylib = dlsym_dylib.as_ref().expect("no dylib for a proc-macro crate"); - self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.disambiguator, span) + self.dlsym_proc_macros(&dlsym_dylib.0, crate_root.disambiguator, span) }); let interpret_alloc_index: Vec = crate_root.interpret_alloc_index