Adjust StableMIR interface to return and not crash
Invoking StableMir::run() on a crate that has any compilation error was crashing the entire process. Instead, return a `CompilerError` so the user knows compilation did not succeed. I believe ICE will also be converted to `CompilerError`. I'm also adding a return value to the callback, because I think it will be handy for users (at least it was for my current task of implementing a tool to validate stable-mir). However, if people disagree, I can remove that.
This commit is contained in:
parent
f222a2dd8f
commit
2db01be584
3 changed files with 37 additions and 11 deletions
|
@ -7,6 +7,7 @@ use std::fmt::Debug;
|
||||||
use std::ops::Index;
|
use std::ops::Index;
|
||||||
|
|
||||||
use crate::rustc_internal;
|
use crate::rustc_internal;
|
||||||
|
use crate::stable_mir::CompilerError;
|
||||||
use crate::{
|
use crate::{
|
||||||
rustc_smir::Tables,
|
rustc_smir::Tables,
|
||||||
stable_mir::{self, with},
|
stable_mir::{self, with},
|
||||||
|
@ -17,6 +18,7 @@ use rustc_middle::mir::interpret::AllocId;
|
||||||
use rustc_middle::ty::TyCtxt;
|
use rustc_middle::ty::TyCtxt;
|
||||||
use rustc_session::EarlyErrorHandler;
|
use rustc_session::EarlyErrorHandler;
|
||||||
pub use rustc_span::def_id::{CrateNum, DefId};
|
pub use rustc_span::def_id::{CrateNum, DefId};
|
||||||
|
use rustc_span::ErrorGuaranteed;
|
||||||
|
|
||||||
fn with_tables<R>(mut f: impl FnMut(&mut Tables<'_>) -> R) -> R {
|
fn with_tables<R>(mut f: impl FnMut(&mut Tables<'_>) -> R) -> R {
|
||||||
let mut ret = None;
|
let mut ret = None;
|
||||||
|
@ -189,27 +191,38 @@ pub(crate) fn opaque<T: Debug>(value: &T) -> Opaque {
|
||||||
Opaque(format!("{value:?}"))
|
Opaque(format!("{value:?}"))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct StableMir {
|
pub struct StableMir<T: Send>
|
||||||
|
where
|
||||||
|
T: Send,
|
||||||
|
{
|
||||||
args: Vec<String>,
|
args: Vec<String>,
|
||||||
callback: fn(TyCtxt<'_>),
|
callback: fn(TyCtxt<'_>) -> T,
|
||||||
|
result: Option<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StableMir {
|
impl<T> StableMir<T>
|
||||||
|
where
|
||||||
|
T: Send,
|
||||||
|
{
|
||||||
/// Creates a new `StableMir` instance, with given test_function and arguments.
|
/// Creates a new `StableMir` instance, with given test_function and arguments.
|
||||||
pub fn new(args: Vec<String>, callback: fn(TyCtxt<'_>)) -> Self {
|
pub fn new(args: Vec<String>, callback: fn(TyCtxt<'_>) -> T) -> Self {
|
||||||
StableMir { args, callback }
|
StableMir { args, callback, result: None }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Runs the compiler against given target and tests it with `test_function`
|
/// Runs the compiler against given target and tests it with `test_function`
|
||||||
pub fn run(&mut self) {
|
pub fn run(mut self) -> Result<T, CompilerError> {
|
||||||
rustc_driver::catch_fatal_errors(|| {
|
rustc_driver::catch_fatal_errors(|| {
|
||||||
RunCompiler::new(&self.args.clone(), self).run().unwrap();
|
RunCompiler::new(&self.args.clone(), &mut self).run().unwrap();
|
||||||
})
|
})
|
||||||
.unwrap();
|
.map_err(|e| <ErrorGuaranteed as Into<CompilerError>>::into(e))?;
|
||||||
|
Ok(self.result.unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Callbacks for StableMir {
|
impl<T> Callbacks for StableMir<T>
|
||||||
|
where
|
||||||
|
T: Send,
|
||||||
|
{
|
||||||
/// Called after analysis. Return value instructs the compiler whether to
|
/// Called after analysis. Return value instructs the compiler whether to
|
||||||
/// continue the compilation afterwards (defaults to `Compilation::Continue`)
|
/// continue the compilation afterwards (defaults to `Compilation::Continue`)
|
||||||
fn after_analysis<'tcx>(
|
fn after_analysis<'tcx>(
|
||||||
|
@ -219,7 +232,9 @@ impl Callbacks for StableMir {
|
||||||
queries: &'tcx Queries<'tcx>,
|
queries: &'tcx Queries<'tcx>,
|
||||||
) -> Compilation {
|
) -> Compilation {
|
||||||
queries.global_ctxt().unwrap().enter(|tcx| {
|
queries.global_ctxt().unwrap().enter(|tcx| {
|
||||||
rustc_internal::run(tcx, || (self.callback)(tcx));
|
rustc_internal::run(tcx, || {
|
||||||
|
self.result = Some((self.callback)(tcx));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
// No need to keep going.
|
// No need to keep going.
|
||||||
Compilation::Stop
|
Compilation::Stop
|
||||||
|
|
|
@ -10,12 +10,13 @@
|
||||||
use crate::rustc_internal::{self, opaque};
|
use crate::rustc_internal::{self, opaque};
|
||||||
use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx};
|
use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx};
|
||||||
use crate::stable_mir::ty::{FloatTy, GenericParamDef, IntTy, Movability, RigidTy, TyKind, UintTy};
|
use crate::stable_mir::ty::{FloatTy, GenericParamDef, IntTy, Movability, RigidTy, TyKind, UintTy};
|
||||||
use crate::stable_mir::{self, Context};
|
use crate::stable_mir::{self, CompilerError, Context};
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_middle::mir::interpret::{alloc_range, AllocId};
|
use rustc_middle::mir::interpret::{alloc_range, AllocId};
|
||||||
use rustc_middle::mir::{self, ConstantKind};
|
use rustc_middle::mir::{self, ConstantKind};
|
||||||
use rustc_middle::ty::{self, Ty, TyCtxt, Variance};
|
use rustc_middle::ty::{self, Ty, TyCtxt, Variance};
|
||||||
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
|
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
|
||||||
|
use rustc_span::ErrorGuaranteed;
|
||||||
use rustc_target::abi::FieldIdx;
|
use rustc_target::abi::FieldIdx;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
|
@ -1452,3 +1453,9 @@ impl<'tcx> Stable<'tcx> for rustc_span::Span {
|
||||||
opaque(self)
|
opaque(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<ErrorGuaranteed> for CompilerError {
|
||||||
|
fn from(_error: ErrorGuaranteed) -> Self {
|
||||||
|
CompilerError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -56,6 +56,10 @@ pub type TraitDecls = Vec<TraitDef>;
|
||||||
/// A list of impl trait decls.
|
/// A list of impl trait decls.
|
||||||
pub type ImplTraitDecls = Vec<ImplDef>;
|
pub type ImplTraitDecls = Vec<ImplDef>;
|
||||||
|
|
||||||
|
/// An error type used to represent an error that has already been reported by the compiler.
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
|
pub struct CompilerError;
|
||||||
|
|
||||||
/// Holds information about a crate.
|
/// Holds information about a crate.
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||||
pub struct Crate {
|
pub struct Crate {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue