1
Fork 0

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.
This commit is contained in:
bjorn3 2021-08-30 18:11:09 +02:00
parent 4f35f66796
commit a3ada4e68a
2 changed files with 13 additions and 18 deletions

View file

@ -459,7 +459,7 @@ impl<'a> CrateLoader<'a> {
let mut proc_macro_locator = locator.clone(); let mut proc_macro_locator = locator.clone();
// Try to load a proc macro // 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 // Load the proc macro crate for the target
let (locator, target_result) = if self.sess.opts.debugging_opts.dual_proc_macros { 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 // Load the proc macro crate for the host
locator.reset(); locator.reset();
locator.is_proc_macro = Some(true); locator.is_proc_macro = true;
locator.target = &self.sess.host; locator.target = &self.sess.host;
locator.triple = TargetTriple::from_triple(config::host_triple()); locator.triple = TargetTriple::from_triple(config::host_triple());
locator.filesearch = self.sess.host_filesearch(path_kind); locator.filesearch = self.sess.host_filesearch(path_kind);
@ -556,7 +556,6 @@ impl<'a> CrateLoader<'a> {
false, // is_host false, // is_host
path_kind, path_kind,
root, root,
Some(false), // is_proc_macro
); );
match self.load(&mut locator)? { 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 // FIXME: why is this condition necessary? It was adding in #33625 but I
// don't know why and the original author doesn't remember ... // don't know why and the original author doesn't remember ...
let can_reuse_cratenum = 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 { Ok(Some(if can_reuse_cratenum {
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| {

View file

@ -254,7 +254,7 @@ crate struct CrateLocator<'a> {
pub triple: TargetTriple, pub triple: TargetTriple,
pub filesearch: FileSearch<'a>, pub filesearch: FileSearch<'a>,
root: Option<&'a CratePaths>, root: Option<&'a CratePaths>,
pub is_proc_macro: Option<bool>, pub is_proc_macro: bool,
// Mutable in-progress state or output. // Mutable in-progress state or output.
rejected_via_hash: Vec<CrateMismatch>, rejected_via_hash: Vec<CrateMismatch>,
@ -304,7 +304,6 @@ impl<'a> CrateLocator<'a> {
is_host: bool, is_host: bool,
path_kind: PathKind, path_kind: PathKind,
root: Option<&'a CratePaths>, root: Option<&'a CratePaths>,
is_proc_macro: Option<bool>,
) -> CrateLocator<'a> { ) -> CrateLocator<'a> {
// The all loop is because `--crate-type=rlib --crate-type=rlib` is // The all loop is because `--crate-type=rlib --crate-type=rlib` is
// legal and produces both inside this type. // legal and produces both inside this type.
@ -349,7 +348,7 @@ impl<'a> CrateLocator<'a> {
sess.target_filesearch(path_kind) sess.target_filesearch(path_kind)
}, },
root, root,
is_proc_macro, is_proc_macro: false,
rejected_via_hash: Vec::new(), rejected_via_hash: Vec::new(),
rejected_via_triple: Vec::new(), rejected_via_triple: Vec::new(),
rejected_via_kind: Vec::new(), rejected_via_kind: Vec::new(),
@ -491,7 +490,7 @@ impl<'a> CrateLocator<'a> {
} }
fn needs_crate_flavor(&self, flavor: CrateFlavor) -> bool { 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; return true;
} }
@ -623,16 +622,14 @@ impl<'a> CrateLocator<'a> {
} }
let root = metadata.get_root(); let root = metadata.get_root();
if let Some(expected_is_proc_macro) = self.is_proc_macro { if root.is_proc_macro_crate() != self.is_proc_macro {
let is_proc_macro = root.is_proc_macro_crate();
if is_proc_macro != expected_is_proc_macro {
info!( info!(
"Rejecting via proc macro: expected {} got {}", "Rejecting via proc macro: expected {} got {}",
expected_is_proc_macro, is_proc_macro self.is_proc_macro,
root.is_proc_macro_crate(),
); );
return None; return None;
} }
}
if self.exact_paths.is_empty() && self.crate_name != root.name() { if self.exact_paths.is_empty() && self.crate_name != root.name() {
info!("Rejecting via crate name"); info!("Rejecting via crate name");
@ -815,7 +812,6 @@ fn find_plugin_registrar_impl<'a>(
true, // is_host true, // is_host
PathKind::Crate, PathKind::Crate,
None, // root None, // root
None, // is_proc_macro
); );
match locator.maybe_load_library_crate()? { match locator.maybe_load_library_crate()? {