Auto merge of #116837 - oli-obk:smir_run_macro, r=spastorino
Avoid having `rustc_smir` depend on `rustc_interface` or `rustc_driver` This is done by moving all the logic into a macro that performs the entire "run" operation in one go. This makes https://github.com/rust-lang/rust/pull/116806 obsolete as a follow up we should make the macro usable without manually having to write ```rust #[macro_use] extern crate rustc_smir; extern crate stable_mir; extern crate rustc_driver; extern crate rustc_interface; use rustc_smir::rustc_internal; ``` in every crate that uses the macro. r? `@spastorino`
This commit is contained in:
commit
858a42bf46
@ -4520,9 +4520,7 @@ name = "rustc_smir"
|
|||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"rustc_data_structures",
|
"rustc_data_structures",
|
||||||
"rustc_driver",
|
|
||||||
"rustc_hir",
|
"rustc_hir",
|
||||||
"rustc_interface",
|
|
||||||
"rustc_middle",
|
"rustc_middle",
|
||||||
"rustc_span",
|
"rustc_span",
|
||||||
"rustc_target",
|
"rustc_target",
|
||||||
|
@ -5,9 +5,7 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rustc_data_structures = { path = "../rustc_data_structures" }
|
rustc_data_structures = { path = "../rustc_data_structures" }
|
||||||
rustc_driver = { path = "../rustc_driver" }
|
|
||||||
rustc_hir = { path = "../rustc_hir" }
|
rustc_hir = { path = "../rustc_hir" }
|
||||||
rustc_interface = { path = "../rustc_interface" }
|
|
||||||
rustc_middle = { path = "../rustc_middle" }
|
rustc_middle = { path = "../rustc_middle" }
|
||||||
rustc_span = { path = "../rustc_span" }
|
rustc_span = { path = "../rustc_span" }
|
||||||
rustc_target = { path = "../rustc_target" }
|
rustc_target = { path = "../rustc_target" }
|
||||||
|
@ -3,22 +3,18 @@
|
|||||||
//! For that, we define APIs that will temporarily be public to 3P that exposes rustc internal APIs
|
//! For that, we define APIs that will temporarily be public to 3P that exposes rustc internal APIs
|
||||||
//! until stable MIR is complete.
|
//! until stable MIR is complete.
|
||||||
|
|
||||||
use crate::rustc_internal;
|
|
||||||
use crate::rustc_smir::Tables;
|
use crate::rustc_smir::Tables;
|
||||||
use rustc_data_structures::fx;
|
use rustc_data_structures::fx;
|
||||||
use rustc_data_structures::fx::FxIndexMap;
|
use rustc_data_structures::fx::FxIndexMap;
|
||||||
use rustc_driver::{Callbacks, Compilation, RunCompiler};
|
|
||||||
use rustc_interface::{interface, Queries};
|
|
||||||
use rustc_middle::mir::interpret::AllocId;
|
use rustc_middle::mir::interpret::AllocId;
|
||||||
use rustc_middle::ty;
|
use rustc_middle::ty;
|
||||||
use rustc_middle::ty::TyCtxt;
|
use rustc_middle::ty::TyCtxt;
|
||||||
use rustc_span::def_id::{CrateNum, DefId};
|
use rustc_span::def_id::{CrateNum, DefId};
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
use stable_mir::ty::IndexedVal;
|
use stable_mir::ty::IndexedVal;
|
||||||
use stable_mir::CompilerError;
|
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use std::ops::{ControlFlow, Index};
|
use std::ops::Index;
|
||||||
|
|
||||||
mod internal;
|
mod internal;
|
||||||
|
|
||||||
@ -143,63 +139,81 @@ pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct StableMir<B = (), C = ()>
|
#[macro_export]
|
||||||
where
|
macro_rules! run {
|
||||||
B: Send,
|
($args:expr, $callback:expr) => {
|
||||||
C: Send,
|
run!($args, tcx, $callback)
|
||||||
{
|
};
|
||||||
args: Vec<String>,
|
($args:expr, $tcx:ident, $callback:expr) => {{
|
||||||
callback: fn(TyCtxt<'_>) -> ControlFlow<B, C>,
|
use rustc_driver::{Callbacks, Compilation, RunCompiler};
|
||||||
result: Option<ControlFlow<B, C>>,
|
use rustc_interface::{interface, Queries};
|
||||||
}
|
use stable_mir::CompilerError;
|
||||||
|
use std::ops::ControlFlow;
|
||||||
|
|
||||||
impl<B, C> StableMir<B, C>
|
pub struct StableMir<B = (), C = ()>
|
||||||
where
|
where
|
||||||
B: Send,
|
B: Send,
|
||||||
C: Send,
|
C: Send,
|
||||||
{
|
{
|
||||||
/// Creates a new `StableMir` instance, with given test_function and arguments.
|
args: Vec<String>,
|
||||||
pub fn new(args: Vec<String>, callback: fn(TyCtxt<'_>) -> ControlFlow<B, C>) -> Self {
|
callback: fn(TyCtxt<'_>) -> ControlFlow<B, C>,
|
||||||
StableMir { args, callback, result: None }
|
result: Option<ControlFlow<B, C>>,
|
||||||
}
|
|
||||||
|
|
||||||
/// Runs the compiler against given target and tests it with `test_function`
|
|
||||||
pub fn run(&mut self) -> Result<C, CompilerError<B>> {
|
|
||||||
let compiler_result =
|
|
||||||
rustc_driver::catch_fatal_errors(|| RunCompiler::new(&self.args.clone(), self).run());
|
|
||||||
match (compiler_result, self.result.take()) {
|
|
||||||
(Ok(Ok(())), Some(ControlFlow::Continue(value))) => Ok(value),
|
|
||||||
(Ok(Ok(())), Some(ControlFlow::Break(value))) => Err(CompilerError::Interrupted(value)),
|
|
||||||
(Ok(Ok(_)), None) => Err(CompilerError::Skipped),
|
|
||||||
(Ok(Err(_)), _) => Err(CompilerError::CompilationFailed),
|
|
||||||
(Err(_), _) => Err(CompilerError::ICE),
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<B, C> Callbacks for StableMir<B, C>
|
impl<B, C> StableMir<B, C>
|
||||||
where
|
where
|
||||||
B: Send,
|
B: Send,
|
||||||
C: Send,
|
C: Send,
|
||||||
{
|
{
|
||||||
/// Called after analysis. Return value instructs the compiler whether to
|
/// Creates a new `StableMir` instance, with given test_function and arguments.
|
||||||
/// continue the compilation afterwards (defaults to `Compilation::Continue`)
|
pub fn new(args: Vec<String>, callback: fn(TyCtxt<'_>) -> ControlFlow<B, C>) -> Self {
|
||||||
fn after_analysis<'tcx>(
|
StableMir { args, callback, result: None }
|
||||||
&mut self,
|
|
||||||
_compiler: &interface::Compiler,
|
|
||||||
queries: &'tcx Queries<'tcx>,
|
|
||||||
) -> Compilation {
|
|
||||||
queries.global_ctxt().unwrap().enter(|tcx| {
|
|
||||||
rustc_internal::run(tcx, || {
|
|
||||||
self.result = Some((self.callback)(tcx));
|
|
||||||
});
|
|
||||||
if self.result.as_ref().is_some_and(|val| val.is_continue()) {
|
|
||||||
Compilation::Continue
|
|
||||||
} else {
|
|
||||||
Compilation::Stop
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
/// Runs the compiler against given target and tests it with `test_function`
|
||||||
|
pub fn run(&mut self) -> Result<C, CompilerError<B>> {
|
||||||
|
let compiler_result = rustc_driver::catch_fatal_errors(|| {
|
||||||
|
RunCompiler::new(&self.args.clone(), self).run()
|
||||||
|
});
|
||||||
|
match (compiler_result, self.result.take()) {
|
||||||
|
(Ok(Ok(())), Some(ControlFlow::Continue(value))) => Ok(value),
|
||||||
|
(Ok(Ok(())), Some(ControlFlow::Break(value))) => {
|
||||||
|
Err(CompilerError::Interrupted(value))
|
||||||
|
}
|
||||||
|
(Ok(Ok(_)), None) => Err(CompilerError::Skipped),
|
||||||
|
(Ok(Err(_)), _) => Err(CompilerError::CompilationFailed),
|
||||||
|
(Err(_), _) => Err(CompilerError::ICE),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<B, C> Callbacks for StableMir<B, C>
|
||||||
|
where
|
||||||
|
B: Send,
|
||||||
|
C: Send,
|
||||||
|
{
|
||||||
|
/// Called after analysis. Return value instructs the compiler whether to
|
||||||
|
/// continue the compilation afterwards (defaults to `Compilation::Continue`)
|
||||||
|
fn after_analysis<'tcx>(
|
||||||
|
&mut self,
|
||||||
|
_compiler: &interface::Compiler,
|
||||||
|
queries: &'tcx Queries<'tcx>,
|
||||||
|
) -> Compilation {
|
||||||
|
queries.global_ctxt().unwrap().enter(|tcx| {
|
||||||
|
rustc_internal::run(tcx, || {
|
||||||
|
self.result = Some((self.callback)(tcx));
|
||||||
|
});
|
||||||
|
if self.result.as_ref().is_some_and(|val| val.is_continue()) {
|
||||||
|
Compilation::Continue
|
||||||
|
} else {
|
||||||
|
Compilation::Stop
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
StableMir::new($args, |$tcx| $callback).run()
|
||||||
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Simmilar to rustc's `FxIndexMap`, `IndexMap` with extra
|
/// Simmilar to rustc's `FxIndexMap`, `IndexMap` with extra
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
// ignore-stage1
|
// ignore-stage1
|
||||||
// ignore-cross-compile
|
// ignore-cross-compile
|
||||||
// ignore-remote
|
// ignore-remote
|
||||||
|
// ignore-windows-gnu mingw has troubles with linking https://github.com/rust-lang/rust/pull/116837
|
||||||
// edition: 2021
|
// edition: 2021
|
||||||
|
|
||||||
#![feature(rustc_private)]
|
#![feature(rustc_private)]
|
||||||
@ -11,8 +12,11 @@
|
|||||||
#![feature(control_flow_enum)]
|
#![feature(control_flow_enum)]
|
||||||
|
|
||||||
extern crate rustc_middle;
|
extern crate rustc_middle;
|
||||||
|
#[macro_use]
|
||||||
extern crate rustc_smir;
|
extern crate rustc_smir;
|
||||||
extern crate stable_mir;
|
extern crate stable_mir;
|
||||||
|
extern crate rustc_driver;
|
||||||
|
extern crate rustc_interface;
|
||||||
|
|
||||||
use rustc_middle::ty::TyCtxt;
|
use rustc_middle::ty::TyCtxt;
|
||||||
use mir::{mono::Instance, TerminatorKind::*};
|
use mir::{mono::Instance, TerminatorKind::*};
|
||||||
@ -82,7 +86,7 @@ fn main() {
|
|||||||
CRATE_NAME.to_string(),
|
CRATE_NAME.to_string(),
|
||||||
path.to_string(),
|
path.to_string(),
|
||||||
];
|
];
|
||||||
rustc_internal::StableMir::new(args, test_stable_mir).run().unwrap();
|
run!(args, tcx, test_stable_mir(tcx)).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_input(path: &str) -> std::io::Result<()> {
|
fn generate_input(path: &str) -> std::io::Result<()> {
|
||||||
|
@ -4,19 +4,22 @@
|
|||||||
// ignore-stage1
|
// ignore-stage1
|
||||||
// ignore-cross-compile
|
// ignore-cross-compile
|
||||||
// ignore-remote
|
// ignore-remote
|
||||||
|
// ignore-windows-gnu mingw has troubles with linking https://github.com/rust-lang/rust/pull/116837
|
||||||
// edition: 2021
|
// edition: 2021
|
||||||
|
|
||||||
#![feature(rustc_private)]
|
#![feature(rustc_private)]
|
||||||
#![feature(assert_matches)]
|
#![feature(assert_matches)]
|
||||||
|
|
||||||
extern crate rustc_middle;
|
extern crate rustc_middle;
|
||||||
|
#[macro_use]
|
||||||
extern crate rustc_smir;
|
extern crate rustc_smir;
|
||||||
|
extern crate rustc_driver;
|
||||||
|
extern crate rustc_interface;
|
||||||
extern crate stable_mir;
|
extern crate stable_mir;
|
||||||
|
|
||||||
use rustc_middle::ty::TyCtxt;
|
use rustc_middle::ty::TyCtxt;
|
||||||
use rustc_smir::rustc_internal;
|
use rustc_smir::rustc_internal;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::ops::ControlFlow;
|
|
||||||
|
|
||||||
/// This test will generate and analyze a dummy crate using the stable mir.
|
/// This test will generate and analyze a dummy crate using the stable mir.
|
||||||
/// For that, it will first write the dummy crate into a file.
|
/// For that, it will first write the dummy crate into a file.
|
||||||
@ -33,28 +36,26 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn test_continue(args: Vec<String>) {
|
fn test_continue(args: Vec<String>) {
|
||||||
let continue_fn = |_: TyCtxt| ControlFlow::Continue::<(), bool>(true);
|
let result = run!(args, ControlFlow::Continue::<(), bool>(true));
|
||||||
let result = rustc_internal::StableMir::new(args, continue_fn).run();
|
|
||||||
assert_eq!(result, Ok(true));
|
assert_eq!(result, Ok(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_break(args: Vec<String>) {
|
fn test_break(args: Vec<String>) {
|
||||||
let continue_fn = |_: TyCtxt| ControlFlow::Break::<bool, i32>(false);
|
let result = run!(args, ControlFlow::Break::<bool, i32>(false));
|
||||||
let result = rustc_internal::StableMir::new(args, continue_fn).run();
|
|
||||||
assert_eq!(result, Err(stable_mir::CompilerError::Interrupted(false)));
|
assert_eq!(result, Err(stable_mir::CompilerError::Interrupted(false)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(unreachable_code)]
|
||||||
fn test_skipped(mut args: Vec<String>) {
|
fn test_skipped(mut args: Vec<String>) {
|
||||||
args.push("--version".to_string());
|
args.push("--version".to_string());
|
||||||
let unreach_fn = |_: TyCtxt| -> ControlFlow<()> { unreachable!() };
|
let result = run!(args, unreachable!() as ControlFlow<()>);
|
||||||
let result = rustc_internal::StableMir::new(args, unreach_fn).run();
|
|
||||||
assert_eq!(result, Err(stable_mir::CompilerError::Skipped));
|
assert_eq!(result, Err(stable_mir::CompilerError::Skipped));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(unreachable_code)]
|
||||||
fn test_failed(mut args: Vec<String>) {
|
fn test_failed(mut args: Vec<String>) {
|
||||||
args.push("--cfg=broken".to_string());
|
args.push("--cfg=broken".to_string());
|
||||||
let unreach_fn = |_: TyCtxt| -> ControlFlow<()> { unreachable!() };
|
let result = run!(args, unreachable!() as ControlFlow<()>);
|
||||||
let result = rustc_internal::StableMir::new(args, unreach_fn).run();
|
|
||||||
assert_eq!(result, Err(stable_mir::CompilerError::CompilationFailed));
|
assert_eq!(result, Err(stable_mir::CompilerError::CompilationFailed));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
// ignore-stage1
|
// ignore-stage1
|
||||||
// ignore-cross-compile
|
// ignore-cross-compile
|
||||||
// ignore-remote
|
// ignore-remote
|
||||||
|
// ignore-windows-gnu mingw has troubles with linking https://github.com/rust-lang/rust/pull/116837
|
||||||
// edition: 2021
|
// edition: 2021
|
||||||
|
|
||||||
#![feature(rustc_private)]
|
#![feature(rustc_private)]
|
||||||
@ -12,7 +13,10 @@
|
|||||||
|
|
||||||
extern crate rustc_hir;
|
extern crate rustc_hir;
|
||||||
extern crate rustc_middle;
|
extern crate rustc_middle;
|
||||||
|
#[macro_use]
|
||||||
extern crate rustc_smir;
|
extern crate rustc_smir;
|
||||||
|
extern crate rustc_driver;
|
||||||
|
extern crate rustc_interface;
|
||||||
extern crate stable_mir;
|
extern crate stable_mir;
|
||||||
|
|
||||||
use rustc_hir::def::DefKind;
|
use rustc_hir::def::DefKind;
|
||||||
@ -185,7 +189,7 @@ fn main() {
|
|||||||
CRATE_NAME.to_string(),
|
CRATE_NAME.to_string(),
|
||||||
path.to_string(),
|
path.to_string(),
|
||||||
];
|
];
|
||||||
rustc_internal::StableMir::new(args, test_stable_mir).run().unwrap();
|
run!(args, tcx, test_stable_mir(tcx)).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_input(path: &str) -> std::io::Result<()> {
|
fn generate_input(path: &str) -> std::io::Result<()> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user