Skip to content

Commit

Permalink
all: replace qualified anyhow:: with use (avanhatt#83)
Browse files Browse the repository at this point in the history
Replace qualifed `anyhow::...` with `use anyhow::...`.
  • Loading branch information
mmcloughlin authored Sep 28, 2024
1 parent 4d6b78c commit 743ebed
Show file tree
Hide file tree
Showing 22 changed files with 276 additions and 339 deletions.
10 changes: 4 additions & 6 deletions cranelift/isle/veri/aslp/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::{bail, Result};
use reqwest::IntoUrl;
use serde::Deserialize;
use tracing::debug;
Expand All @@ -10,17 +11,14 @@ pub struct Client<'a> {
}

impl<'a> Client<'a> {
pub fn new<U: IntoUrl>(
client: &'a reqwest::blocking::Client,
server_url: U,
) -> anyhow::Result<Self> {
pub fn new<U: IntoUrl>(client: &'a reqwest::blocking::Client, server_url: U) -> Result<Self> {
Ok(Self {
client,
server_url: server_url.into_url()?,
})
}

pub fn opcode(&self, opcode: u32) -> anyhow::Result<Block> {
pub fn opcode(&self, opcode: u32) -> Result<Block> {
// Model for response JSON data.
#[derive(Deserialize, Debug)]
struct Response {
Expand All @@ -41,7 +39,7 @@ impl<'a> Client<'a> {

// Ensure response instruction matches.
if res.instruction != opcode_hex {
anyhow::bail!("response opcode mismatch");
bail!("response opcode mismatch");
}

// Parse semantics.
Expand Down
3 changes: 2 additions & 1 deletion cranelift/isle/veri/aslp/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Result;
use clap::Parser as ClapParser;
use cranelift_isle_veri_aslp::parser;
use std::{fs, path::PathBuf};
Expand All @@ -13,7 +14,7 @@ struct Args {
debug_level: u8,
}

fn main() -> anyhow::Result<()> {
fn main() -> Result<()> {
let args = Args::parse();

tracing_subscriber::fmt()
Expand Down
31 changes: 16 additions & 15 deletions cranelift/isle/veri/aslp/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Result;
use enquote::unquote;
use pest::{
iterators::{Pair, Pairs},
Expand All @@ -12,17 +13,17 @@ use crate::ast::{Block, Expr, Func, LExpr, Slice, Stmt, Type};
#[grammar = "aslt.pest"]
struct ASLTParser;

pub fn parse(src: &str) -> anyhow::Result<Block> {
pub fn parse(src: &str) -> Result<Block> {
let pairs = ASLTParser::parse(Rule::aslt, src)?;
parse_block(pairs)
}

fn parse_block(pairs: Pairs<Rule>) -> anyhow::Result<Block> {
fn parse_block(pairs: Pairs<Rule>) -> Result<Block> {
let stmts = parse_stmts(pairs)?;
Ok(Block { stmts })
}

fn parse_stmts(pairs: Pairs<Rule>) -> anyhow::Result<Vec<Stmt>> {
fn parse_stmts(pairs: Pairs<Rule>) -> Result<Vec<Stmt>> {
let mut stmts = Vec::new();
for pair in pairs {
let rule = pair.as_rule();
Expand All @@ -36,7 +37,7 @@ fn parse_stmts(pairs: Pairs<Rule>) -> anyhow::Result<Vec<Stmt>> {
Ok(stmts)
}

fn parse_stmt(pair: Pair<Rule>) -> anyhow::Result<Stmt> {
fn parse_stmt(pair: Pair<Rule>) -> Result<Stmt> {
let rule = pair.as_rule();
debug!(?rule, "parse stmt");
match rule {
Expand Down Expand Up @@ -90,7 +91,7 @@ fn parse_stmt(pair: Pair<Rule>) -> anyhow::Result<Stmt> {
}
}

fn parse_lexpr(pair: Pair<Rule>) -> anyhow::Result<LExpr> {
fn parse_lexpr(pair: Pair<Rule>) -> Result<LExpr> {
let rule = pair.as_rule();
debug!(?rule, "parse lexpr");
match rule {
Expand All @@ -115,7 +116,7 @@ fn parse_lexpr(pair: Pair<Rule>) -> anyhow::Result<LExpr> {
}
}

fn parse_expr(pair: Pair<Rule>) -> anyhow::Result<Expr> {
fn parse_expr(pair: Pair<Rule>) -> Result<Expr> {
let rule = pair.as_rule();
debug!(?rule, "parse expr");
match rule {
Expand Down Expand Up @@ -161,7 +162,7 @@ fn parse_expr(pair: Pair<Rule>) -> anyhow::Result<Expr> {
}
}

fn parse_exprs(pairs: Pairs<Rule>) -> anyhow::Result<Vec<Expr>> {
fn parse_exprs(pairs: Pairs<Rule>) -> Result<Vec<Expr>> {
let mut exprs = Vec::new();
for pair in pairs {
let rule = pair.as_rule();
Expand All @@ -174,7 +175,7 @@ fn parse_exprs(pairs: Pairs<Rule>) -> anyhow::Result<Vec<Expr>> {
Ok(exprs)
}

fn parse_slice(pair: Pair<Rule>) -> anyhow::Result<Slice> {
fn parse_slice(pair: Pair<Rule>) -> Result<Slice> {
let rule = pair.as_rule();
debug!(?rule, "parse slice");
match rule {
Expand All @@ -189,7 +190,7 @@ fn parse_slice(pair: Pair<Rule>) -> anyhow::Result<Slice> {
}
}

fn parse_slices(pairs: Pairs<Rule>) -> anyhow::Result<Vec<Slice>> {
fn parse_slices(pairs: Pairs<Rule>) -> Result<Vec<Slice>> {
let mut slices = Vec::new();
for pair in pairs {
let rule = pair.as_rule();
Expand All @@ -202,7 +203,7 @@ fn parse_slices(pairs: Pairs<Rule>) -> anyhow::Result<Vec<Slice>> {
Ok(slices)
}

fn parse_type(pair: Pair<Rule>) -> anyhow::Result<Type> {
fn parse_type(pair: Pair<Rule>) -> Result<Type> {
let rule = pair.as_rule();
debug!(?rule, "parse type");
match rule {
Expand All @@ -216,7 +217,7 @@ fn parse_type(pair: Pair<Rule>) -> anyhow::Result<Type> {
}
}

fn parse_var(pair: Pair<Rule>) -> anyhow::Result<String> {
fn parse_var(pair: Pair<Rule>) -> Result<String> {
let rule = pair.as_rule();
debug!(?rule, "parse var");
match rule {
Expand All @@ -226,7 +227,7 @@ fn parse_var(pair: Pair<Rule>) -> anyhow::Result<String> {
}
}

fn parse_vars(pairs: Pairs<Rule>) -> anyhow::Result<Vec<String>> {
fn parse_vars(pairs: Pairs<Rule>) -> Result<Vec<String>> {
let mut vars = Vec::new();
for pair in pairs {
let rule = pair.as_rule();
Expand All @@ -239,7 +240,7 @@ fn parse_vars(pairs: Pairs<Rule>) -> anyhow::Result<Vec<String>> {
Ok(vars)
}

fn parse_func_ident(pair: Pair<Rule>) -> anyhow::Result<Func> {
fn parse_func_ident(pair: Pair<Rule>) -> Result<Func> {
let rule = pair.as_rule();
debug!(?rule, "parse func ident");
match rule {
Expand All @@ -253,11 +254,11 @@ fn parse_func_ident(pair: Pair<Rule>) -> anyhow::Result<Func> {
}
}

fn parse_ident(pair: Pair<Rule>) -> anyhow::Result<String> {
fn parse_ident(pair: Pair<Rule>) -> Result<String> {
Ok(unquote(pair.as_str())?)
}

fn parse_literal(pair: Pair<Rule>) -> anyhow::Result<String> {
fn parse_literal(pair: Pair<Rule>) -> Result<String> {
let rule = pair.as_rule();
debug!(?rule, "parse literal");
match rule {
Expand Down
5 changes: 3 additions & 2 deletions cranelift/isle/veri/isaspec/src/bin/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Result;
use clap::Parser as ClapParser;
use cranelift_codegen::isa::aarch64::inst::{
vreg, writable_vreg, writable_xreg, xreg, ALUOp, ALUOp3, BitOp, Cond, Inst, OperandSize,
Expand All @@ -24,7 +25,7 @@ struct Args {
debug_level: u8,
}

fn main() -> anyhow::Result<()> {
fn main() -> Result<()> {
let args = Args::parse();

// Setup tracing output.
Expand Down Expand Up @@ -231,7 +232,7 @@ fn define_insts() -> Vec<Inst> {
}

// Convert a semantics block and print the result.
fn convert_block(block: &Block) -> anyhow::Result<()> {
fn convert_block(block: &Block) -> Result<()> {
// Translation.
let mut translator = Translator::new(aarch64::state(), "v".to_string());
translator.translate(block)?;
Expand Down
5 changes: 3 additions & 2 deletions cranelift/isle/veri/isaspec/src/bin/isaspec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::io;
use std::io::Write;
use std::path::{Path, PathBuf};

use anyhow::Result;
use clap::Parser as ClapParser;
use cranelift_codegen::isa::aarch64::inst::{
writable_xreg, xreg, ALUOp, ALUOp3, BitOp, Inst, OperandSize,
Expand Down Expand Up @@ -34,7 +35,7 @@ struct Args {
debug_level: u8,
}

fn main() -> anyhow::Result<()> {
fn main() -> Result<()> {
let args = Args::parse();

// Setup tracing output.
Expand Down Expand Up @@ -74,7 +75,7 @@ fn main() -> anyhow::Result<()> {
Ok(())
}

fn write_spec(path: &Path, defs: &Vec<Def>) -> anyhow::Result<()> {
fn write_spec(path: &Path, defs: &Vec<Def>) -> Result<()> {
let mut output = std::fs::File::create(path)?;

// Code generation warning.
Expand Down
29 changes: 13 additions & 16 deletions cranelift/isle/veri/isaspec/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::collections::{HashMap, HashSet};

use anyhow::bail;
use anyhow::{bail, Result};
use cranelift_codegen::isa::aarch64::inst::Inst;
use cranelift_isle::ast::{Def, Spec, SpecExpr};
use cranelift_isle::lexer::Pos;
Expand Down Expand Up @@ -87,13 +87,13 @@ impl<'a> Builder<'a> {
Self { cfg, client }
}

pub fn build(&self) -> anyhow::Result<Def> {
pub fn build(&self) -> Result<Def> {
let spec = self.spec(&self.cfg)?;
let def = Def::Spec(spec);
Ok(def)
}

fn spec(&self, cfg: &SpecConfig) -> anyhow::Result<Spec> {
fn spec(&self, cfg: &SpecConfig) -> Result<Spec> {
// Derive conditions for each case.
let conds: Vec<Conditions> = cfg
.cases
Expand All @@ -113,7 +113,7 @@ impl<'a> Builder<'a> {
Ok(spec)
}

fn case(&self, i: usize, case: &InstConfig) -> anyhow::Result<Conditions> {
fn case(&self, i: usize, case: &InstConfig) -> Result<Conditions> {
// Semantics.
let block = inst_semantics(&case.inst, self.client)?;

Expand All @@ -131,7 +131,7 @@ impl<'a> Builder<'a> {
for target in reads.iter().sorted() {
// Expect mapping for the read.
let Some(mapping) = case.mappings.reads.get(target) else {
anyhow::bail!("read of {target} is unmapped");
bail!("read of {target} is unmapped");
};

// Lookup variable holding the initial read value.
Expand All @@ -142,7 +142,7 @@ impl<'a> Builder<'a> {
}

if let Some(target) = case.mappings.required_reads().difference(reads).next() {
anyhow::bail!("{target} should have been read");
bail!("{target} should have been read");
}

// Writes mapping.
Expand All @@ -151,20 +151,20 @@ impl<'a> Builder<'a> {
for target in writes.iter().sorted() {
// Expect mapping for the write.
let Some(mapping) = case.mappings.writes.get(target) else {
anyhow::bail!("write to {target} is unmapped");
bail!("write to {target} is unmapped");
};

// Lookup bound variable.
let Some(Binding::Var(v)) = bindings.get(target) else {
anyhow::bail!("{target} not bound to variable");
bail!("{target} not bound to variable");
};

// Substitute variable for mapped expression.
substitutions.insert(v.clone(), mapping.expr.clone());
}

if let Some(target) = case.mappings.required_writes().difference(writes).next() {
anyhow::bail!("{target} should have been written");
bail!("{target} should have been written");
}

// Finalize provided constraints.
Expand Down Expand Up @@ -194,10 +194,7 @@ impl<'a> Builder<'a> {
}
}

fn substitute(
expr: SpecExpr,
substitutions: &HashMap<String, SpecExpr>,
) -> anyhow::Result<SpecExpr> {
fn substitute(expr: SpecExpr, substitutions: &HashMap<String, SpecExpr>) -> Result<SpecExpr> {
Ok(match expr {
// Variable
SpecExpr::Var { ref var, pos: _ } => {
Expand All @@ -223,7 +220,7 @@ fn substitute(
}
Ok((var, substitute(expr, substitutions)?))
})
.collect::<anyhow::Result<_>>()?,
.collect::<Result<_>>()?,
body: Box::new(substitute(*body, substitutions)?),
pos,
},
Expand Down Expand Up @@ -256,7 +253,7 @@ fn substitute(
args: args
.into_iter()
.map(|arg| substitute(arg, substitutions))
.collect::<anyhow::Result<_>>()?,
.collect::<Result<_>>()?,
pos,
},
SpecExpr::Pair { l, r, pos } => SpecExpr::Pair {
Expand All @@ -275,7 +272,7 @@ fn substitute(
args: args
.into_iter()
.map(|arg| substitute(arg, substitutions))
.collect::<anyhow::Result<_>>()?,
.collect::<Result<_>>()?,
pos,
},
})
Expand Down
Loading

0 comments on commit 743ebed

Please sign in to comment.