Skip to content

Commit

Permalink
Refactor CrateLocator.is_proc_macro
Browse files Browse the repository at this point in the history
This also fixes a (theoretical) bug where a proc-macro may be loaded as
plugin if it exports a symbol with the right name.
  • Loading branch information
bjorn3 committed Sep 2, 2021
1 parent 4f35f66 commit a3ada4e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
7 changes: 3 additions & 4 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down Expand Up @@ -556,7 +556,6 @@ impl<'a> CrateLoader<'a> {
false, // is_host
path_kind,
root,
Some(false), // is_proc_macro
);

match self.load(&mut locator)? {
Expand Down Expand Up @@ -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| {
Expand Down
24 changes: 10 additions & 14 deletions compiler/rustc_metadata/src/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ crate struct CrateLocator<'a> {
pub triple: TargetTriple,
pub filesearch: FileSearch<'a>,
root: Option<&'a CratePaths>,
pub is_proc_macro: Option<bool>,
pub is_proc_macro: bool,

// Mutable in-progress state or output.
rejected_via_hash: Vec<CrateMismatch>,
Expand Down Expand Up @@ -304,7 +304,6 @@ impl<'a> CrateLocator<'a> {
is_host: bool,
path_kind: PathKind,
root: Option<&'a CratePaths>,
is_proc_macro: Option<bool>,
) -> CrateLocator<'a> {
// The all loop is because `--crate-type=rlib --crate-type=rlib` is
// legal and produces both inside this type.
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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()? {
Expand Down

0 comments on commit a3ada4e

Please sign in to comment.