Skip to content

Commit 3533676

Browse files
committed
Pass TyCtxt instead of Queries to the after_analysis callbacks
There is no other query that may need to be called at that point anyway.
1 parent 717287f commit 3533676

File tree

4 files changed

+96
-110
lines changed

4 files changed

+96
-110
lines changed

compiler/rustc_driver_impl/src/lib.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use rustc_interface::{Linker, Queries, interface, passes};
4949
use rustc_lint::unerased_lint_store;
5050
use rustc_metadata::creader::MetadataLoader;
5151
use rustc_metadata::locator;
52+
use rustc_middle::ty::TyCtxt;
5253
use rustc_parse::{new_parser_from_file, new_parser_from_source_str, unwrap_or_emit_fatal};
5354
use rustc_session::config::{
5455
CG_OPTIONS, ErrorOutputType, Input, OutFileName, OutputType, UnstableOptions, Z_OPTIONS,
@@ -177,7 +178,7 @@ pub trait Callbacks {
177178
fn after_analysis<'tcx>(
178179
&mut self,
179180
_compiler: &interface::Compiler,
180-
_queries: &'tcx Queries<'tcx>,
181+
_tcx: TyCtxt<'tcx>,
181182
) -> Compilation {
182183
Compilation::Continue
183184
}
@@ -435,13 +436,11 @@ fn run_compiler(
435436
}
436437

437438
tcx.analysis(())?;
438-
})?;
439439

440-
if callbacks.after_analysis(compiler, queries) == Compilation::Stop {
441-
return early_exit();
442-
}
440+
if callbacks.after_analysis(compiler, tcx) == Compilation::Stop {
441+
return early_exit();
442+
}
443443

444-
queries.global_ctxt()?.enter(|tcx| {
445444
Ok(Some(Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend)?))
446445
})
447446
})?;

compiler/rustc_smir/src/rustc_internal/mod.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -373,23 +373,21 @@ macro_rules! run_driver {
373373
fn after_analysis<'tcx>(
374374
&mut self,
375375
_compiler: &interface::Compiler,
376-
queries: &'tcx Queries<'tcx>,
376+
tcx: TyCtxt<'tcx>,
377377
) -> Compilation {
378-
queries.global_ctxt().unwrap().enter(|tcx| {
379-
if let Some(callback) = self.callback.take() {
380-
rustc_internal::run(tcx, || {
381-
self.result = Some(callback($(optional!($with_tcx tcx))?));
382-
})
383-
.unwrap();
384-
if self.result.as_ref().is_some_and(|val| val.is_continue()) {
385-
Compilation::Continue
386-
} else {
387-
Compilation::Stop
388-
}
389-
} else {
378+
if let Some(callback) = self.callback.take() {
379+
rustc_internal::run(tcx, || {
380+
self.result = Some(callback($(optional!($with_tcx tcx))?));
381+
})
382+
.unwrap();
383+
if self.result.as_ref().is_some_and(|val| val.is_continue()) {
390384
Compilation::Continue
385+
} else {
386+
Compilation::Stop
391387
}
392-
})
388+
} else {
389+
Compilation::Continue
390+
}
393391
}
394392
}
395393

src/tools/miri/src/bin/miri.rs

