From 4a61922ef00153a2c6e74d3cc97f687c9c8c6817 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 22 Feb 2023 18:47:59 +0400 Subject: [PATCH] metadata/resolve: Minor refactoring to "tcx -> cstore" conversions --- compiler/rustc_metadata/src/creader.rs | 15 +++++++++------ compiler/rustc_resolve/src/lib.rs | 8 +++++--- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index b05626311e8..f870a1db82d 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -8,7 +8,7 @@ use rustc_ast::{self as ast, *}; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; 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_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE}; use rustc_hir::definitions::Definitions; @@ -133,8 +133,14 @@ fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { impl CStore { pub fn from_tcx(tcx: TyCtxt<'_>) -> MappedReadGuard<'_, CStore> { - MappedReadGuard::map(tcx.cstore_untracked(), |c| { - c.as_any().downcast_ref::().expect("`tcx.cstore` is not a `CStore`") + ReadGuard::map(tcx.untracked().cstore.read(), |cstore| { + cstore.as_any().downcast_ref::().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 @@ pub fn new( ) -> Self { CrateLoader { tcx, cstore, used_extern_options } } - pub fn cstore(&self) -> &CStore { - &self.cstore - } fn existing_match(&self, name: Symbol, hash: Option, kind: PathKind) -> Option { for (cnum, data) in self.cstore.iter_crate_data() { diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index ae1d9406467..cd90fd3ef84 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1431,9 +1431,11 @@ fn create_stable_hashing_context(&self) -> StableHashingContext<'_> { } fn crate_loader(&mut self, f: impl FnOnce(&mut CrateLoader<'_, '_>) -> T) -> T { - let mut cstore = self.tcx.untracked().cstore.write(); - let cstore = cstore.untracked_as_any().downcast_mut().unwrap(); - f(&mut CrateLoader::new(self.tcx, &mut *cstore, &mut self.used_extern_options)) + f(&mut CrateLoader::new( + self.tcx, + &mut CStore::from_tcx_mut(self.tcx), + &mut self.used_extern_options, + )) } fn cstore(&self) -> MappedReadGuard<'_, CStore> {