Skip to content

Commit

Permalink
Rollup merge of #134130 - bjorn3:prepare_driver_query_removal, r=oli-obk
Browse files Browse the repository at this point in the history
Stop using driver queries in the public API

Follow up to #132410 and #133567

The next PR will completely get rid of driver queries. That PR will also contains some non-trivial refactorings enabled by no longer needing to support entering TyCtxt multiple times after it is constructed. The changes in the current PR have been split out to make it easier to review the api changes and to reduce the size of the next PR to review.

## Custom driver breaking change

The `after_crate_root_parsing` and `after_expansion` callbacks now accept `ast::Crate` and `TyCtxt` respectively rather than `Queries`. The only safe query in `Queries` to call inside these callbacks are `parse()` and `global_ctxt()` respectively which allows you to access the `ast::Crate` and `TyCtxt` either way. To fix your custom driver, replace the `queries: &'tcx Queries<'tcx>` argument with `crate_: ast::Crate` and `tcx: TyCtxt<'tcx>` respectively and for `after_expansion` remove your `queries.global_ctxt().unwrap().enter(|tcx| { ... })` call and only keep the contents of the closure.
  • Loading branch information
matthiaskrgr authored Dec 13, 2024
2 parents 97dc513 + 7e37943 commit 9f6b07e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 34 deletions.
34 changes: 16 additions & 18 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use rustc_errors::registry::Registry;
use rustc_errors::{ColorConfig, DiagCtxt, ErrCode, FatalError, PResult, markdown};
use rustc_feature::find_gated_cfg;
use rustc_interface::util::{self, get_codegen_backend};
use rustc_interface::{Linker, Queries, interface, passes};
use rustc_interface::{Linker, interface, passes};
use rustc_lint::unerased_lint_store;
use rustc_metadata::creader::MetadataLoader;
use rustc_metadata::locator;
Expand Down Expand Up @@ -158,13 +158,10 @@ pub trait Callbacks {
/// Called after parsing the crate root. Submodules are not yet parsed when
/// this callback is called. Return value instructs the compiler whether to
/// continue the compilation afterwards (defaults to `Compilation::Continue`)
#[deprecated = "This callback will likely be removed or stop giving access \
to the TyCtxt in the future. Use either the after_expansion \
or the after_analysis callback instead."]
fn after_crate_root_parsing<'tcx>(
fn after_crate_root_parsing(
&mut self,
_compiler: &interface::Compiler,
_queries: &'tcx Queries<'tcx>,
_queries: &ast::Crate,
) -> Compilation {
Compilation::Continue
}
Expand All @@ -173,7 +170,7 @@ pub trait Callbacks {
fn after_expansion<'tcx>(
&mut self,
_compiler: &interface::Compiler,
_queries: &'tcx Queries<'tcx>,
_tcx: TyCtxt<'tcx>,
) -> Compilation {
Compilation::Continue
}
Expand Down Expand Up @@ -416,27 +413,28 @@ fn run_compiler(
return early_exit();
}

#[allow(deprecated)]
if callbacks.after_crate_root_parsing(compiler, queries) == Compilation::Stop {
if callbacks.after_crate_root_parsing(compiler, &*queries.parse().borrow())
== Compilation::Stop
{
return early_exit();
}

if sess.opts.unstable_opts.parse_crate_root_only {
return early_exit();
}

// Make sure name resolution and macro expansion is run.
queries.global_ctxt().enter(|tcx| tcx.resolver_for_lowering());
queries.global_ctxt().enter(|tcx| {
// Make sure name resolution and macro expansion is run.
let _ = tcx.resolver_for_lowering();

if let Some(metrics_dir) = &sess.opts.unstable_opts.metrics_dir {
queries.global_ctxt().enter(|tcxt| dump_feature_usage_metrics(tcxt, metrics_dir));
}
if let Some(metrics_dir) = &sess.opts.unstable_opts.metrics_dir {
dump_feature_usage_metrics(tcx, metrics_dir);
}

if callbacks.after_expansion(compiler, queries) == Compilation::Stop {
return early_exit();
}
if callbacks.after_expansion(compiler, tcx) == Compilation::Stop {
return early_exit();
}

queries.global_ctxt().enter(|tcx| {
passes::write_dep_info(tcx);

if sess.opts.output_types.contains_key(&OutputType::DepInfo)
Expand Down
12 changes: 12 additions & 0 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,18 @@ pub(crate) fn start_codegen<'tcx>(
}
}

// This must run after monomorphization so that all generic types
// have been instantiated.
if tcx.sess.opts.unstable_opts.print_type_sizes {
tcx.sess.code_stats.print_type_sizes();
}

if tcx.sess.opts.unstable_opts.print_vtable_sizes {
let crate_name = tcx.crate_name(LOCAL_CRATE);

tcx.sess.code_stats.print_vtable_sizes(crate_name);
}

codegen
}

Expand Down
12 changes: 0 additions & 12 deletions compiler/rustc_interface/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,6 @@ impl Linker {
) -> Linker {
let ongoing_codegen = passes::start_codegen(codegen_backend, tcx);

// This must run after monomorphization so that all generic types
// have been instantiated.
if tcx.sess.opts.unstable_opts.print_type_sizes {
tcx.sess.code_stats.print_type_sizes();
}

if tcx.sess.opts.unstable_opts.print_vtable_sizes {
let crate_name = tcx.crate_name(LOCAL_CRATE);

tcx.sess.code_stats.print_vtable_sizes(crate_name);
}

Linker {
dep_graph: tcx.dep_graph.clone(),
output_filenames: Arc::clone(tcx.output_filenames(())),
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_middle/src/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ pub struct DynamicQuery<'tcx, C: QueryCache> {
pub format_value: fn(&C::Value) -> String,
}

pub struct QuerySystemFns<'tcx> {
pub struct QuerySystemFns {
pub engine: QueryEngine,
pub local_providers: Providers,
pub extern_providers: ExternProviders,
pub encode_query_results: fn(
pub encode_query_results: for<'tcx> fn(
tcx: TyCtxt<'tcx>,
encoder: &mut CacheEncoder<'_, 'tcx>,
query_result_index: &mut EncodedDepNodeIndex,
),
pub try_mark_green: fn(tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool,
pub try_mark_green: for<'tcx> fn(tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool,
}

pub struct QuerySystem<'tcx> {
Expand All @@ -68,7 +68,7 @@ pub struct QuerySystem<'tcx> {
/// This is `None` if we are not incremental compilation mode
pub on_disk_cache: Option<OnDiskCache>,

pub fns: QuerySystemFns<'tcx>,
pub fns: QuerySystemFns,

pub jobs: AtomicU64,
}
Expand Down

0 comments on commit 9f6b07e

Please sign in to comment.