metadata/resolve: Minor refactoring to "tcx -> cstore" conversions

This commit is contained in:
Vadim Petrochenkov 2023-02-22 18:47:59 +04:00
parent 98cce81917
commit 4a61922ef0
2 changed files with 14 additions and 9 deletions
compiler
rustc_metadata/src
rustc_resolve/src

View file

@ -8,7 +8,7 @@ use rustc_ast::expand::allocator::AllocatorKind;
use rustc_ast::{self as ast, *}; use rustc_ast::{self as ast, *};
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::MappedReadGuard; use rustc_data_structures::sync::{MappedReadGuard, MappedWriteGuard, ReadGuard, WriteGuard};
use rustc_expand::base::SyntaxExtension; use rustc_expand::base::SyntaxExtension;
use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE}; use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE};
use rustc_hir::definitions::Definitions; use rustc_hir::definitions::Definitions;
@ -133,8 +133,14 @@ impl<'a> std::fmt::Debug for CrateDump<'a> {
impl CStore { impl CStore {
pub fn from_tcx(tcx: TyCtxt<'_>) -> MappedReadGuard<'_, CStore> { pub fn from_tcx(tcx: TyCtxt<'_>) -> MappedReadGuard<'_, CStore> {
MappedReadGuard::map(tcx.cstore_untracked(), |c| { ReadGuard::map(tcx.untracked().cstore.read(), |cstore| {
c.as_any().downcast_ref::<CStore>().expect("`tcx.cstore` is not a `CStore`") cstore.as_any().downcast_ref::<CStore>().expect("`tcx.cstore` is not a `CStore`")
})
}
pub fn from_tcx_mut(tcx: TyCtxt<'_>) -> MappedWriteGuard<'_, CStore> {
WriteGuard::map(tcx.untracked().cstore.write(), |cstore| {
cstore.untracked_as_any().downcast_mut().expect("`tcx.cstore` is not a `CStore`")
}) })
} }
@ -268,9 +274,6 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
) -> Self { ) -> Self {
CrateLoader { tcx, cstore, used_extern_options } CrateLoader { tcx, cstore, used_extern_options }
} }
pub fn cstore(&self) -> &CStore {
&self.cstore
}
fn existing_match(&self, name: Symbol, hash: Option<Svh>, kind: PathKind) -> Option<CrateNum> { fn existing_match(&self, name: Symbol, hash: Option<Svh>, kind: PathKind) -> Option<CrateNum> {
for (cnum, data) in self.cstore.iter_crate_data() { for (cnum, data) in self.cstore.iter_crate_data() {

View file

@ -1431,9 +1431,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
} }
fn crate_loader<T>(&mut self, f: impl FnOnce(&mut CrateLoader<'_, '_>) -> T) -> T { fn crate_loader<T>(&mut self, f: impl FnOnce(&mut CrateLoader<'_, '_>) -> T) -> T {
let mut cstore = self.tcx.untracked().cstore.write(); f(&mut CrateLoader::new(
let cstore = cstore.untracked_as_any().downcast_mut().unwrap(); self.tcx,
f(&mut CrateLoader::new(self.tcx, &mut *cstore, &mut self.used_extern_options)) &mut CStore::from_tcx_mut(self.tcx),
&mut self.used_extern_options,
))
} }
fn cstore(&self) -> MappedReadGuard<'_, CStore> { fn cstore(&self) -> MappedReadGuard<'_, CStore> {