Create StableMir replacer for SMirCalls
This commit is contained in:
parent
cedbe5c715
commit
c2fe0bf253
5 changed files with 54 additions and 33 deletions
|
@ -4251,8 +4251,11 @@ dependencies = [
|
||||||
name = "rustc_smir"
|
name = "rustc_smir"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"rustc_driver",
|
||||||
"rustc_hir",
|
"rustc_hir",
|
||||||
|
"rustc_interface",
|
||||||
"rustc_middle",
|
"rustc_middle",
|
||||||
|
"rustc_session",
|
||||||
"rustc_span",
|
"rustc_span",
|
||||||
"rustc_target",
|
"rustc_target",
|
||||||
"scoped-tls",
|
"scoped-tls",
|
||||||
|
|
|
@ -9,6 +9,9 @@ rustc_hir = { path = "../rustc_hir", optional = true }
|
||||||
rustc_middle = { path = "../rustc_middle", optional = true }
|
rustc_middle = { path = "../rustc_middle", optional = true }
|
||||||
rustc_span = { path = "../rustc_span", optional = true }
|
rustc_span = { path = "../rustc_span", optional = true }
|
||||||
rustc_target = { path = "../rustc_target", optional = true }
|
rustc_target = { path = "../rustc_target", optional = true }
|
||||||
|
rustc_driver = { path = "../rustc_driver", optional = true }
|
||||||
|
rustc_interface = { path = "../rustc_interface", optional = true}
|
||||||
|
rustc_session = {path = "../rustc_session", optional = true}
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
scoped-tls = "1.0"
|
scoped-tls = "1.0"
|
||||||
|
|
||||||
|
@ -18,4 +21,7 @@ default = [
|
||||||
"rustc_middle",
|
"rustc_middle",
|
||||||
"rustc_span",
|
"rustc_span",
|
||||||
"rustc_target",
|
"rustc_target",
|
||||||
|
"rustc_driver",
|
||||||
|
"rustc_interface",
|
||||||
|
"rustc_session",
|
||||||
]
|
]
|
||||||
|
|
|
@ -6,11 +6,15 @@
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::string::ToString;
|
use std::string::ToString;
|
||||||
|
|
||||||
|
use crate::rustc_internal;
|
||||||
use crate::{
|
use crate::{
|
||||||
rustc_smir::Tables,
|
rustc_smir::Tables,
|
||||||
stable_mir::{self, with},
|
stable_mir::{self, with},
|
||||||
};
|
};
|
||||||
|
use rustc_driver::{Callbacks, Compilation, RunCompiler};
|
||||||
|
use rustc_interface::{interface, Queries};
|
||||||
use rustc_middle::ty::TyCtxt;
|
use rustc_middle::ty::TyCtxt;
|
||||||
|
use rustc_session::EarlyErrorHandler;
|
||||||
pub use rustc_span::def_id::{CrateNum, DefId};
|
pub use rustc_span::def_id::{CrateNum, DefId};
|
||||||
|
|
||||||
fn with_tables<R>(mut f: impl FnMut(&mut Tables<'_>) -> R) -> R {
|
fn with_tables<R>(mut f: impl FnMut(&mut Tables<'_>) -> R) -> R {
|
||||||
|
@ -163,3 +167,40 @@ pub type Opaque = impl Debug + ToString + Clone;
|
||||||
pub(crate) fn opaque<T: Debug>(value: &T) -> Opaque {
|
pub(crate) fn opaque<T: Debug>(value: &T) -> Opaque {
|
||||||
format!("{value:?}")
|
format!("{value:?}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct StableMir {
|
||||||
|
args: Vec<String>,
|
||||||
|
callback: fn(TyCtxt<'_>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StableMir {
|
||||||
|
/// Creates a new `StableMir` instance, with given test_function and arguments.
|
||||||
|
pub fn new(args: Vec<String>, callback: fn(TyCtxt<'_>)) -> Self {
|
||||||
|
StableMir { args, callback }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs the compiler against given target and tests it with `test_function`
|
||||||
|
pub fn run(&mut self) {
|
||||||
|
rustc_driver::catch_fatal_errors(|| {
|
||||||
|
RunCompiler::new(&self.args.clone(), self).run().unwrap();
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Callbacks for StableMir {
|
||||||
|
/// Called after analysis. Return value instructs the compiler whether to
|
||||||
|
/// continue the compilation afterwards (defaults to `Compilation::Continue`)
|
||||||
|
fn after_analysis<'tcx>(
|
||||||
|
&mut self,
|
||||||
|
_handler: &EarlyErrorHandler,
|
||||||
|
_compiler: &interface::Compiler,
|
||||||
|
queries: &'tcx Queries<'tcx>,
|
||||||
|
) -> Compilation {
|
||||||
|
queries.global_ctxt().unwrap().enter(|tcx| {
|
||||||
|
rustc_internal::run(tcx, || (self.callback)(tcx));
|
||||||
|
});
|
||||||
|
// No need to keep going.
|
||||||
|
Compilation::Stop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -13,11 +13,10 @@
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
use crate::rustc_smir::Tables;
|
|
||||||
|
|
||||||
use self::ty::{
|
use self::ty::{
|
||||||
GenericDef, Generics, ImplDef, ImplTrait, PredicateKind, Span, TraitDecl, TraitDef, Ty, TyKind,
|
GenericDef, Generics, ImplDef, ImplTrait, PredicateKind, Span, TraitDecl, TraitDef, Ty, TyKind,
|
||||||
};
|
};
|
||||||
|
use crate::rustc_smir::Tables;
|
||||||
|
|
||||||
pub mod mir;
|
pub mod mir;
|
||||||
pub mod ty;
|
pub mod ty;
|
||||||
|
|
|
@ -9,18 +9,12 @@
|
||||||
#![feature(rustc_private)]
|
#![feature(rustc_private)]
|
||||||
#![feature(assert_matches)]
|
#![feature(assert_matches)]
|
||||||
|
|
||||||
extern crate rustc_driver;
|
|
||||||
extern crate rustc_hir;
|
extern crate rustc_hir;
|
||||||
extern crate rustc_interface;
|
|
||||||
extern crate rustc_middle;
|
extern crate rustc_middle;
|
||||||
extern crate rustc_session;
|
|
||||||
extern crate rustc_smir;
|
extern crate rustc_smir;
|
||||||
|
|
||||||
use rustc_driver::{Callbacks, Compilation, RunCompiler};
|
|
||||||
use rustc_hir::def::DefKind;
|
use rustc_hir::def::DefKind;
|
||||||
use rustc_interface::{interface, Queries};
|
|
||||||
use rustc_middle::ty::TyCtxt;
|
use rustc_middle::ty::TyCtxt;
|
||||||
use rustc_session::EarlyErrorHandler;
|
|
||||||
use rustc_smir::{rustc_internal, stable_mir};
|
use rustc_smir::{rustc_internal, stable_mir};
|
||||||
use std::assert_matches::assert_matches;
|
use std::assert_matches::assert_matches;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
@ -130,8 +124,8 @@ fn get_item<'a>(
|
||||||
|
|
||||||
/// 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.
|
||||||
/// It will invoke the compiler using a custom Callback implementation, which will
|
/// Then it will create a `StableMir` using custom arguments and then
|
||||||
/// invoke Stable MIR APIs after the compiler has finished its analysis.
|
/// it will run the compiler.
|
||||||
fn main() {
|
fn main() {
|
||||||
let path = "input.rs";
|
let path = "input.rs";
|
||||||
generate_input(&path).unwrap();
|
generate_input(&path).unwrap();
|
||||||
|
@ -142,29 +136,7 @@ fn main() {
|
||||||
CRATE_NAME.to_string(),
|
CRATE_NAME.to_string(),
|
||||||
path.to_string(),
|
path.to_string(),
|
||||||
];
|
];
|
||||||
rustc_driver::catch_fatal_errors(|| {
|
rustc_internal::StableMir::new(args, test_stable_mir).run();
|
||||||
RunCompiler::new(&args, &mut SMirCalls {}).run().unwrap();
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
struct SMirCalls {}
|
|
||||||
|
|
||||||
impl Callbacks for SMirCalls {
|
|
||||||
/// Called after analysis. Return value instructs the compiler whether to
|
|
||||||
/// continue the compilation afterwards (defaults to `Compilation::Continue`)
|
|
||||||
fn after_analysis<'tcx>(
|
|
||||||
&mut self,
|
|
||||||
_handler: &EarlyErrorHandler,
|
|
||||||
_compiler: &interface::Compiler,
|
|
||||||
queries: &'tcx Queries<'tcx>,
|
|
||||||
) -> Compilation {
|
|
||||||
queries.global_ctxt().unwrap().enter(|tcx| {
|
|
||||||
rustc_smir::rustc_internal::run(tcx, || test_stable_mir(tcx));
|
|
||||||
});
|
|
||||||
// No need to keep going.
|
|
||||||
Compilation::Stop
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_input(path: &str) -> std::io::Result<()> {
|
fn generate_input(path: &str) -> std::io::Result<()> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue