1
Fork 0

Make Compiler::{sess,codegen_backend} public.

And remove the relevant getters on `Compiler` and `Queries`.
This commit is contained in:
Nicholas Nethercote 2023-11-20 13:26:09 +11:00
parent 09c807ed82
commit 3a4798c92d
8 changed files with 28 additions and 41 deletions

View file

@ -360,8 +360,8 @@ fn run_compiler(
drop(default_handler); drop(default_handler);
interface::run_compiler(config, |compiler| { interface::run_compiler(config, |compiler| {
let sess = compiler.session(); let sess = &compiler.sess;
let codegen_backend = compiler.codegen_backend(); let codegen_backend = &*compiler.codegen_backend;
// This implements `-Whelp`. It should be handled very early, like // This implements `-Whelp`. It should be handled very early, like
// `--help`/`-Zhelp`/`-Chelp`. This is the earliest it can run, because // `--help`/`-Zhelp`/`-Chelp`. This is the earliest it can run, because
@ -672,7 +672,7 @@ fn process_rlink(sess: &Session, compiler: &interface::Compiler) {
}; };
} }
}; };
let result = compiler.codegen_backend().link(sess, codegen_results, &outputs); let result = compiler.codegen_backend.link(sess, codegen_results, &outputs);
abort_on_err(result, sess); abort_on_err(result, sess);
} else { } else {
sess.emit_fatal(RlinkNotAFile {}) sess.emit_fatal(RlinkNotAFile {})

View file

@ -38,18 +38,12 @@ pub type Result<T> = result::Result<T, ErrorGuaranteed>;
/// Can be used to run `rustc_interface` queries. /// Can be used to run `rustc_interface` queries.
/// Created by passing [`Config`] to [`run_compiler`]. /// Created by passing [`Config`] to [`run_compiler`].
pub struct Compiler { pub struct Compiler {
sess: Session, pub sess: Session,
codegen_backend: Box<dyn CodegenBackend>, pub codegen_backend: Box<dyn CodegenBackend>,
pub(crate) override_queries: Option<fn(&Session, &mut Providers)>, pub(crate) override_queries: Option<fn(&Session, &mut Providers)>,
} }
impl Compiler { impl Compiler {
pub fn session(&self) -> &Session {
&self.sess
}
pub fn codegen_backend(&self) -> &dyn CodegenBackend {
&*self.codegen_backend
}
pub fn build_output_filenames( pub fn build_output_filenames(
&self, &self,
sess: &Session, sess: &Session,

View file

@ -645,10 +645,10 @@ pub fn create_global_ctxt<'tcx>(
// incr. comp. yet. // incr. comp. yet.
dep_graph.assert_ignored(); dep_graph.assert_ignored();
let sess = &compiler.session(); let sess = &compiler.sess;
let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess); let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);
let codegen_backend = compiler.codegen_backend(); let codegen_backend = &compiler.codegen_backend;
let mut providers = *DEFAULT_QUERY_PROVIDERS; let mut providers = *DEFAULT_QUERY_PROVIDERS;
codegen_backend.provide(&mut providers); codegen_backend.provide(&mut providers);

View file

@ -17,7 +17,8 @@ use rustc_middle::dep_graph::DepGraph;
use rustc_middle::ty::{GlobalCtxt, TyCtxt}; use rustc_middle::ty::{GlobalCtxt, TyCtxt};
use rustc_session::config::{self, CrateType, OutputFilenames, OutputType}; use rustc_session::config::{self, CrateType, OutputFilenames, OutputType};
use rustc_session::cstore::Untracked; use rustc_session::cstore::Untracked;
use rustc_session::{output::find_crate_name, Session}; use rustc_session::output::find_crate_name;
use rustc_session::Session;
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
use std::any::Any; use std::any::Any;
use std::cell::{RefCell, RefMut}; use std::cell::{RefCell, RefMut};
@ -101,17 +102,10 @@ impl<'tcx> Queries<'tcx> {
} }
} }
fn session(&self) -> &Session {
self.compiler.session()
}
fn codegen_backend(&self) -> &dyn CodegenBackend {
self.compiler.codegen_backend()
}
pub fn parse(&self) -> Result<QueryResult<'_, ast::Crate>> { pub fn parse(&self) -> Result<QueryResult<'_, ast::Crate>> {
self.parse self.parse.compute(|| {
.compute(|| passes::parse(self.session()).map_err(|mut parse_error| parse_error.emit())) passes::parse(&self.compiler.sess).map_err(|mut parse_error| parse_error.emit())
})
} }
#[deprecated = "pre_configure may be made private in the future. If you need it please open an issue with your use case."] #[deprecated = "pre_configure may be made private in the future. If you need it please open an issue with your use case."]
@ -119,7 +113,7 @@ impl<'tcx> Queries<'tcx> {
self.pre_configure.compute(|| { self.pre_configure.compute(|| {
let mut krate = self.parse()?.steal(); let mut krate = self.parse()?.steal();
let sess = self.session(); let sess = &self.compiler.sess;
rustc_builtin_macros::cmdline_attrs::inject( rustc_builtin_macros::cmdline_attrs::inject(
&mut krate, &mut krate,
&sess.parse_sess, &sess.parse_sess,
@ -134,7 +128,7 @@ impl<'tcx> Queries<'tcx> {
pub fn global_ctxt(&'tcx self) -> Result<QueryResult<'_, &'tcx GlobalCtxt<'tcx>>> { pub fn global_ctxt(&'tcx self) -> Result<QueryResult<'_, &'tcx GlobalCtxt<'tcx>>> {
self.gcx.compute(|| { self.gcx.compute(|| {
let sess = self.session(); let sess = &self.compiler.sess;
#[allow(deprecated)] #[allow(deprecated)]
let (krate, pre_configured_attrs) = self.pre_configure()?.steal(); let (krate, pre_configured_attrs) = self.pre_configure()?.steal();
@ -150,7 +144,7 @@ impl<'tcx> Queries<'tcx> {
let dep_graph = setup_dep_graph(sess, crate_name, stable_crate_id)?; let dep_graph = setup_dep_graph(sess, crate_name, stable_crate_id)?;
let cstore = FreezeLock::new(Box::new(CStore::new( let cstore = FreezeLock::new(Box::new(CStore::new(
self.codegen_backend().metadata_loader(), self.compiler.codegen_backend.metadata_loader(),
stable_crate_id, stable_crate_id,
)) as _); )) as _);
let definitions = FreezeLock::new(Definitions::new(stable_crate_id)); let definitions = FreezeLock::new(Definitions::new(stable_crate_id));
@ -189,16 +183,16 @@ impl<'tcx> Queries<'tcx> {
pub fn ongoing_codegen(&'tcx self) -> Result<Box<dyn Any>> { pub fn ongoing_codegen(&'tcx self) -> Result<Box<dyn Any>> {
self.global_ctxt()?.enter(|tcx| { self.global_ctxt()?.enter(|tcx| {
// Don't do code generation if there were any errors // Don't do code generation if there were any errors
self.session().compile_status()?; self.compiler.sess.compile_status()?;
// If we have any delayed bugs, for example because we created TyKind::Error earlier, // If we have any delayed bugs, for example because we created TyKind::Error earlier,
// it's likely that codegen will only cause more ICEs, obscuring the original problem // it's likely that codegen will only cause more ICEs, obscuring the original problem
self.session().diagnostic().flush_delayed(); self.compiler.sess.diagnostic().flush_delayed();
// Hook for UI tests. // Hook for UI tests.
Self::check_for_rustc_errors_attr(tcx); Self::check_for_rustc_errors_attr(tcx);
Ok(passes::start_codegen(self.codegen_backend(), tcx)) Ok(passes::start_codegen(&*self.compiler.codegen_backend, tcx))
}) })
} }
@ -317,17 +311,16 @@ impl Compiler {
// after this point, they'll show up as "<unknown>" in self-profiling data. // after this point, they'll show up as "<unknown>" in self-profiling data.
{ {
let _prof_timer = let _prof_timer =
queries.session().prof.generic_activity("self_profile_alloc_query_strings"); queries.compiler.sess.prof.generic_activity("self_profile_alloc_query_strings");
gcx.enter(rustc_query_impl::alloc_self_profile_query_strings); gcx.enter(rustc_query_impl::alloc_self_profile_query_strings);
} }
self.session() self.sess.time("serialize_dep_graph", || gcx.enter(rustc_incremental::save_dep_graph));
.time("serialize_dep_graph", || gcx.enter(rustc_incremental::save_dep_graph));
} }
// The timer's lifetime spans the dropping of `queries`, which contains // The timer's lifetime spans the dropping of `queries`, which contains
// the global context. // the global context.
_timer = Some(self.session().timer("free_global_ctxt")); _timer = Some(self.sess.timer("free_global_ctxt"));
ret ret
} }

View file

@ -127,17 +127,17 @@ pub(crate) fn run(options: RustdocOptions) -> Result<(), ErrorGuaranteed> {
options, options,
false, false,
opts, opts,
Some(compiler.session().parse_sess.clone_source_map()), Some(compiler.sess.parse_sess.clone_source_map()),
None, None,
enable_per_target_ignores, enable_per_target_ignores,
); );
let mut hir_collector = HirCollector { let mut hir_collector = HirCollector {
sess: compiler.session(), sess: &compiler.sess,
collector: &mut collector, collector: &mut collector,
map: tcx.hir(), map: tcx.hir(),
codes: ErrorCodes::from( codes: ErrorCodes::from(
compiler.session().opts.unstable_features.is_nightly_build(), compiler.sess.opts.unstable_features.is_nightly_build(),
), ),
tcx, tcx,
}; };
@ -150,7 +150,7 @@ pub(crate) fn run(options: RustdocOptions) -> Result<(), ErrorGuaranteed> {
collector collector
}); });
if compiler.session().diagnostic().has_errors_or_lint_errors().is_some() { if compiler.sess.diagnostic().has_errors_or_lint_errors().is_some() {
FatalError.raise(); FatalError.raise();
} }

View file

@ -794,7 +794,7 @@ fn main_args(
let config = core::create_config(options, &render_options, using_internal_features); let config = core::create_config(options, &render_options, using_internal_features);
interface::run_compiler(config, |compiler| { interface::run_compiler(config, |compiler| {
let sess = compiler.session(); let sess = &compiler.sess;
if sess.opts.describe_lints { if sess.opts.describe_lints {
rustc_driver::describe_lints(sess); rustc_driver::describe_lints(sess);

View file

@ -72,6 +72,6 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
let ongoing_codegen = queries.ongoing_codegen()?; let ongoing_codegen = queries.ongoing_codegen()?;
queries.linker(ongoing_codegen) queries.linker(ongoing_codegen)
}); });
linker.unwrap().link(compiler.session(), compiler.codegen_backend()).unwrap(); linker.unwrap().link(&compiler.sess, &*compiler.codegen_backend).unwrap();
}); });
} }

View file

@ -61,7 +61,7 @@ impl rustc_driver::Callbacks for CompilerCalls {
compiler: &Compiler, compiler: &Compiler,
queries: &'tcx Queries<'tcx>, queries: &'tcx Queries<'tcx>,
) -> Compilation { ) -> Compilation {
compiler.session().abort_if_errors(); compiler.sess.abort_if_errors();
queries.global_ctxt().unwrap().enter(|tcx| { queries.global_ctxt().unwrap().enter(|tcx| {
// Collect definition ids of MIR bodies. // Collect definition ids of MIR bodies.
let hir = tcx.hir(); let hir = tcx.hir();