WIP send panics to the repl more gracefully

This commit is contained in:
Richard Feldman 2022-10-30 03:12:41 -04:00
parent 45e800c328
commit a36ddbf6cb
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
5 changed files with 31 additions and 14 deletions

View file

@ -118,9 +118,9 @@ macro_rules! run_jit_function {
// only if there are no exceptions thrown, check for errors
assert!($errors.is_empty(), "Encountered errors:\n{}", $errors);
$transform(success)
Ok($transform(success))
}
Err(error_msg) => panic!("Roc failed with message: {}", error_msg),
Err(error_msg) => Err(error_msg.to_string()),
}
}};
}

View file

@ -107,7 +107,11 @@ impl<'a> ReplApp<'a> for CliApp {
/// Run user code that returns a type with a `Builtin` layout
/// Size of the return value is statically determined from its Rust type
fn call_function<Return, F>(&mut self, main_fn_name: &str, mut transform: F) -> Expr<'a>
fn call_function<Return, F>(
&mut self,
main_fn_name: &str,
mut transform: F,
) -> Result<Expr<'a>, String>
where
F: FnMut(&'a Self::Memory, Return) -> Expr<'a>,
Self::Memory: 'a,

View file

@ -49,7 +49,7 @@ pub fn jit_to_ast<'a, A: ReplApp<'a>>(
interns: &'a Interns,
layout_interner: LayoutInterner<'a>,
target_info: TargetInfo,
) -> Expr<'a> {
) -> Result<Expr<'a>, String> {
let mut env = Env {
arena,
subs,
@ -330,7 +330,7 @@ fn jit_to_ast_help<'a, A: ReplApp<'a>>(
main_fn_name: &str,
layout: &Layout<'a>,
var: Variable,
) -> Expr<'a> {
) -> Result<Expr<'a>, String> {
let (newtype_containers, alias_content, raw_var) = unroll_newtypes_and_aliases(env, var);
macro_rules! num_helper {
@ -341,7 +341,7 @@ fn jit_to_ast_help<'a, A: ReplApp<'a>>(
};
}
let expr = match layout {
let result = match layout {
Layout::Builtin(Builtin::Bool) => {
app.call_function(main_fn_name, |mem: &A::Memory, num: bool| {
bool_to_ast(
@ -499,7 +499,7 @@ fn jit_to_ast_help<'a, A: ReplApp<'a>>(
Layout::RecursivePointer => {
unreachable!("RecursivePointers can only be inside structures")
}
Layout::LambdaSet(_) => OPAQUE_FUNCTION,
Layout::LambdaSet(_) => Ok(OPAQUE_FUNCTION),
Layout::Boxed(_) => {
let size = layout.stack_size(&env.layout_cache.interner, env.target_info);
@ -516,7 +516,7 @@ fn jit_to_ast_help<'a, A: ReplApp<'a>>(
}
};
apply_newtypes(env, newtype_containers.into_bump_slice(), expr)
result.map(|expr| apply_newtypes(env, newtype_containers.into_bump_slice(), expr))
}
fn tag_name_to_expr<'a>(env: &Env<'a, '_>, tag_name: &TagName) -> Expr<'a> {

View file

@ -11,12 +11,20 @@ pub trait ReplApp<'a> {
/// Run user code that returns a type with a `Builtin` layout
/// Size of the return value is statically determined from its Rust type
/// The `transform` callback takes the app's memory and the returned value
fn call_function<Return, F>(&mut self, main_fn_name: &str, transform: F) -> Expr<'a>
fn call_function<Return, F>(
&mut self,
main_fn_name: &str,
transform: F,
) -> Result<Expr<'a>, String>
where
F: FnMut(&'a Self::Memory, Return) -> Expr<'a>,
Self::Memory: 'a;
fn call_function_returns_roc_list<F>(&mut self, main_fn_name: &str, transform: F) -> Expr<'a>
fn call_function_returns_roc_list<F>(
&mut self,
main_fn_name: &str,
transform: F,
) -> Result<Expr<'a>, String>
where
F: FnMut(&'a Self::Memory, (usize, usize, usize)) -> Expr<'a>,
Self::Memory: 'a,
@ -29,7 +37,7 @@ pub trait ReplApp<'a> {
target_info: TargetInfo,
main_fn_name: &str,
transform: F,
) -> T
) -> Result<T, String>
where
F: Fn(&'a Self::Memory, usize) -> T,
Self::Memory: 'a,
@ -49,7 +57,7 @@ pub trait ReplApp<'a> {
main_fn_name: &str,
ret_bytes: usize,
transform: F,
) -> T
) -> Result<T, String>
where
F: FnMut(&'a Self::Memory, usize) -> T,
Self::Memory: 'a;

View file

@ -84,7 +84,11 @@ impl<'a> ReplApp<'a> for ExpectReplApp<'a> {
/// Size of the return value is statically determined from its Rust type
/// The `transform` callback takes the app's memory and the returned value
/// _main_fn_name is always the same and we don't use it here
fn call_function<Return, F>(&mut self, _main_fn_name: &str, mut transform: F) -> Expr<'a>
fn call_function<Return, F>(
&mut self,
_main_fn_name: &str,
mut transform: F,
) -> Result<Expr<'a>, String>
where
F: FnMut(&'a Self::Memory, Return) -> Expr<'a>,
Self::Memory: 'a,
@ -95,7 +99,8 @@ impl<'a> ReplApp<'a> for ExpectReplApp<'a> {
ptr.read()
};
transform(self.memory, result)
// TODO if it panicked, return Err
Ok(transform(self.memory, result))
}
fn call_function_returns_roc_list<F>(&mut self, main_fn_name: &str, transform: F) -> Expr<'a>