Rollup merge of #144059 - LorrensP-2158466:remove-crate-loader, r=petrochenkov

Refactor `CrateLoader` into the `CStore`

Removes the `CrateLoader` and moves the code to `CStore`. Now, if you want to use the `CrateLoader`, you can just use `CStore`.

Should we rename `creader.rs` to `cstore.rs`?

r? ``@petrochenkov``
This commit is contained in:
Matthias Krüger
2025-07-18 19:14:46 +02:00
committed by GitHub
5 changed files with 169 additions and 158 deletions

View File

@@ -45,7 +45,7 @@ use rustc_attr_data_structures::StrippedCfgItem;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::intern::Interned;
use rustc_data_structures::steal::Steal;
use rustc_data_structures::sync::FreezeReadGuard;
use rustc_data_structures::sync::{FreezeReadGuard, FreezeWriteGuard};
use rustc_data_structures::unord::{UnordMap, UnordSet};
use rustc_errors::{Applicability, Diag, ErrCode, ErrorGuaranteed};
use rustc_expand::base::{DeriveResolution, SyntaxExtension, SyntaxExtensionKind};
@@ -58,7 +58,7 @@ use rustc_hir::def_id::{CRATE_DEF_ID, CrateNum, DefId, LOCAL_CRATE, LocalDefId,
use rustc_hir::definitions::DisambiguatorState;
use rustc_hir::{PrimTy, TraitCandidate};
use rustc_index::bit_set::DenseBitSet;
use rustc_metadata::creader::{CStore, CrateLoader};
use rustc_metadata::creader::CStore;
use rustc_middle::metadata::ModChild;
use rustc_middle::middle::privacy::EffectiveVisibilities;
use rustc_middle::query::Providers;
@@ -1119,7 +1119,6 @@ pub struct Resolver<'ra, 'tcx> {
builtin_types_bindings: FxHashMap<Symbol, NameBinding<'ra>>,
builtin_attrs_bindings: FxHashMap<Symbol, NameBinding<'ra>>,
registered_tool_bindings: FxHashMap<Ident, NameBinding<'ra>>,
used_extern_options: FxHashSet<Symbol>,
macro_names: FxHashSet<Ident>,
builtin_macros: FxHashMap<Symbol, SyntaxExtensionKind>,
registered_tools: &'tcx RegisteredTools,
@@ -1554,7 +1553,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
(*ident, binding)
})
.collect(),
used_extern_options: Default::default(),
macro_names: FxHashSet::default(),
builtin_macros: Default::default(),
registered_tools,
@@ -1741,18 +1739,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
StableHashingContext::new(self.tcx.sess, self.tcx.untracked())
}
fn crate_loader<T>(&mut self, f: impl FnOnce(&mut CrateLoader<'_, '_>) -> T) -> T {
f(&mut CrateLoader::new(
self.tcx,
&mut CStore::from_tcx_mut(self.tcx),
&mut self.used_extern_options,
))
}
fn cstore(&self) -> FreezeReadGuard<'_, CStore> {
CStore::from_tcx(self.tcx)
}
fn cstore_mut(&self) -> FreezeWriteGuard<'_, CStore> {
CStore::from_tcx_mut(self.tcx)
}
fn dummy_ext(&self, macro_kind: MacroKind) -> Arc<SyntaxExtension> {
match macro_kind {
MacroKind::Bang => Arc::clone(&self.dummy_ext_bang),
@@ -1798,7 +1792,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
self.tcx.sess.time("resolve_report_errors", || self.report_errors(krate));
self.tcx
.sess
.time("resolve_postprocess", || self.crate_loader(|c| c.postprocess(krate)));
.time("resolve_postprocess", || self.cstore_mut().postprocess(self.tcx, krate));
});
// Make sure we don't mutate the cstore from here on.
@@ -2153,7 +2147,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
Some(if let Some(binding) = entry.binding {
if finalize {
if !entry.is_import() {
self.crate_loader(|c| c.process_path_extern(ident.name, ident.span));
self.cstore_mut().process_path_extern(self.tcx, ident.name, ident.span);
} else if entry.introduced_by_item {
self.record_use(ident, binding, Used::Other);
}
@@ -2162,13 +2156,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
} else {
let crate_id = if finalize {
let Some(crate_id) =
self.crate_loader(|c| c.process_path_extern(ident.name, ident.span))
self.cstore_mut().process_path_extern(self.tcx, ident.name, ident.span)
else {
return Some(self.dummy_binding);
};
crate_id
} else {
self.crate_loader(|c| c.maybe_process_path_extern(ident.name))?
self.cstore_mut().maybe_process_path_extern(self.tcx, ident.name)?
};
let res = Res::Def(DefKind::Mod, crate_id.as_def_id());
self.arenas.new_pub_res_binding(res, DUMMY_SP, LocalExpnId::ROOT)