From a3ada4e68add8972b1e45eecf383d51b62f6b1c7 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 30 Aug 2021 18:11:09 +0200 Subject: [PATCH] Refactor CrateLocator.is_proc_macro This also fixes a (theoretical) bug where a proc-macro may be loaded as plugin if it exports a symbol with the right name. --- compiler/rustc_metadata/src/creader.rs | 7 +++---- compiler/rustc_metadata/src/locator.rs | 24 ++++++++++-------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index 394cb838935..5968fc258dc 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -459,7 +459,7 @@ impl<'a> CrateLoader<'a> { let mut proc_macro_locator = locator.clone(); // Try to load a proc macro - proc_macro_locator.is_proc_macro = Some(true); + proc_macro_locator.is_proc_macro = true; // Load the proc macro crate for the target let (locator, target_result) = if self.sess.opts.debugging_opts.dual_proc_macros { @@ -482,7 +482,7 @@ impl<'a> CrateLoader<'a> { // Load the proc macro crate for the host locator.reset(); - locator.is_proc_macro = Some(true); + locator.is_proc_macro = true; locator.target = &self.sess.host; locator.triple = TargetTriple::from_triple(config::host_triple()); locator.filesearch = self.sess.host_filesearch(path_kind); @@ -556,7 +556,6 @@ impl<'a> CrateLoader<'a> { false, // is_host path_kind, root, - Some(false), // is_proc_macro ); match self.load(&mut locator)? { @@ -605,7 +604,7 @@ impl<'a> CrateLoader<'a> { // FIXME: why is this condition necessary? It was adding in #33625 but I // don't know why and the original author doesn't remember ... let can_reuse_cratenum = - locator.triple == self.sess.opts.target_triple || locator.is_proc_macro == Some(true); + locator.triple == self.sess.opts.target_triple || locator.is_proc_macro; Ok(Some(if can_reuse_cratenum { let mut result = LoadResult::Loaded(library); self.cstore.iter_crate_data(|cnum, data| { diff --git a/compiler/rustc_metadata/src/locator.rs b/compiler/rustc_metadata/src/locator.rs index d03575dfb53..75efad22b5d 100644 --- a/compiler/rustc_metadata/src/locator.rs +++ b/compiler/rustc_metadata/src/locator.rs @@ -254,7 +254,7 @@ crate struct CrateLocator<'a> { pub triple: TargetTriple, pub filesearch: FileSearch<'a>, root: Option<&'a CratePaths>, - pub is_proc_macro: Option, + pub is_proc_macro: bool, // Mutable in-progress state or output. rejected_via_hash: Vec, @@ -304,7 +304,6 @@ impl<'a> CrateLocator<'a> { is_host: bool, path_kind: PathKind, root: Option<&'a CratePaths>, - is_proc_macro: Option, ) -> CrateLocator<'a> { // The all loop is because `--crate-type=rlib --crate-type=rlib` is // legal and produces both inside this type. @@ -349,7 +348,7 @@ impl<'a> CrateLocator<'a> { sess.target_filesearch(path_kind) }, root, - is_proc_macro, + is_proc_macro: false, rejected_via_hash: Vec::new(), rejected_via_triple: Vec::new(), rejected_via_kind: Vec::new(), @@ -491,7 +490,7 @@ impl<'a> CrateLocator<'a> { } fn needs_crate_flavor(&self, flavor: CrateFlavor) -> bool { - if flavor == CrateFlavor::Dylib && self.is_proc_macro == Some(true) { + if flavor == CrateFlavor::Dylib && self.is_proc_macro { return true; } @@ -623,15 +622,13 @@ impl<'a> CrateLocator<'a> { } let root = metadata.get_root(); - if let Some(expected_is_proc_macro) = self.is_proc_macro { - let is_proc_macro = root.is_proc_macro_crate(); - if is_proc_macro != expected_is_proc_macro { - info!( - "Rejecting via proc macro: expected {} got {}", - expected_is_proc_macro, is_proc_macro - ); - return None; - } + if root.is_proc_macro_crate() != self.is_proc_macro { + info!( + "Rejecting via proc macro: expected {} got {}", + self.is_proc_macro, + root.is_proc_macro_crate(), + ); + return None; } if self.exact_paths.is_empty() && self.crate_name != root.name() { @@ -815,7 +812,6 @@ fn find_plugin_registrar_impl<'a>( true, // is_host PathKind::Crate, None, // root - None, // is_proc_macro ); match locator.maybe_load_library_crate()? {