Skip to content

Commit 9f6b07e

Browse files
authored
Rollup merge of rust-lang#134130 - bjorn3:prepare_driver_query_removal, r=oli-obk
Stop using driver queries in the public API Follow up to rust-lang#132410 and rust-lang#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.
2 parents 97dc513 + 7e37943 commit 9f6b07e

File tree

4 files changed

+32
-34
lines changed

4 files changed

+32
-34
lines changed

Diff for: compiler/rustc_driver_impl/src/lib.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use rustc_errors::registry::Registry;
4545
use rustc_errors::{ColorConfig, DiagCtxt, ErrCode, FatalError, PResult, markdown};
4646
use rustc_feature::find_gated_cfg;
4747
use rustc_interface::util::{self, get_codegen_backend};
48-
use rustc_interface::{Linker, Queries, interface, passes};
48+
use rustc_interface::{Linker, interface, passes};
4949
use rustc_lint::unerased_lint_store;
5050
use rustc_metadata::creader::MetadataLoader;
5151
use rustc_metadata::locator;
@@ -158,13 +158,10 @@ pub trait Callbacks {
158158
/// Called after parsing the crate root. Submodules are not yet parsed when
159159
/// this callback is called. Return value instructs the compiler whether to
160160
/// continue the compilation afterwards (defaults to `Compilation::Continue`)
161-
#[deprecated = "This callback will likely be removed or stop giving access \
162-
to the TyCtxt in the future. Use either the after_expansion \
163-
or the after_analysis callback instead."]
164-
fn after_crate_root_parsing<'tcx>(
161+
fn after_crate_root_parsing(
165162
&mut self,
166163
_compiler: &interface::Compiler,
167-
_queries: &'tcx Queries<'tcx>,
164+
_queries: &ast::Crate,
168165
) -> Compilation {
169166
Compilation::Continue
170167
}
@@ -173,7 +170,7 @@ pub trait Callbacks {
173170
fn after_expansion<'tcx>(
174171
&mut self,
175172
_compiler: &interface::Compiler,
176-
_queries: &'tcx Queries<'tcx>,
173+
_tcx: TyCtxt<'tcx>,
177174
) -> Compilation {
178175
Compilation::Continue
179176
}
@@ -416,27 +413,28 @@ fn run_compiler(
416413
return early_exit();
417414
}
418415

419-
#[allow(deprecated)]
420-
if callbacks.after_crate_root_parsing(compiler, queries) == Compilation::Stop {
416+
if callbacks.after_crate_root_parsing(compiler, &*queries.parse().borrow())
417+
== Compilation::Stop
418+
{
421419
return early_exit();
422420
}
423421

424422
if sess.opts.unstable_opts.parse_crate_root_only {
425423
return early_exit();
426424
}
427425

428-
// Make sure name resolution and macro expansion is run.
429-
queries.global_ctxt().enter(|tcx| tcx.resolver_for_lowering());
426+
queries.global_ctxt().enter(|tcx| {
427+
// Make sure name resolution and macro expansion is run.
428+
let _ = tcx.resolver_for_lowering();
430429

431-
if let Some(metrics_dir) = &sess.opts.unstable_opts.metrics_dir {
432-
queries.global_ctxt().enter(|tcxt| dump_feature_usage_metrics(tcxt, metrics_dir));
433-
}
430+
if let Some(metrics_dir) = &sess.opts.unstable_opts.metrics_dir {
431+
dump_feature_usage_metrics(tcx, metrics_dir);
432+
}
434433

435-
if callbacks.after_expansion(compiler, queries) == Compilation::Stop {
436-
return early_exit();
437-
}
434+
if callbacks.after_expansion(compiler, tcx) == Compilation::Stop {
435+
return early_exit();
436+
}
438437

439-
queries.global_ctxt().enter(|tcx| {
440438
passes::write_dep_info(tcx);
441439

442440
if sess.opts.output_types.contains_key(&OutputType::DepInfo)

Diff for: compiler/rustc_interface/src/passes.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,18 @@ pub(crate) fn start_codegen<'tcx>(
11251125
}
11261126
}
11271127

1128+
// This must run after monomorphization so that all generic types
1129+
// have been instantiated.
1130+
if tcx.sess.opts.unstable_opts.print_type_sizes {
1131+
tcx.sess.code_stats.print_type_sizes();
1132+
}
1133+
1134+
if tcx.sess.opts.unstable_opts.print_vtable_sizes {
1135+
let crate_name = tcx.crate_name(LOCAL_CRATE);
1136+
1137+
tcx.sess.code_stats.print_vtable_sizes(crate_name);
1138+
}
1139+
11281140
codegen
11291141
}
11301142

Diff for: compiler/rustc_interface/src/queries.rs

-12
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,6 @@ impl Linker {
127127
) -> Linker {
128128
let ongoing_codegen = passes::start_codegen(codegen_backend, tcx);
129129

130-
// This must run after monomorphization so that all generic types
131-
// have been instantiated.
132-
if tcx.sess.opts.unstable_opts.print_type_sizes {
133-
tcx.sess.code_stats.print_type_sizes();
134-
}
135-
136-
if tcx.sess.opts.unstable_opts.print_vtable_sizes {
137-
let crate_name = tcx.crate_name(LOCAL_CRATE);
138-
139-
tcx.sess.code_stats.print_vtable_sizes(crate_name);
140-
}
141-
142130
Linker {
143131
dep_graph: tcx.dep_graph.clone(),
144132
output_filenames: Arc::clone(tcx.output_filenames(())),

Diff for: compiler/rustc_middle/src/query/plumbing.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ pub struct DynamicQuery<'tcx, C: QueryCache> {
4444
pub format_value: fn(&C::Value) -> String,
4545
}
4646

47-
pub struct QuerySystemFns<'tcx> {
47+
pub struct QuerySystemFns {
4848
pub engine: QueryEngine,
4949
pub local_providers: Providers,
5050
pub extern_providers: ExternProviders,
51-
pub encode_query_results: fn(
51+
pub encode_query_results: for<'tcx> fn(
5252
tcx: TyCtxt<'tcx>,
5353
encoder: &mut CacheEncoder<'_, 'tcx>,
5454
query_result_index: &mut EncodedDepNodeIndex,
5555
),
56-
pub try_mark_green: fn(tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool,
56+
pub try_mark_green: for<'tcx> fn(tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool,
5757
}
5858

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

71-
pub fns: QuerySystemFns<'tcx>,
71+
pub fns: QuerySystemFns,
7272

7373
pub jobs: AtomicU64,
7474
}

0 commit comments

Comments
 (0)