Skip to content

Commit

Permalink
Pass TyCtxt instead of Queries to the after_analysis callbacks
Browse files Browse the repository at this point in the history
There is no other query that may need to be called at that point anyway.
  • Loading branch information
bjorn3 committed Nov 9, 2024
1 parent 1eece74 commit 3b02a33
Show file tree
Hide file tree
Showing 22 changed files with 116 additions and 111 deletions.
11 changes: 5 additions & 6 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use rustc_interface::{Linker, Queries, interface, passes};
use rustc_lint::unerased_lint_store;
use rustc_metadata::creader::MetadataLoader;
use rustc_metadata::locator;
use rustc_middle::ty::TyCtxt;
use rustc_parse::{new_parser_from_file, new_parser_from_source_str, unwrap_or_emit_fatal};
use rustc_session::config::{
CG_OPTIONS, ErrorOutputType, Input, OutFileName, OutputType, UnstableOptions, Z_OPTIONS,
Expand Down Expand Up @@ -179,7 +180,7 @@ pub trait Callbacks {
fn after_analysis<'tcx>(
&mut self,
_compiler: &interface::Compiler,
_queries: &'tcx Queries<'tcx>,
_tcx: TyCtxt<'tcx>,
) -> Compilation {
Compilation::Continue
}
Expand Down Expand Up @@ -437,13 +438,11 @@ fn run_compiler(
}

tcx.analysis(())?;
})?;

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

