Skip to content

Commit 8763305

Browse files
committed
Pass CrateRejections separately from CrateLocator
This allows all CrateLocator methods to take &self.
1 parent 121dac5 commit 8763305

File tree

2 files changed

+87
-63
lines changed

2 files changed

+87
-63
lines changed

compiler/rustc_metadata/src/creader.rs

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use rustc_target::spec::{PanicStrategy, Target, TargetTuple};
3737
use tracing::{debug, info, trace};
3838

3939
use crate::errors;
40-
use crate::locator::{CrateError, CrateLocator, CratePaths};
40+
use crate::locator::{CrateError, CrateLocator, CratePaths, CrateRejections};
4141
use crate::rmeta::{
4242
CrateDep, CrateMetadata, CrateNumMap, CrateRoot, MetadataBlob, TargetModifiers,
4343
};
@@ -684,47 +684,51 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
684684
fn load_proc_macro<'b>(
685685
&self,
686686
locator: &mut CrateLocator<'b>,
687+
crate_rejections: &mut CrateRejections,
687688
path_kind: PathKind,
688689
host_hash: Option<Svh>,
689690
) -> Result<Option<(LoadResult, Option<Library>)>, CrateError>
690691
where
691692
'a: 'b,
692693
{
693-
// Use a new crate locator so trying to load a proc macro doesn't affect the error
694-
// message we emit
694+
// Use a new crate locator and crate rejections so trying to load a proc macro doesn't
695+
// affect the error message we emit
695696
let mut proc_macro_locator = locator.clone();
697+
let mut proc_macro_crate_rejections = CrateRejections::default();
696698

697699
// Try to load a proc macro
698700
proc_macro_locator.is_proc_macro = true;
699701

700702
// Load the proc macro crate for the target
701-
let (locator, target_result) = if self.sess.opts.unstable_opts.dual_proc_macros {
702-
proc_macro_locator.reset();
703-
let result = match self.load(&mut proc_macro_locator)? {
704-
Some(LoadResult::Previous(cnum)) => {
705-
return Ok(Some((LoadResult::Previous(cnum), None)));
706-
}
707-
Some(LoadResult::Loaded(library)) => Some(LoadResult::Loaded(library)),
708-
None => return Ok(None),
703+
let (locator, crate_rejections, target_result) =
704+
if self.sess.opts.unstable_opts.dual_proc_macros {
705+
let result =
706+
match self.load(&mut proc_macro_locator, &mut proc_macro_crate_rejections)? {
707+
Some(LoadResult::Previous(cnum)) => {
708+
return Ok(Some((LoadResult::Previous(cnum), None)));
709+
}
710+
Some(LoadResult::Loaded(library)) => Some(LoadResult::Loaded(library)),
711+
None => return Ok(None),
712+
};
713+
locator.hash = host_hash;
714+
// Use the locator when looking for the host proc macro crate, as that is required
715+
// so we want it to affect the error message
716+
(locator, crate_rejections, result)
717+
} else {
718+
(&mut proc_macro_locator, &mut proc_macro_crate_rejections, None)
709719
};
710-
locator.hash = host_hash;
711-
// Use the locator when looking for the host proc macro crate, as that is required
712-
// so we want it to affect the error message
713-
(locator, result)
714-
} else {
715-
(&mut proc_macro_locator, None)
716-
};
717720

718721
// Load the proc macro crate for the host
719722

720-
locator.reset();
723+
*crate_rejections = CrateRejections::default();
724+
// FIXME use a separate CrateLocator for the host rather than mutating the target CrateLocator
721725
locator.is_proc_macro = true;
722726
locator.target = &self.sess.host;
723727
locator.tuple = TargetTuple::from_tuple(config::host_tuple());
724728
locator.filesearch = self.sess.host_filesearch();
725729
locator.path_kind = path_kind;
726730

727-
let Some(host_result) = self.load(locator)? else {
731+
let Some(host_result) = self.load(locator, crate_rejections)? else {
728732
return Ok(None);
729733
};
730734

@@ -799,15 +803,21 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
799803
extra_filename,
800804
path_kind,
801805
);
806+
let mut crate_rejections = CrateRejections::default();
802807

803-
match self.load(&mut locator)? {
808+
match self.load(&mut locator, &mut crate_rejections)? {
804809
Some(res) => (res, None),
805810
None => {
806811
info!("falling back to loading proc_macro");
807812
dep_kind = CrateDepKind::MacrosOnly;
808-
match self.load_proc_macro(&mut locator, path_kind, host_hash)? {
813+
match self.load_proc_macro(
814+
&mut locator,
815+
&mut crate_rejections,
816+
path_kind,
817+
host_hash,
818+
)? {
809819
Some(res) => res,
810-
None => return Err(locator.into_error(dep_root.cloned())),
820+
None => return Err(locator.into_error(crate_rejections, dep_root.cloned())),
811821
}
812822
}
813823
}
@@ -837,8 +847,12 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
837847
}
838848
}
839849

840-
fn load(&self, locator: &mut CrateLocator<'_>) -> Result<Option<LoadResult>, CrateError> {
841-
let Some(library) = locator.maybe_load_library_crate()? else {
850+
fn load(
851+
&self,
852+
locator: &CrateLocator<'_>,
853+
crate_rejections: &mut CrateRejections,
854+
) -> Result<Option<LoadResult>, CrateError> {
855+
let Some(library) = locator.maybe_load_library_crate(crate_rejections)? else {
842856
return Ok(None);
843857
};
844858

compiler/rustc_metadata/src/locator.rs

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,7 @@ pub(crate) struct CrateLocator<'a> {
255255
pub tuple: TargetTuple,
256256
pub filesearch: &'a FileSearch,
257257
pub is_proc_macro: bool,
258-
259258
pub path_kind: PathKind,
260-
// Mutable in-progress state or output.
261-
crate_rejections: CrateRejections,
262259
}
263260

264261
#[derive(Clone, Debug)]
@@ -346,34 +343,30 @@ impl<'a> CrateLocator<'a> {
346343
filesearch: sess.target_filesearch(),
347344
path_kind,
348345
is_proc_macro: false,
349-
crate_rejections: CrateRejections::default(),
350346
}
351347
}
352348

353-
pub(crate) fn reset(&mut self) {
354-
self.crate_rejections.via_hash.clear();
355-
self.crate_rejections.via_triple.clear();
356-
self.crate_rejections.via_kind.clear();
357-
self.crate_rejections.via_version.clear();
358-
self.crate_rejections.via_filename.clear();
359-
self.crate_rejections.via_invalid.clear();
360-
}
361-
362-
pub(crate) fn maybe_load_library_crate(&mut self) -> Result<Option<Library>, CrateError> {
349+
pub(crate) fn maybe_load_library_crate(
350+
&self,
351+
crate_rejections: &mut CrateRejections,
352+
) -> Result<Option<Library>, CrateError> {
363353
if !self.exact_paths.is_empty() {
364-
return self.find_commandline_library();
354+
return self.find_commandline_library(crate_rejections);
365355
}
366356
let mut seen_paths = FxHashSet::default();
367357
if let Some(extra_filename) = self.extra_filename {
368-
if let library @ Some(_) = self.find_library_crate(extra_filename, &mut seen_paths)? {
358+
if let library @ Some(_) =
359+
self.find_library_crate(crate_rejections, extra_filename, &mut seen_paths)?
360+
{
369361
return Ok(library);
370362
}
371363
}
372-
self.find_library_crate("", &mut seen_paths)
364+
self.find_library_crate(crate_rejections, "", &mut seen_paths)
373365
}
374366

375367
fn find_library_crate(
376-
&mut self,
368+
&self,
369+
crate_rejections: &mut CrateRejections,
377370
extra_prefix: &str,
378371
seen_paths: &mut FxHashSet<PathBuf>,
379372
) -> Result<Option<Library>, CrateError> {
@@ -465,7 +458,7 @@ impl<'a> CrateLocator<'a> {
465458
.flatten()
466459
{
467460
for (_, spf) in static_matches {
468-
self.crate_rejections.via_kind.push(CrateMismatch {
461+
crate_rejections.via_kind.push(CrateMismatch {
469462
path: spf.path.to_path_buf(),
470463
got: "static".to_string(),
471464
});
@@ -483,7 +476,9 @@ impl<'a> CrateLocator<'a> {
483476
// search is being performed for.
484477
let mut libraries = FxIndexMap::default();
485478
for (_hash, (rlibs, rmetas, dylibs, interfaces)) in candidates {
486-
if let Some((svh, lib)) = self.extract_lib(rlibs, rmetas, dylibs, interfaces)? {
479+
if let Some((svh, lib)) =
480+
self.extract_lib(crate_rejections, rlibs, rmetas, dylibs, interfaces)?
481+
{
487482
libraries.insert(svh, lib);
488483
}
489484
}
@@ -512,7 +507,8 @@ impl<'a> CrateLocator<'a> {
512507
}
513508

514509
fn extract_lib(
515-
&mut self,
510+
&self,
511+
crate_rejections: &mut CrateRejections,
516512
rlibs: FxIndexMap<PathBuf, PathKind>,
517513
rmetas: FxIndexMap<PathBuf, PathKind>,
518514
dylibs: FxIndexMap<PathBuf, PathKind>,
@@ -524,10 +520,11 @@ impl<'a> CrateLocator<'a> {
524520
// Make sure there's at most one rlib and at most one dylib.
525521
//
526522
// See comment in `extract_one` below.
527-
let rmeta = self.extract_one(rmetas, CrateFlavor::Rmeta, &mut slot)?;
528-
let rlib = self.extract_one(rlibs, CrateFlavor::Rlib, &mut slot)?;
529-
let sdylib_interface = self.extract_one(interfaces, CrateFlavor::SDylib, &mut slot)?;
530-
let dylib = self.extract_one(dylibs, CrateFlavor::Dylib, &mut slot)?;
523+
let rmeta = self.extract_one(crate_rejections, rmetas, CrateFlavor::Rmeta, &mut slot)?;
524+
let rlib = self.extract_one(crate_rejections, rlibs, CrateFlavor::Rlib, &mut slot)?;
525+
let sdylib_interface =
526+
self.extract_one(crate_rejections, interfaces, CrateFlavor::SDylib, &mut slot)?;
527+
let dylib = self.extract_one(crate_rejections, dylibs, CrateFlavor::Dylib, &mut slot)?;
531528

532529
if sdylib_interface.is_some() && dylib.is_none() {
533530
return Err(CrateError::FullMetadataNotFound(self.crate_name, CrateFlavor::SDylib));
@@ -561,7 +558,8 @@ impl<'a> CrateLocator<'a> {
561558
//
562559
// The `PathBuf` in `slot` will only be used for diagnostic purposes.
563560
fn extract_one(
564-
&mut self,
561+
&self,
562+
crate_rejections: &mut CrateRejections,
565563
m: FxIndexMap<PathBuf, PathKind>,
566564
flavor: CrateFlavor,
567565
slot: &mut Option<(Svh, MetadataBlob, PathBuf, CrateFlavor)>,
@@ -603,7 +601,7 @@ impl<'a> CrateLocator<'a> {
603601
Some(self.crate_name),
604602
) {
605603
Ok(blob) => {
606-
if let Some(h) = self.crate_matches(&blob, &lib) {
604+
if let Some(h) = self.crate_matches(crate_rejections, &blob, &lib) {
607605
(h, blob)
608606
} else {
609607
info!("metadata mismatch");
@@ -618,7 +616,7 @@ impl<'a> CrateLocator<'a> {
618616
"Rejecting via version: expected {} got {}",
619617
expected_version, found_version
620618
);
621-
self.crate_rejections
619+
crate_rejections
622620
.via_version
623621
.push(CrateMismatch { path: lib, got: found_version });
624622
continue;
@@ -633,7 +631,7 @@ impl<'a> CrateLocator<'a> {
633631
// The file was present and created by the same compiler version, but we
634632
// couldn't load it for some reason. Give a hard error instead of silently
635633
// ignoring it, but only if we would have given an error anyway.
636-
self.crate_rejections.via_invalid.push(CrateMismatch { path: lib, got: err });
634+
crate_rejections.via_invalid.push(CrateMismatch { path: lib, got: err });
637635
continue;
638636
}
639637
Err(err @ MetadataError::NotPresent(_)) => {
@@ -711,7 +709,12 @@ impl<'a> CrateLocator<'a> {
711709
}
712710
}
713711

714-
fn crate_matches(&mut self, metadata: &MetadataBlob, libpath: &Path) -> Option<Svh> {
712+
fn crate_matches(
713+
&self,
714+
crate_rejections: &mut CrateRejections,
715+
metadata: &MetadataBlob,
716+
libpath: &Path,
717+
) -> Option<Svh> {
715718
let header = metadata.get_header();
716719
if header.is_proc_macro_crate != self.is_proc_macro {
717720
info!(
@@ -728,7 +731,7 @@ impl<'a> CrateLocator<'a> {
728731

729732
if header.triple != self.tuple {
730733
info!("Rejecting via crate triple: expected {} got {}", self.tuple, header.triple);
731-
self.crate_rejections.via_triple.push(CrateMismatch {
734+
crate_rejections.via_triple.push(CrateMismatch {
732735
path: libpath.to_path_buf(),
733736
got: header.triple.to_string(),
734737
});
@@ -739,7 +742,7 @@ impl<'a> CrateLocator<'a> {
739742
if let Some(expected_hash) = self.hash {
740743
if hash != expected_hash {
741744
info!("Rejecting via hash: expected {} got {}", expected_hash, hash);
742-
self.crate_rejections
745+
crate_rejections
743746
.via_hash
744747
.push(CrateMismatch { path: libpath.to_path_buf(), got: hash.to_string() });
745748
return None;
@@ -749,7 +752,10 @@ impl<'a> CrateLocator<'a> {
749752
Some(hash)
750753
}
751754

752-
fn find_commandline_library(&mut self) -> Result<Option<Library>, CrateError> {
755+
fn find_commandline_library(
756+
&self,
757+
crate_rejections: &mut CrateRejections,
758+
) -> Result<Option<Library>, CrateError> {
753759
// First, filter out all libraries that look suspicious. We only accept
754760
// files which actually exist that have the correct naming scheme for
755761
// rlibs/dylibs.
@@ -794,24 +800,28 @@ impl<'a> CrateLocator<'a> {
794800
dylibs.insert(loc_canon.clone(), PathKind::ExternFlag);
795801
continue;
796802
}
797-
self.crate_rejections
803+
crate_rejections
798804
.via_filename
799805
.push(CrateMismatch { path: loc_orig.clone(), got: String::new() });
800806
}
801807

802808
// Extract the dylib/rlib/rmeta triple.
803-
self.extract_lib(rlibs, rmetas, dylibs, sdylib_interfaces)
809+
self.extract_lib(crate_rejections, rlibs, rmetas, dylibs, sdylib_interfaces)
804810
.map(|opt| opt.map(|(_, lib)| lib))
805811
}
806812

807-
pub(crate) fn into_error(self, dep_root: Option<CratePaths>) -> CrateError {
813+
pub(crate) fn into_error(
814+
self,
815+
crate_rejections: CrateRejections,
816+
dep_root: Option<CratePaths>,
817+
) -> CrateError {
808818
CrateError::LocatorCombined(Box::new(CombinedLocatorError {
809819
crate_name: self.crate_name,
810820
dep_root,
811821
triple: self.tuple,
812822
dll_prefix: self.target.dll_prefix.to_string(),
813823
dll_suffix: self.target.dll_suffix.to_string(),
814-
crate_rejections: self.crate_rejections,
824+
crate_rejections,
815825
}))
816826
}
817827
}
@@ -991,7 +1001,7 @@ struct CrateMismatch {
9911001
}
9921002

9931003
#[derive(Clone, Debug, Default)]
994-
struct CrateRejections {
1004+
pub(crate) struct CrateRejections {
9951005
via_hash: Vec<CrateMismatch>,
9961006
via_triple: Vec<CrateMismatch>,
9971007
via_kind: Vec<CrateMismatch>,

0 commit comments

Comments
 (0)