clif-util: Use anyhow::Error for errors instead of String

Also does the same for `cranelift-filetests`.
This commit is contained in:
Nick Fitzgerald
2020-09-14 16:23:30 -07:00
parent 9fea412333
commit 31cbbd1d20
43 changed files with 415 additions and 443 deletions

View File

@@ -3,7 +3,7 @@
//! The `interpret` test command interprets each function on the host machine
//! using [RunCommand](cranelift_reader::RunCommand)s.
use crate::subtest::{Context, SubTest, SubtestResult};
use crate::subtest::{Context, SubTest};
use cranelift_codegen::{self, ir};
use cranelift_interpreter::environment::Environment;
use cranelift_interpreter::interpreter::{ControlFlow, Interpreter};
@@ -13,13 +13,12 @@ use std::borrow::Cow;
struct TestInterpret;
pub fn subtest(parsed: &TestCommand) -> SubtestResult<Box<dyn SubTest>> {
pub fn subtest(parsed: &TestCommand) -> anyhow::Result<Box<dyn SubTest>> {
assert_eq!(parsed.command, "interpret");
if !parsed.options.is_empty() {
Err(format!("No options allowed on {}", parsed))
} else {
Ok(Box::new(TestInterpret))
anyhow::bail!("No options allowed on {}", parsed);
}
Ok(Box::new(TestInterpret))
}
impl SubTest for TestInterpret {
@@ -35,26 +34,28 @@ impl SubTest for TestInterpret {
false
}
fn run(&self, func: Cow<ir::Function>, context: &Context) -> SubtestResult<()> {
fn run(&self, func: Cow<ir::Function>, context: &Context) -> anyhow::Result<()> {
for comment in context.details.comments.iter() {
if let Some(command) =
parse_run_command(comment.text, &func.signature).map_err(|e| e.to_string())?
{
if let Some(command) = parse_run_command(comment.text, &func.signature)? {
trace!("Parsed run command: {}", command);
let mut env = Environment::default();
env.add(func.name.to_string(), func.clone().into_owned());
let interpreter = Interpreter::new(env);
command.run(|func_name, args| {
// Because we have stored function names with a leading %, we need to re-add it.
let func_name = &format!("%{}", func_name);
match interpreter.call_by_name(func_name, args) {
Ok(ControlFlow::Return(results)) => Ok(results),
Ok(_) => panic!("Unexpected returned control flow--this is likely a bug."),
Err(t) => Err(t.to_string()),
}
})?;
command
.run(|func_name, args| {
// Because we have stored function names with a leading %, we need to re-add it.
let func_name = &format!("%{}", func_name);
match interpreter.call_by_name(func_name, args) {
Ok(ControlFlow::Return(results)) => Ok(results),
Ok(_) => {
panic!("Unexpected returned control flow--this is likely a bug.")
}
Err(t) => Err(format!("unexpected trap: {:?}", t)),
}
})
.map_err(|e| anyhow::anyhow!("{}", e))?;
}
}
Ok(())