1
Fork 0

Stop passing in values that one can also get from the tcx lazily

This commit is contained in:
Oli Scherer 2023-02-16 14:45:26 +00:00
parent c3522d0637
commit acbcfaaf7b
3 changed files with 23 additions and 34 deletions

View file

@ -571,13 +571,7 @@ fn resolver_for_lowering<'tcx>(
) -> &'tcx Steal<(ty::ResolverAstLowering, Lrc<ast::Crate>)> { ) -> &'tcx Steal<(ty::ResolverAstLowering, Lrc<ast::Crate>)> {
let arenas = Resolver::arenas(); let arenas = Resolver::arenas();
let krate = tcx.crate_for_resolver(()).steal(); let krate = tcx.crate_for_resolver(()).steal();
let mut resolver = Resolver::new( let mut resolver = Resolver::new(tcx, &krate, &arenas);
tcx,
&krate,
tcx.crate_name(LOCAL_CRATE),
tcx.metadata_loader(()).steal(),
&arenas,
);
let krate = configure_and_expand(tcx, krate, &mut resolver); let krate = configure_and_expand(tcx, krate, &mut resolver);
// Make sure we don't mutate the cstore from here on. // Make sure we don't mutate the cstore from here on.

View file

@ -15,8 +15,8 @@ use rustc_hir::definitions::Definitions;
use rustc_index::vec::IndexVec; use rustc_index::vec::IndexVec;
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_session::config::{self, CrateType, ExternLocation}; use rustc_session::config::{self, CrateType, ExternLocation};
use rustc_session::cstore::ExternCrateSource;
use rustc_session::cstore::{CrateDepKind, CrateSource, ExternCrate}; use rustc_session::cstore::{CrateDepKind, CrateSource, ExternCrate};
use rustc_session::cstore::{ExternCrateSource, MetadataLoaderDyn};
use rustc_session::lint; use rustc_session::lint;
use rustc_session::output::validate_crate_name; use rustc_session::output::validate_crate_name;
use rustc_session::search_paths::PathKind; use rustc_session::search_paths::PathKind;
@ -60,16 +60,22 @@ impl std::fmt::Debug for CStore {
} }
} }
pub struct CrateLoader<'a> { pub struct CrateLoader<'a, 'tcx: 'a> {
// Immutable configuration. // Immutable configuration.
sess: &'a Session, tcx: TyCtxt<'tcx>,
metadata_loader: &'a MetadataLoaderDyn,
local_crate_name: Symbol,
// Mutable output. // Mutable output.
cstore: &'a mut CStore, cstore: &'a mut CStore,
used_extern_options: &'a mut FxHashSet<Symbol>, used_extern_options: &'a mut FxHashSet<Symbol>,
} }
impl<'a, 'tcx> std::ops::Deref for CrateLoader<'a, 'tcx> {
type Target = TyCtxt<'tcx>;
fn deref(&self) -> &Self::Target {
&self.tcx
}
}
pub enum LoadedMacro { pub enum LoadedMacro {
MacroDef(ast::Item, Edition), MacroDef(ast::Item, Edition),
ProcMacro(SyntaxExtension), ProcMacro(SyntaxExtension),
@ -254,15 +260,13 @@ impl CStore {
} }
} }
impl<'a> CrateLoader<'a> { impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
pub fn new( pub fn new(
sess: &'a Session, tcx: TyCtxt<'tcx>,
metadata_loader: &'a MetadataLoaderDyn,
local_crate_name: Symbol,
cstore: &'a mut CStore, cstore: &'a mut CStore,
used_extern_options: &'a mut FxHashSet<Symbol>, used_extern_options: &'a mut FxHashSet<Symbol>,
) -> Self { ) -> Self {
CrateLoader { sess, metadata_loader, local_crate_name, cstore, used_extern_options } CrateLoader { tcx, cstore, used_extern_options }
} }
pub fn cstore(&self) -> &CStore { pub fn cstore(&self) -> &CStore {
&self.cstore &self.cstore
@ -553,9 +557,10 @@ impl<'a> CrateLoader<'a> {
(LoadResult::Previous(cnum), None) (LoadResult::Previous(cnum), None)
} else { } else {
info!("falling back to a load"); info!("falling back to a load");
let metadata_loader = self.tcx.metadata_loader(()).borrow();
let mut locator = CrateLocator::new( let mut locator = CrateLocator::new(
self.sess, self.sess,
&*self.metadata_loader, &**metadata_loader,
name, name,
hash, hash,
extra_filename, extra_filename,
@ -960,7 +965,7 @@ impl<'a> CrateLoader<'a> {
&format!( &format!(
"external crate `{}` unused in `{}`: remove the dependency or add `use {} as _;`", "external crate `{}` unused in `{}`: remove the dependency or add `use {} as _;`",
name, name,
self.local_crate_name, self.tcx.crate_name(LOCAL_CRATE),
name), name),
); );
} }

View file

@ -44,7 +44,7 @@ use rustc_middle::span_bug;
use rustc_middle::ty::{self, DefIdTree, MainDefinition, RegisteredTools, TyCtxt}; use rustc_middle::ty::{self, DefIdTree, MainDefinition, RegisteredTools, TyCtxt};
use rustc_middle::ty::{ResolverGlobalCtxt, ResolverOutputs}; use rustc_middle::ty::{ResolverGlobalCtxt, ResolverOutputs};
use rustc_query_system::ich::StableHashingContext; use rustc_query_system::ich::StableHashingContext;
use rustc_session::cstore::{CrateStore, MetadataLoaderDyn, Untracked}; use rustc_session::cstore::{CrateStore, Untracked};
use rustc_session::lint::LintBuffer; use rustc_session::lint::LintBuffer;
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind, SyntaxContext, Transparency}; use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind, SyntaxContext, Transparency};
use rustc_span::source_map::Spanned; use rustc_span::source_map::Spanned;
@ -955,8 +955,6 @@ pub struct Resolver<'a, 'tcx> {
arenas: &'a ResolverArenas<'a>, arenas: &'a ResolverArenas<'a>,
dummy_binding: &'a NameBinding<'a>, dummy_binding: &'a NameBinding<'a>,
local_crate_name: Symbol,
metadata_loader: Box<MetadataLoaderDyn>,
used_extern_options: FxHashSet<Symbol>, used_extern_options: FxHashSet<Symbol>,
macro_names: FxHashSet<Ident>, macro_names: FxHashSet<Ident>,
builtin_macros: FxHashMap<Symbol, BuiltinMacroState>, builtin_macros: FxHashMap<Symbol, BuiltinMacroState>,
@ -1203,8 +1201,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
pub fn new( pub fn new(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
krate: &Crate, krate: &Crate,
crate_name: Symbol,
metadata_loader: Box<MetadataLoaderDyn>,
arenas: &'a ResolverArenas<'a>, arenas: &'a ResolverArenas<'a>,
) -> Resolver<'a, 'tcx> { ) -> Resolver<'a, 'tcx> {
let root_def_id = CRATE_DEF_ID.to_def_id(); let root_def_id = CRATE_DEF_ID.to_def_id();
@ -1312,8 +1308,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
vis: ty::Visibility::Public, vis: ty::Visibility::Public,
}), }),
metadata_loader,
local_crate_name: crate_name,
used_extern_options: Default::default(), used_extern_options: Default::default(),
macro_names: FxHashSet::default(), macro_names: FxHashSet::default(),
builtin_macros: Default::default(), builtin_macros: Default::default(),
@ -1464,14 +1458,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
StableHashingContext::new(self.tcx.sess, self.tcx.untracked()) StableHashingContext::new(self.tcx.sess, self.tcx.untracked())
} }
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 {
f(&mut CrateLoader::new( let mut cstore = self.tcx.untracked().cstore.write();
&self.tcx.sess, let cstore = cstore.untracked_as_any().downcast_mut().unwrap();
&*self.metadata_loader, f(&mut CrateLoader::new(self.tcx, &mut *cstore, &mut self.used_extern_options))
self.local_crate_name,
&mut *self.tcx.untracked().cstore.write().untracked_as_any().downcast_mut().unwrap(),
&mut self.used_extern_options,
))
} }
fn cstore(&self) -> MappedReadGuard<'_, CStore> { fn cstore(&self) -> MappedReadGuard<'_, CStore> {