Move the resolver into a query

This commit is contained in:
Oli Scherer 2023-02-16 14:07:42 +00:00
parent 37e2f4f487
commit c3522d0637
8 changed files with 54 additions and 31 deletions

View File

@ -33,6 +33,7 @@
/// <dt>dylib</dt>
/// <dd>The metadata can be found in the `.rustc` section of the shared library.</dd>
/// </dl>
#[derive(Debug)]
pub struct DefaultMetadataLoader;
fn load_metadata_with(

View File

@ -320,7 +320,7 @@ fn run_compiler(
}
// Make sure name resolution and macro expansion is run.
queries.global_ctxt()?;
queries.global_ctxt()?.enter(|tcx| tcx.resolver_for_lowering(()));
if callbacks.after_expansion(compiler, queries) == Compilation::Stop {
return early_exit();

View File

@ -8,6 +8,7 @@
use rustc_borrowck as mir_borrowck;
use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_data_structures::parallel;
use rustc_data_structures::steal::Steal;
use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
use rustc_errors::PResult;
use rustc_expand::base::{ExtCtxt, LintStoreExpand, ResolverExpand};
@ -172,7 +173,7 @@ fn pre_expansion_lint(
/// harness if one is to be provided, injection of a dependency on the
/// standard library and prelude, and name resolution.
#[instrument(level = "trace", skip(tcx, krate, resolver))]
pub fn configure_and_expand(
fn configure_and_expand(
tcx: TyCtxt<'_>,
mut krate: ast::Crate,
resolver: &mut Resolver<'_, '_>,
@ -564,6 +565,34 @@ fn write_out_deps(
}
}
fn resolver_for_lowering<'tcx>(
tcx: TyCtxt<'tcx>,
(): (),
) -> &'tcx Steal<(ty::ResolverAstLowering, Lrc<ast::Crate>)> {
let arenas = Resolver::arenas();
let krate = tcx.crate_for_resolver(()).steal();
let mut resolver = Resolver::new(
tcx,
&krate,
tcx.crate_name(LOCAL_CRATE),
tcx.metadata_loader(()).steal(),
&arenas,
);
let krate = configure_and_expand(tcx, krate, &mut resolver);
// Make sure we don't mutate the cstore from here on.
tcx.untracked().cstore.leak();
let ty::ResolverOutputs {
global_ctxt: untracked_resolutions,
ast_lowering: untracked_resolver_for_lowering,
} = resolver.into_outputs();
let feed = tcx.feed_unit_query();
feed.resolutions(tcx.arena.alloc(untracked_resolutions));
tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, Lrc::new(krate))))
}
fn output_filenames(tcx: TyCtxt<'_>, (): ()) -> Arc<OutputFilenames> {
let sess = tcx.sess;
let _timer = sess.timer("prepare_outputs");
@ -618,6 +647,7 @@ fn output_filenames(tcx: TyCtxt<'_>, (): ()) -> Arc<OutputFilenames> {
providers.analysis = analysis;
providers.hir_crate = rustc_ast_lowering::lower_to_hir;
providers.output_filenames = output_filenames;
providers.resolver_for_lowering = resolver_for_lowering;
proc_macro_decls::provide(providers);
rustc_const_eval::provide(providers);
rustc_middle::hir::provide(providers);

View File

@ -16,9 +16,8 @@
use rustc_metadata::creader::CStore;
use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::DepGraph;
use rustc_middle::ty::{self, GlobalCtxt, TyCtxt};
use rustc_middle::ty::{GlobalCtxt, TyCtxt};
use rustc_query_impl::Queries as TcxQueries;
use rustc_resolve::Resolver;
use rustc_session::config::{self, OutputFilenames, OutputType};
use rustc_session::cstore::Untracked;
use rustc_session::{output::find_crate_name, Session};
@ -216,34 +215,12 @@ pub fn global_ctxt(&'tcx self) -> Result<QueryResult<'_, &'tcx GlobalCtxt<'tcx>>
qcx.enter(|tcx| {
let feed = tcx.feed_local_crate();
feed.crate_name(crate_name);
let (krate, resolver_outputs) = {
let _timer = sess.timer("configure_and_expand");
let arenas = Resolver::arenas();
let mut resolver = Resolver::new(
tcx,
&krate,
crate_name,
self.codegen_backend().metadata_loader(),
&arenas,
);
let krate = passes::configure_and_expand(tcx, krate, &mut resolver);
// Make sure we don't mutate the cstore from here on.
tcx.untracked().cstore.leak();
(Lrc::new(krate), resolver.into_outputs())
};
let ty::ResolverOutputs {
global_ctxt: untracked_resolutions,
ast_lowering: untracked_resolver_for_lowering,
} = resolver_outputs;
let feed = tcx.feed_unit_query();
feed.resolver_for_lowering(
tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, krate))),
feed.crate_for_resolver(tcx.arena.alloc(Steal::new(krate)));
feed.metadata_loader(
tcx.arena.alloc(Steal::new(self.codegen_backend().metadata_loader())),
);
feed.resolutions(tcx.arena.alloc(untracked_resolutions));
feed.features_query(tcx.sess.features_untracked());
});
Ok(qcx)

View File

@ -35,6 +35,8 @@ macro_rules! arena_types {
rustc_data_structures::sync::Lrc<rustc_ast::Crate>,
)>,
[] output_filenames: std::sync::Arc<rustc_session::config::OutputFilenames>,
[] metadata_loader: rustc_data_structures::steal::Steal<Box<rustc_session::cstore::MetadataLoaderDyn>>,
[] crate_for_resolver: rustc_data_structures::steal::Steal<rustc_ast::ast::Crate>,
[] resolutions: rustc_middle::ty::ResolverGlobalCtxt,
[decode] unsafety_check_result: rustc_middle::mir::UnsafetyCheckResult,
[decode] code_region: rustc_middle::mir::coverage::CodeRegion,

View File

@ -33,7 +33,7 @@
}
query resolver_for_lowering(_: ()) -> &'tcx Steal<(ty::ResolverAstLowering, Lrc<ast::Crate>)> {
feedable
eval_always
no_hash
desc { "getting the resolver for lowering" }
}
@ -2077,6 +2077,18 @@
desc { "looking up enabled feature gates" }
}
query metadata_loader((): ()) -> &'tcx Steal<Box<rustc_session::cstore::MetadataLoaderDyn>> {
feedable
no_hash
desc { "raw operations for metadata file access" }
}
query crate_for_resolver((): ()) -> &'tcx Steal<rustc_ast::ast::Crate> {
feedable
no_hash
desc { "the ast before macro expansion and name resolution" }
}
/// Attempt to resolve the given `DefId` to an `Instance`, for the
/// given generics args (`SubstsRef`), returning one of:
/// * `Ok(Some(instance))` on success

View File

@ -200,7 +200,7 @@ pub enum ExternCrateSource {
/// At the time of this writing, there is only one backend and one way to store
/// metadata in library -- this trait just serves to decouple rustc_metadata from
/// the archive reader, which depends on LLVM.
pub trait MetadataLoader {
pub trait MetadataLoader: std::fmt::Debug {
fn get_rlib_metadata(&self, target: &Target, filename: &Path) -> Result<MetadataRef, String>;
fn get_dylib_metadata(&self, target: &Target, filename: &Path) -> Result<MetadataRef, String>;
}

View File

@ -13,4 +13,5 @@ error: the compiler unexpectedly panicked. this is a bug.
query stack during panic:
#0 [resolver_for_lowering] getting the resolver for lowering
end of query stack