Skip to content

Commit 2db01be

Browse files
committed
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.
1 parent f222a2d commit 2db01be

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::fmt::Debug;
77
use std::ops::Index;
88

99
use crate::rustc_internal;
10+
use crate::stable_mir::CompilerError;
1011
use crate::{
1112
rustc_smir::Tables,
1213
stable_mir::{self, with},
@@ -17,6 +18,7 @@ use rustc_middle::mir::interpret::AllocId;
1718
use rustc_middle::ty::TyCtxt;
1819
use rustc_session::EarlyErrorHandler;
1920
pub use rustc_span::def_id::{CrateNum, DefId};
21+
use rustc_span::ErrorGuaranteed;
2022

2123
fn with_tables<R>(mut f: impl FnMut(&mut Tables<'_>) -> R) -> R {
2224
let mut ret = None;
@@ -189,27 +191,38 @@ pub(crate) fn opaque<T: Debug>(value: &T) -> Opaque {
189191
Opaque(format!("{value:?}"))
190192
}
191193

192-
pub struct StableMir {
194+
pub struct StableMir<T: Send>
195+
where
196+
T: Send,
197+
{
193198
args: Vec<String>,
194-
callback: fn(TyCtxt<'_>),
199+
callback: fn(TyCtxt<'_>) -> T,
200+
result: Option<T>,
195201
}
196202

197-
impl StableMir {
203+
impl<T> StableMir<T>
204+
where
205+
T: Send,
206+
{
198207
/// Creates a new `StableMir` instance, with given test_function and arguments.
199-
pub fn new(args: Vec<String>, callback: fn(TyCtxt<'_>)) -> Self {
200-
StableMir { args, callback }
208+
pub fn new(args: Vec<String>, callback: fn(TyCtxt<'_>) -> T) -> Self {
209+
StableMir { args, callback, result: None }
201210
}
202211

203212
/// Runs the compiler against given target and tests it with `test_function`
204-
pub fn run(&mut self) {
213+
pub fn run(mut self) -> Result<T, CompilerError> {
205214
rustc_driver::catch_fatal_errors(|| {
206-
RunCompiler::new(&self.args.clone(), self).run().unwrap();
215+
RunCompiler::new(&self.args.clone(), &mut self).run().unwrap();
207216
})
208-
.unwrap();
217+
.map_err(|e| <ErrorGuaranteed as Into<CompilerError>>::into(e))?;
218+
Ok(self.result.unwrap())
209219
}
210220
}
211221

212-
impl Callbacks for StableMir {
222+
impl<T> Callbacks for StableMir<T>
223+
where
224+
T: Send,
225+
{
213226
/// Called after analysis. Return value instructs the compiler whether to
214227
/// continue the compilation afterwards (defaults to `Compilation::Continue`)
215228
fn after_analysis<'tcx>(
@@ -219,7 +232,9 @@ impl Callbacks for StableMir {
219232
queries: &'tcx Queries<'tcx>,
220233
) -> Compilation {
221234
queries.global_ctxt().unwrap().enter(|tcx| {
222-
rustc_internal::run(tcx, || (self.callback)(tcx));
235+
rustc_internal::run(tcx, || {
236+
self.result = Some((self.callback)(tcx));
237+
});
223238
});
224239
// No need to keep going.
225240
Compilation::Stop

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
use crate::rustc_internal::{self, opaque};
1111
use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx};
1212
use crate::stable_mir::ty::{FloatTy, GenericParamDef, IntTy, Movability, RigidTy, TyKind, UintTy};
13-
use crate::stable_mir::{self, Context};
13+
use crate::stable_mir::{self, CompilerError, Context};
1414
use rustc_hir as hir;
1515
use rustc_middle::mir::interpret::{alloc_range, AllocId};
1616
use rustc_middle::mir::{self, ConstantKind};
1717
use rustc_middle::ty::{self, Ty, TyCtxt, Variance};
1818
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
19+
use rustc_span::ErrorGuaranteed;
1920
use rustc_target::abi::FieldIdx;
2021
use tracing::debug;
2122

@@ -1452,3 +1453,9 @@ impl<'tcx> Stable<'tcx> for rustc_span::Span {
14521453
opaque(self)
14531454
}
14541455
}
1456+
1457+
impl From<ErrorGuaranteed> for CompilerError {
1458+
fn from(_error: ErrorGuaranteed) -> Self {
1459+
CompilerError
1460+
}
1461+
}

compiler/rustc_smir/src/stable_mir/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ pub type TraitDecls = Vec<TraitDef>;
5656
/// A list of impl trait decls.
5757
pub type ImplTraitDecls = Vec<ImplDef>;
5858

59+
/// An error type used to represent an error that has already been reported by the compiler.
60+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
61+
pub struct CompilerError;
62+
5963
/// Holds information about a crate.
6064
#[derive(Clone, PartialEq, Eq, Debug)]
6165
pub struct Crate {

0 commit comments

Comments
 (0)