queries.global_ctxt()?.enter(|tcx| {
Ok(Some(Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend)?))
})
})?;
Expand Down
27 changes: 13 additions & 14 deletions compiler/rustc_smir/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ macro_rules! optional {
macro_rules! run_driver {
($args:expr, $callback:expr $(, $with_tcx:ident)?) => {{
use rustc_driver::{Callbacks, Compilation, RunCompiler};
use rustc_middle::ty::TyCtxt;
use rustc_interface::{interface, Queries};
use stable_mir::CompilerError;
use std::ops::ControlFlow;
Expand Down Expand Up @@ -373,23 +374,21 @@ macro_rules! run_driver {
fn after_analysis<'tcx>(
&mut self,
_compiler: &interface::Compiler,
queries: &'tcx Queries<'tcx>,
tcx: TyCtxt<'tcx>,
) -> Compilation {
queries.global_ctxt().unwrap().enter(|tcx| {
if let Some(callback) = self.callback.take() {
rustc_internal::run(tcx, || {
self.result = Some(callback($(optional!($with_tcx tcx))?));
})
.unwrap();
if self.result.as_ref().is_some_and(|val| val.is_continue()) {
Compilation::Continue
} else {
Compilation::Stop
}
} else {
if let Some(callback) = self.callback.take() {
rustc_internal::run(tcx, || {
self.result = Some(callback($(optional!($with_tcx tcx))?));
})
.unwrap();
if self.result.as_ref().is_some_and(|val| val.is_continue()) {
Compilation::Continue
} else {
Compilation::Stop
}
})
} else {
Compilation::Continue
}
}
}

Expand Down
82 changes: 38 additions & 44 deletions src/tools/miri/src/bin/miri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,51 +73,47 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
fn after_analysis<'tcx>(
&mut self,
_: &rustc_interface::interface::Compiler,
queries: &'tcx rustc_interface::Queries<'tcx>,
tcx: TyCtxt<'tcx>,
) -> Compilation {
queries.global_ctxt().unwrap().enter(|tcx| {
if tcx.sess.dcx().has_errors_or_delayed_bugs().is_some() {
tcx.dcx().fatal("miri cannot be run on programs that fail compilation");
}
if tcx.sess.dcx().has_errors_or_delayed_bugs().is_some() {
tcx.dcx().fatal("miri cannot be run on programs that fail compilation");
}

let early_dcx = EarlyDiagCtxt::new(tcx.sess.opts.error_format);
init_late_loggers(&early_dcx, tcx);
if !tcx.crate_types().contains(&CrateType::Executable) {
tcx.dcx().fatal("miri only makes sense on bin crates");
}
let early_dcx = EarlyDiagCtxt::new(tcx.sess.opts.error_format);
init_late_loggers(&early_dcx, tcx);
if !tcx.crate_types().contains(&CrateType::Executable) {
tcx.dcx().fatal("miri only makes sense on bin crates");
}

let (entry_def_id, entry_type) = entry_fn(tcx);
let mut config = self.miri_config.clone();
let (entry_def_id, entry_type) = entry_fn(tcx);
let mut config = self.miri_config.clone();

// Add filename to `miri` arguments.
config.args.insert(0, tcx.sess.io.input.filestem().to_string());
// Add filename to `miri` arguments.
config.args.insert(0, tcx.sess.io.input.filestem().to_string());

// Adjust working directory for interpretation.
if let Some(cwd) = env::var_os("MIRI_CWD") {
env::set_current_dir(cwd).unwrap();
}
// Adjust working directory for interpretation.
if let Some(cwd) = env::var_os("MIRI_CWD") {
env::set_current_dir(cwd).unwrap();
}

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

if let Some(return_code) = miri::eval_entry(tcx, entry_def_id, entry_type, config) {
std::process::exit(
i32::try_from(return_code).expect("Return value was too large!"),
);
}
tcx.dcx().abort_if_errors();
});
if let Some(return_code) = miri::eval_entry(tcx, entry_def_id, entry_type, config) {
std::process::exit(i32::try_from(return_code).expect("Return value was too large!"));
}
tcx.dcx().abort_if_errors();

Compilation::Stop
}
Expand Down Expand Up @@ -193,20 +189,18 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
fn after_analysis<'tcx>(
&mut self,
_: &rustc_interface::interface::Compiler,
queries: &'tcx rustc_interface::Queries<'tcx>,
tcx: TyCtxt<'tcx>,
) -> Compilation {
queries.global_ctxt().unwrap().enter(|tcx| {
if self.target_crate {
// cargo-miri has patched the compiler flags to make these into check-only builds,
// but we are still emulating regular rustc builds, which would perform post-mono
// const-eval during collection. So let's also do that here, even if we might be
// running with `--emit=metadata`. In particular this is needed to make
// `compile_fail` doc tests trigger post-mono errors.
// In general `collect_and_partition_mono_items` is not safe to call in check-only
// builds, but we are setting `-Zalways-encode-mir` which avoids those issues.
let _ = tcx.collect_and_partition_mono_items(());
}
});
if self.target_crate {
// cargo-miri has patched the compiler flags to make these into check-only builds,
// but we are still emulating regular rustc builds, which would perform post-mono
// const-eval during collection. So let's also do that here, even if we might be
// running with `--emit=metadata`. In particular this is needed to make
// `compile_fail` doc tests trigger post-mono errors.
// In general `collect_and_partition_mono_items` is not safe to call in check-only
// builds, but we are setting `-Zalways-encode-mir` which avoids those issues.
let _ = tcx.collect_and_partition_mono_items(());
}
Compilation::Continue
}
}
Expand Down
89 changes: 42 additions & 47 deletions tests/ui-fulldeps/obtain-borrowck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,20 @@ extern crate rustc_interface;
extern crate rustc_middle;
extern crate rustc_session;

use std::cell::RefCell;
use std::collections::HashMap;
use std::thread_local;

use rustc_borrowck::consumers::{self, BodyWithBorrowckFacts, ConsumerOptions};
use rustc_driver::Compilation;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::LocalDefId;
use rustc_interface::Config;
use rustc_interface::interface::Compiler;
use rustc_interface::{Config, Queries};
use rustc_middle::query::queries::mir_borrowck::ProvidedValue;
use rustc_middle::ty::TyCtxt;
use rustc_middle::util::Providers;
use rustc_session::Session;
use std::cell::RefCell;
use std::collections::HashMap;
use std::thread_local;

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

// In this callback we trigger borrow checking of all functions and obtain
// the result.
fn after_analysis<'tcx>(
&mut self,
compiler: &Compiler,
queries: &'tcx Queries<'tcx>,
) -> Compilation {
compiler.sess.dcx().abort_if_errors();
queries.global_ctxt().unwrap().enter(|tcx| {
// Collect definition ids of MIR bodies.
let hir = tcx.hir();
let mut bodies = Vec::new();

let crate_items = tcx.hir_crate_items(());
for id in crate_items.free_items() {
if matches!(tcx.def_kind(id.owner_id), DefKind::Fn) {
bodies.push(id.owner_id);
}
fn after_analysis<'tcx>(&mut self, _compiler: &Compiler, tcx: TyCtxt<'tcx>) -> Compilation {
tcx.sess.dcx().abort_if_errors();
// Collect definition ids of MIR bodies.
let hir = tcx.hir();
let mut bodies = Vec::new();

let crate_items = tcx.hir_crate_items(());
for id in crate_items.free_items() {
if matches!(tcx.def_kind(id.owner_id), DefKind::Fn) {
bodies.push(id.owner_id);
}

for id in crate_items.trait_items() {
if matches!(tcx.def_kind(id.owner_id), DefKind::AssocFn) {
let trait_item = hir.trait_item(id);
if let rustc_hir::TraitItemKind::Fn(_, trait_fn) = &trait_item.kind {
if let rustc_hir::TraitFn::Provided(_) = trait_fn {
bodies.push(trait_item.owner_id);
}
}

for id in crate_items.trait_items() {
if matches!(tcx.def_kind(id.owner_id), DefKind::AssocFn) {
let trait_item = hir.trait_item(id);
if let rustc_hir::TraitItemKind::Fn(_, trait_fn) = &trait_item.kind {
if let rustc_hir::TraitFn::Provided(_) = trait_fn {
bodies.push(trait_item.owner_id);
}
}
}
}

for id in crate_items.impl_items() {
if matches!(tcx.def_kind(id.owner_id), DefKind::AssocFn) {
bodies.push(id.owner_id);
}
}

// Trigger borrow checking of all bodies.
for def_id in bodies {
let _ = tcx.optimized_mir(def_id);
}

// See what bodies were borrow checked.
let mut bodies = get_bodies(tcx);
bodies.sort_by(|(def_id1, _), (def_id2, _)| def_id1.cmp(def_id2));
println!("Bodies retrieved for:");
for (def_id, body) in bodies {
println!("{}", def_id);
assert!(body.input_facts.unwrap().cfg_edge.len() > 0);
for id in crate_items.impl_items() {
if matches!(tcx.def_kind(id.owner_id), DefKind::AssocFn) {
bodies.push(id.owner_id);
}
});
}

// Trigger borrow checking of all bodies.
for def_id in bodies {
let _ = tcx.optimized_mir(def_id);
}

// See what bodies were borrow checked.
let mut bodies = get_bodies(tcx);
bodies.sort_by(|(def_id1, _), (def_id2, _)| def_id1.cmp(def_id2));
println!("Bodies retrieved for:");
for (def_id, body) in bodies {
println!("{}", def_id);
assert!(body.input_facts.unwrap().cfg_edge.len() > 0);
}

Compilation::Continue
}
Expand Down
1 change: 1 addition & 0 deletions tests/ui-fulldeps/stable-mir/check_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#![feature(ascii_char, ascii_char_variants)]

extern crate rustc_hir;
extern crate rustc_middle;
#[macro_use]
extern crate rustc_smir;
extern crate rustc_driver;
Expand Down
1 change: 1 addition & 0 deletions tests/ui-fulldeps/stable-mir/check_allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#![feature(ascii_char, ascii_char_variants)]

extern crate rustc_hir;
extern crate rustc_middle;
#[macro_use]
extern crate rustc_smir;
extern crate rustc_driver;
Expand Down
1 change: 1 addition & 0 deletions tests/ui-fulldeps/stable-mir/check_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#![feature(rustc_private)]

extern crate rustc_hir;
extern crate rustc_middle;
#[macro_use]
extern crate rustc_smir;
extern crate rustc_driver;
Expand Down
1 change: 1 addition & 0 deletions tests/ui-fulldeps/stable-mir/check_binop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#![feature(rustc_private)]

extern crate rustc_hir;
extern crate rustc_middle;
#[macro_use]
extern crate rustc_smir;
extern crate rustc_driver;
Expand Down
1 change: 1 addition & 0 deletions tests/ui-fulldeps/stable-mir/check_crate_defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#![feature(assert_matches)]

extern crate rustc_hir;
extern crate rustc_middle;
#[macro_use]
extern crate rustc_smir;
extern crate rustc_driver;
Expand Down
1 change: 1 addition & 0 deletions tests/ui-fulldeps/stable-mir/check_def_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#![feature(rustc_private)]
#![feature(assert_matches)]

extern crate rustc_middle;
#[macro_use]
extern crate rustc_smir;
extern crate rustc_driver;
Expand Down
1 change: 1 addition & 0 deletions tests/ui-fulldeps/stable-mir/check_defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#![feature(rustc_private)]
#![feature(assert_matches)]

extern crate rustc_middle;
#[macro_use]
extern crate rustc_smir;
extern crate rustc_driver;
Expand Down
1 change: 1 addition & 0 deletions tests/ui-fulldeps/stable-mir/check_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#![feature(rustc_private)]
#![feature(assert_matches)]

extern crate rustc_middle;
#[macro_use]
extern crate rustc_smir;
extern crate rustc_driver;
Expand Down
1 change: 1 addition & 0 deletions tests/ui-fulldeps/stable-mir/check_intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#![feature(rustc_private)]
#![feature(assert_matches)]

extern crate rustc_middle;
extern crate rustc_hir;
#[macro_use]
extern crate rustc_smir;
Expand Down
1 change: 1 addition & 0 deletions tests/ui-fulldeps/stable-mir/check_item_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#![feature(rustc_private)]
#![feature(assert_matches)]

extern crate rustc_middle;
#[macro_use]
extern crate rustc_smir;
extern crate rustc_driver;
Expand Down
1 change: 1 addition & 0 deletions tests/ui-fulldeps/stable-mir/check_normalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#![feature(rustc_private)]

extern crate rustc_middle;
#[macro_use]
extern crate rustc_smir;
extern crate rustc_driver;
Expand Down
Loading

0 comments on commit 3b02a33

Please sign in to comment.