Skip to content

Commit 4a61922

Browse files
committed
metadata/resolve: Minor refactoring to "tcx -> cstore" conversions
1 parent 98cce81 commit 4a61922

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

compiler/rustc_metadata/src/creader.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_ast::expand::allocator::AllocatorKind;
88
use rustc_ast::{self as ast, *};
99
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1010
use rustc_data_structures::svh::Svh;
11-
use rustc_data_structures::sync::MappedReadGuard;
11+
use rustc_data_structures::sync::{MappedReadGuard, MappedWriteGuard, ReadGuard, WriteGuard};
1212
use rustc_expand::base::SyntaxExtension;
1313
use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE};
1414
use rustc_hir::definitions::Definitions;
@@ -133,8 +133,14 @@ impl<'a> std::fmt::Debug for CrateDump<'a> {
133133

134134
impl CStore {
135135
pub fn from_tcx(tcx: TyCtxt<'_>) -> MappedReadGuard<'_, CStore> {
136-
MappedReadGuard::map(tcx.cstore_untracked(), |c| {
137-
c.as_any().downcast_ref::<CStore>().expect("`tcx.cstore` is not a `CStore`")
136+
ReadGuard::map(tcx.untracked().cstore.read(), |cstore| {
137+
cstore.as_any().downcast_ref::<CStore>().expect("`tcx.cstore` is not a `CStore`")
138+
})
139+
}
140+
141+
pub fn from_tcx_mut(tcx: TyCtxt<'_>) -> MappedWriteGuard<'_, CStore> {
142+
WriteGuard::map(tcx.untracked().cstore.write(), |cstore| {
143+
cstore.untracked_as_any().downcast_mut().expect("`tcx.cstore` is not a `CStore`")
138144
})
139145
}
140146

@@ -268,9 +274,6 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
268274
) -> Self {
269275
CrateLoader { tcx, cstore, used_extern_options }
270276
}
271-
pub fn cstore(&self) -> &CStore {
272-
&self.cstore
273-
}
274277

275278
fn existing_match(&self, name: Symbol, hash: Option<Svh>, kind: PathKind) -> Option<CrateNum> {
276279
for (cnum, data) in self.cstore.iter_crate_data() {

compiler/rustc_resolve/src/lib.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1431,9 +1431,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14311431
}
14321432

14331433
fn crate_loader<T>(&mut self, f: impl FnOnce(&mut CrateLoader<'_, '_>) -> T) -> T {
1434-
let mut cstore = self.tcx.untracked().cstore.write();
1435-
let cstore = cstore.untracked_as_any().downcast_mut().unwrap();
1436-
f(&mut CrateLoader::new(self.tcx, &mut *cstore, &mut self.used_extern_options))
1434+
f(&mut CrateLoader::new(
1435+
self.tcx,
1436+
&mut CStore::from_tcx_mut(self.tcx),
1437+
&mut self.used_extern_options,
1438+
))
14371439
}
14381440

14391441
fn cstore(&self) -> MappedReadGuard<'_, CStore> {

0 commit comments

Comments
 (0)