Skip to content

Commit a3ada4e

Browse files
committed
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.
1 parent 4f35f66 commit a3ada4e

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

compiler/rustc_metadata/src/creader.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ impl<'a> CrateLoader<'a> {
459459
let mut proc_macro_locator = locator.clone();
460460

461461
// Try to load a proc macro
462-
proc_macro_locator.is_proc_macro = Some(true);
462+
proc_macro_locator.is_proc_macro = true;
463463

464464
// Load the proc macro crate for the target
465465
let (locator, target_result) = if self.sess.opts.debugging_opts.dual_proc_macros {
@@ -482,7 +482,7 @@ impl<'a> CrateLoader<'a> {
482482
// Load the proc macro crate for the host
483483

484484
locator.reset();
485-
locator.is_proc_macro = Some(true);
485+
locator.is_proc_macro = true;
486486
locator.target = &self.sess.host;
487487
locator.triple = TargetTriple::from_triple(config::host_triple());
488488
locator.filesearch = self.sess.host_filesearch(path_kind);
@@ -556,7 +556,6 @@ impl<'a> CrateLoader<'a> {
556556
false, // is_host
557557
path_kind,
558558
root,
559-
Some(false), // is_proc_macro
560559
);
561560

562561
match self.load(&mut locator)? {
@@ -605,7 +604,7 @@ impl<'a> CrateLoader<'a> {
605604
// FIXME: why is this condition necessary? It was adding in #33625 but I
606605
// don't know why and the original author doesn't remember ...
607606
let can_reuse_cratenum =
608-
locator.triple == self.sess.opts.target_triple || locator.is_proc_macro == Some(true);
607+
locator.triple == self.sess.opts.target_triple || locator.is_proc_macro;
609608
Ok(Some(if can_reuse_cratenum {
610609
let mut result = LoadResult::Loaded(library);
611610
self.cstore.iter_crate_data(|cnum, data| {

compiler/rustc_metadata/src/locator.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ crate struct CrateLocator<'a> {
254254
pub triple: TargetTriple,
255255
pub filesearch: FileSearch<'a>,
256256
root: Option<&'a CratePaths>,
257-
pub is_proc_macro: Option<bool>,
257+
pub is_proc_macro: bool,
258258

259259
// Mutable in-progress state or output.
260260
rejected_via_hash: Vec<CrateMismatch>,
@@ -304,7 +304,6 @@ impl<'a> CrateLocator<'a> {
304304
is_host: bool,
305305
path_kind: PathKind,
306306
root: Option<&'a CratePaths>,
307-
is_proc_macro: Option<bool>,
308307
) -> CrateLocator<'a> {
309308
// The all loop is because `--crate-type=rlib --crate-type=rlib` is
310309
// legal and produces both inside this type.
@@ -349,7 +348,7 @@ impl<'a> CrateLocator<'a> {
349348
sess.target_filesearch(path_kind)
350349
},
351350
root,
352-
is_proc_macro,
351+
is_proc_macro: false,
353352
rejected_via_hash: Vec::new(),
354353
rejected_via_triple: Vec::new(),
355354
rejected_via_kind: Vec::new(),
@@ -491,7 +490,7 @@ impl<'a> CrateLocator<'a> {
491490
}
492491

493492
fn needs_crate_flavor(&self, flavor: CrateFlavor) -> bool {
494-
if flavor == CrateFlavor::Dylib && self.is_proc_macro == Some(true) {
493+
if flavor == CrateFlavor::Dylib && self.is_proc_macro {
495494
return true;
496495
}
497496

@@ -623,15 +622,13 @@ impl<'a> CrateLocator<'a> {
623622
}
624623

625624
let root = metadata.get_root();
626-
if let Some(expected_is_proc_macro) = self.is_proc_macro {
627-
let is_proc_macro = root.is_proc_macro_crate();
628-
if is_proc_macro != expected_is_proc_macro {
629-
info!(
630-
"Rejecting via proc macro: expected {} got {}",
631-
expected_is_proc_macro, is_proc_macro
632-
);
633-
return None;
634-
}
625+
if root.is_proc_macro_crate() != self.is_proc_macro {
626+
info!(
627+
"Rejecting via proc macro: expected {} got {}",
628+
self.is_proc_macro,
629+
root.is_proc_macro_crate(),
630+
);
631+
return None;
635632
}
636633

637634
if self.exact_paths.is_empty() && self.crate_name != root.name() {
@@ -815,7 +812,6 @@ fn find_plugin_registrar_impl<'a>(
815812
true, // is_host
816813
PathKind::Crate,
817814
None, // root
818-
None, // is_proc_macro
819815
);
820816

821817
match locator.maybe_load_library_crate()? {

0 commit comments

Comments
 (0)