+38-44
Original file line numberDiff line numberDiff line change
@@ -73,51 +73,47 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
7373
fn after_analysis<'tcx>(
7474
&mut self,
7575
_: &rustc_interface::interface::Compiler,
76-
queries: &'tcx rustc_interface::Queries<'tcx>,
76+
tcx: TyCtxt<'tcx>,
7777
) -> Compilation {
78-
queries.global_ctxt().unwrap().enter(|tcx| {
79-
if tcx.sess.dcx().has_errors_or_delayed_bugs().is_some() {
80-
tcx.dcx().fatal("miri cannot be run on programs that fail compilation");
81-
}
78+
if tcx.sess.dcx().has_errors_or_delayed_bugs().is_some() {
79+
tcx.dcx().fatal("miri cannot be run on programs that fail compilation");
80+
}
8281

83-
let early_dcx = EarlyDiagCtxt::new(tcx.sess.opts.error_format);
84-
init_late_loggers(&early_dcx, tcx);
85-
if !tcx.crate_types().contains(&CrateType::Executable) {
86-
tcx.dcx().fatal("miri only makes sense on bin crates");
87-
}
82+
let early_dcx = EarlyDiagCtxt::new(tcx.sess.opts.error_format);
83+
init_late_loggers(&early_dcx, tcx);
84+
if !tcx.crate_types().contains(&CrateType::Executable) {
85+
tcx.dcx().fatal("miri only makes sense on bin crates");
86+
}
8887

89-
let (entry_def_id, entry_type) = entry_fn(tcx);
90-
let mut config = self.miri_config.clone();
88+
let (entry_def_id, entry_type) = entry_fn(tcx);
89+
let mut config = self.miri_config.clone();
9190

92-
// Add filename to `miri` arguments.
93-
config.args.insert(0, tcx.sess.io.input.filestem().to_string());
91+
// Add filename to `miri` arguments.
92+
config.args.insert(0, tcx.sess.io.input.filestem().to_string());
9493

95-
// Adjust working directory for interpretation.
96-
if let Some(cwd) = env::var_os("MIRI_CWD") {
97-
env::set_current_dir(cwd).unwrap();
98-
}
94+
// Adjust working directory for interpretation.
95+
if let Some(cwd) = env::var_os("MIRI_CWD") {
96+
env::set_current_dir(cwd).unwrap();
97+
}
9998

100-
if tcx.sess.opts.optimize != OptLevel::No {
101-
tcx.dcx().warn("Miri does not support optimizations: the opt-level is ignored. The only effect \
99+
if tcx.sess.opts.optimize != OptLevel::No {
100+
tcx.dcx().warn("Miri does not support optimizations: the opt-level is ignored. The only effect \
102101
of selecting a Cargo profile that enables optimizations (such as --release) is to apply \
103102
its remaining settings, such as whether debug assertions and overflow checks are enabled.");
104-
}
105-
if tcx.sess.mir_opt_level() > 0 {
106-
tcx.dcx().warn("You have explicitly enabled MIR optimizations, overriding Miri's default \
103+
}
104+
if tcx.sess.mir_opt_level() > 0 {
105+
tcx.dcx().warn("You have explicitly enabled MIR optimizations, overriding Miri's default \
107106
which is to completely disable them. Any optimizations may hide UB that Miri would \
108107
otherwise detect, and it is not necessarily possible to predict what kind of UB will \
109108
be missed. If you are enabling optimizations to make Miri run faster, we advise using \
110109
cfg(miri) to shrink your workload instead. The performance benefit of enabling MIR \
111110
optimizations is usually marginal at best.");
112-
}
111+
}
113112

114-
if let Some(return_code) = miri::eval_entry(tcx, entry_def_id, entry_type, config) {
115-
std::process::exit(
116-
i32::try_from(return_code).expect("Return value was too large!"),
117-
);
118-
}
119-
tcx.dcx().abort_if_errors();
120-
});
113+
if let Some(return_code) = miri::eval_entry(tcx, entry_def_id, entry_type, config) {
114+
std::process::exit(i32::try_from(return_code).expect("Return value was too large!"));
115+
}
116+
tcx.dcx().abort_if_errors();
121117

122118
Compilation::Stop
123119
}
@@ -193,20 +189,18 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
193189
fn after_analysis<'tcx>(
194190
&mut self,
195191
_: &rustc_interface::interface::Compiler,
196-
queries: &'tcx rustc_interface::Queries<'tcx>,
192+
tcx: TyCtxt<'tcx>,
197193
) -> Compilation {
198-
queries.global_ctxt().unwrap().enter(|tcx| {
199-
if self.target_crate {
200-
// cargo-miri has patched the compiler flags to make these into check-only builds,
201-
// but we are still emulating regular rustc builds, which would perform post-mono
202-
// const-eval during collection. So let's also do that here, even if we might be
203-
// running with `--emit=metadata`. In particular this is needed to make
204-
// `compile_fail` doc tests trigger post-mono errors.
205-
// In general `collect_and_partition_mono_items` is not safe to call in check-only
206-
// builds, but we are setting `-Zalways-encode-mir` which avoids those issues.
207-
let _ = tcx.collect_and_partition_mono_items(());
208-
}
209-
});
194+
if self.target_crate {
195+
// cargo-miri has patched the compiler flags to make these into check-only builds,
196+
// but we are still emulating regular rustc builds, which would perform post-mono
197+
// const-eval during collection. So let's also do that here, even if we might be
198+
// running with `--emit=metadata`. In particular this is needed to make
199+
// `compile_fail` doc tests trigger post-mono errors.
200+
// In general `collect_and_partition_mono_items` is not safe to call in check-only
201+
// builds, but we are setting `-Zalways-encode-mir` which avoids those issues.
202+
let _ = tcx.collect_and_partition_mono_items(());
203+
}
210204
Compilation::Continue
211205
}
212206
}

tests/ui-fulldeps/obtain-borrowck.rs

+41-46
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ extern crate rustc_interface;
2525
extern crate rustc_middle;
2626
extern crate rustc_session;
2727

28+
use std::cell::RefCell;
29+
use std::collections::HashMap;
30+
use std::thread_local;
31+
2832
use rustc_borrowck::consumers::{self, BodyWithBorrowckFacts, ConsumerOptions};
2933
use rustc_driver::Compilation;
3034
use rustc_hir::def::DefKind;
@@ -35,9 +39,6 @@ use rustc_middle::query::queries::mir_borrowck::ProvidedValue;
3539
use rustc_middle::ty::TyCtxt;
3640
use rustc_middle::util::Providers;
3741
use rustc_session::Session;
38-
use std::cell::RefCell;
39-
use std::collections::HashMap;
40-
use std::thread_local;
4142

4243
fn main() {
4344
let exit_code = rustc_driver::catch_with_exit_code(move || {
@@ -63,55 +64,49 @@ impl rustc_driver::Callbacks for CompilerCalls {
6364

6465
// In this callback we trigger borrow checking of all functions and obtain
6566
// the result.
66-
fn after_analysis<'tcx>(
67-
&mut self,
68-
compiler: &Compiler,
69-
queries: &'tcx Queries<'tcx>,
70-
) -> Compilation {
71-
compiler.sess.dcx().abort_if_errors();
72-
queries.global_ctxt().unwrap().enter(|tcx| {
73-
// Collect definition ids of MIR bodies.
74-
let hir = tcx.hir();
75-
let mut bodies = Vec::new();
76-
77-
let crate_items = tcx.hir_crate_items(());
78-
for id in crate_items.free_items() {
79-
if matches!(tcx.def_kind(id.owner_id), DefKind::Fn) {
80-
bodies.push(id.owner_id);
81-
}
67+
fn after_analysis<'tcx>(&mut self, _compiler: &Compiler, tcx: TyCtxt<'tcx>) -> Compilation {
68+
tcx.sess.dcx().abort_if_errors();
69+
// Collect definition ids of MIR bodies.
70+
let hir = tcx.hir();
71+
let mut bodies = Vec::new();
72+
73+
let crate_items = tcx.hir_crate_items(());
74+
for id in crate_items.items() {
75+
if matches!(tcx.def_kind(id.owner_id), DefKind::Fn) {
76+
bodies.push(id.owner_id);
8277
}
83-
84-
for id in crate_items.trait_items() {
85-
if matches!(tcx.def_kind(id.owner_id), DefKind::AssocFn) {
86-
let trait_item = hir.trait_item(id);
87-
if let rustc_hir::TraitItemKind::Fn(_, trait_fn) = &trait_item.kind {
88-
if let rustc_hir::TraitFn::Provided(_) = trait_fn {
89-
bodies.push(trait_item.owner_id);
90-
}
78+
}
79+
80+
for id in crate_items.trait_items() {
81+
if matches!(tcx.def_kind(id.owner_id), DefKind::AssocFn) {
82+
let trait_item = hir.trait_item(id);
83+
if let rustc_hir::TraitItemKind::Fn(_, trait_fn) = &trait_item.kind {
84+
if let rustc_hir::TraitFn::Provided(_) = trait_fn {
85+
bodies.push(trait_item.owner_id);
9186
}
9287
}
9388
}
89+
}
9490

95-
for id in crate_items.impl_items() {
96-
if matches!(tcx.def_kind(id.owner_id), DefKind::AssocFn) {
97-
bodies.push(id.owner_id);
98-
}
99-
}
100-
101-
// Trigger borrow checking of all bodies.
102-
for def_id in bodies {
103-
let _ = tcx.optimized_mir(def_id);
104-
}
105-
106-
// See what bodies were borrow checked.
107-
let mut bodies = get_bodies(tcx);
108-
bodies.sort_by(|(def_id1, _), (def_id2, _)| def_id1.cmp(def_id2));
109-
println!("Bodies retrieved for:");
110-
for (def_id, body) in bodies {
111-
println!("{}", def_id);
112-
assert!(body.input_facts.unwrap().cfg_edge.len() > 0);
91+
for id in crate_items.impl_items() {
92+
if matches!(tcx.def_kind(id.owner_id), DefKind::AssocFn) {
93+
bodies.push(id.owner_id);
11394
}
114-
});
95+
}
96+
97+
// Trigger borrow checking of all bodies.
98+
for def_id in bodies {
99+
let _ = tcx.optimized_mir(def_id);
100+
}
101+
102+
// See what bodies were borrow checked.
103+
let mut bodies = get_bodies(tcx);
104+
bodies.sort_by(|(def_id1, _), (def_id2, _)| def_id1.cmp(def_id2));
105+
println!("Bodies retrieved for:");
106+
for (def_id, body) in bodies {
107+
println!("{}", def_id);
108+
assert!(body.input_facts.unwrap().cfg_edge.len() > 0);
109+
}
115110

116111
Compilation::Continue
117112
}

0 commit comments

Comments
 (0)