Skip to content

Commit

Permalink
Revamp error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vaivaswatha committed Oct 21, 2023
1 parent 571b733 commit cbe24a2
Show file tree
Hide file tree
Showing 23 changed files with 309 additions and 162 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ keywords = ["pliron", "llvm", "mlir", "compiler"]
generational-arena = "0.2"
downcast-rs = "1.2.0"
rustc-hash = "1.1.0"
# anyerror = "1.0.75"
# thiserror = "1.0.24"
thiserror = "1.0.49"
# clap = "4.1.6"
apint = "0.2.0"
sorted_vector_map = "0.1.0"
Expand Down
18 changes: 9 additions & 9 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::{
common_traits::Verify,
context::Context,
dialect::{Dialect, DialectName},
error::CompilerError,
error::Result,
printable::{self, Printable},
};

Expand All @@ -54,7 +54,7 @@ pub trait Attribute: Printable + Verify + Downcast + CastFrom + Sync {
Self: Sized;

/// Verify all interfaces implemented by this attribute.
fn verify_interfaces(&self, ctx: &Context) -> Result<(), CompilerError>;
fn verify_interfaces(&self, ctx: &Context) -> Result<()>;

/// Register this attribute's [AttrId] in the dialect it belongs to.
/// **Warning**: No check is made as to whether this attr is already registered
Expand Down Expand Up @@ -163,7 +163,7 @@ impl Printable for AttrId {
}

/// Every attribute interface must have a function named `verify` with this type.
pub type AttrInterfaceVerifier = fn(&dyn Attribute, &Context) -> Result<(), CompilerError>;
pub type AttrInterfaceVerifier = fn(&dyn Attribute, &Context) -> Result<()>;

/// impl [Attribute] for a rust type.
///
Expand All @@ -179,7 +179,7 @@ pub type AttrInterfaceVerifier = fn(&dyn Attribute, &Context) -> Result<(), Comp
/// );
/// # use pliron::{
/// # impl_attr, printable::{self, Printable},
/// # context::Context, error::CompilerError, common_traits::Verify,
/// # context::Context, error::Result, common_traits::Verify,
/// # attribute::Attribute,
/// # };
/// # impl Printable for MyAttr {
Expand All @@ -194,7 +194,7 @@ pub type AttrInterfaceVerifier = fn(&dyn Attribute, &Context) -> Result<(), Comp
/// # }
///
/// # impl Verify for MyAttr {
/// # fn verify(&self, _ctx: &Context) -> Result<(), CompilerError> {
/// # fn verify(&self, _ctx: &Context) -> Result<()> {
/// # todo!()
/// # }
/// # }
Expand Down Expand Up @@ -234,7 +234,7 @@ macro_rules! impl_attr {
dialect: $crate::dialect::DialectName::new($dialect_name),
}
}
fn verify_interfaces(&self, ctx: &Context) -> Result<(), CompilerError> {
fn verify_interfaces(&self, ctx: &Context) -> $crate::error::Result<()> {
let interface_verifiers = paste::paste!{
inventory::iter::<[<AttrInterfaceVerifier_ $structname>]>
};
Expand Down Expand Up @@ -262,7 +262,7 @@ macro_rules! impl_attr {
/// );
/// trait MyAttrInterface: Attribute {
/// fn monu(&self);
/// fn verify(attr: &dyn Attribute, ctx: &Context) -> Result<(), CompilerError>
/// fn verify(attr: &dyn Attribute, ctx: &Context) -> Result<()>
/// where
/// Self: Sized,
/// {
Expand All @@ -277,7 +277,7 @@ macro_rules! impl_attr {
/// );
/// # use pliron::{
/// # impl_attr, printable::{self, Printable},
/// # context::Context, error::CompilerError, common_traits::Verify,
/// # context::Context, error::Result, common_traits::Verify,
/// # attribute::Attribute, impl_attr_interface
/// # };
/// # impl Printable for MyAttr {
Expand All @@ -287,7 +287,7 @@ macro_rules! impl_attr {
/// # }
///
/// # impl Verify for MyAttr {
/// # fn verify(&self, _ctx: &Context) -> Result<(), CompilerError> {
/// # fn verify(&self, _ctx: &Context) -> Result<()> {
/// # todo!()
/// # }
/// # }
Expand Down
4 changes: 2 additions & 2 deletions src/basic_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
common_traits::{Named, Verify},
context::{private::ArenaObj, ArenaCell, Context, Ptr},
debug_info::get_block_arg_name,
error::CompilerError,
error::Result,
indented_block,
linked_list::{private, ContainsLinkedList, LinkedList},
operation::Operation,
Expand Down Expand Up @@ -294,7 +294,7 @@ impl ArenaObj for BasicBlock {
}

impl Verify for BasicBlock {
fn verify(&self, ctx: &Context) -> Result<(), CompilerError> {
fn verify(&self, ctx: &Context) -> Result<()> {
self.iter(ctx).try_for_each(|op| op.deref(ctx).verify(ctx))
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/common_traits.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Utility traits such as [Named], [Verify] etc.
use crate::{context::Context, error::CompilerError};
use crate::{context::Context, error::Result};

/// Check and ensure correctness.
pub trait Verify {
fn verify(&self, ctx: &Context) -> Result<(), CompilerError>;
fn verify(&self, ctx: &Context) -> Result<()>;
}

/// Anything that has a name.
Expand Down
3 changes: 2 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
basic_block::BasicBlock,
common_traits::Verify,
dialect::{Dialect, DialectName},
error::Result,
op::{OpCreator, OpId},
operation::Operation,
printable::{self, Printable},
Expand Down Expand Up @@ -175,7 +176,7 @@ impl<T: ArenaObj + Printable> Printable for Ptr<T> {
}

impl<T: ArenaObj + Verify> Verify for Ptr<T> {
fn verify(&self, ctx: &Context) -> Result<(), crate::error::CompilerError> {
fn verify(&self, ctx: &Context) -> Result<()> {
self.deref(ctx).verify(ctx)
}
}
6 changes: 3 additions & 3 deletions src/debug_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ mod tests {
types::{IntegerType, Signedness},
},
},
error::CompilerError,
error::Result,
op::Op,
};
use apint::ApInt;

use super::{get_operation_result_name, set_operation_result_name};

#[test]
fn test_op_result_name() -> Result<(), CompilerError> {
fn test_op_result_name() -> Result<()> {
let mut ctx = Context::new();
dialects::builtin::register(&mut ctx);

Expand All @@ -158,7 +158,7 @@ mod tests {
}

#[test]
fn test_block_arg_name() -> Result<(), CompilerError> {
fn test_block_arg_name() -> Result<()> {
let mut ctx = Context::new();
dialects::builtin::register(&mut ctx);

Expand Down
8 changes: 7 additions & 1 deletion src/dialect.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! [Dialect]s are a mechanism to group related [Op](crate::op::Op)s, [Type](crate::type::Type)s
//! and [Attribute](crate::attribute::Attribute)s.
use std::ops::Deref;
use std::{fmt::Display, ops::Deref};

use combine::{easy, ParseResult, Parser};
use rustc_hash::FxHashMap;
Expand Down Expand Up @@ -32,6 +32,12 @@ impl Printable for DialectName {
_state: &printable::State,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
<Self as Display>::fmt(self, f)
}
}

impl Display for DialectName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/dialects/builtin/attr_interfaces.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
attribute::Attribute,
context::{Context, Ptr},
error::CompilerError,
error::Result,
r#type::TypeObj,
};

Expand All @@ -11,7 +11,7 @@ pub trait TypedAttrInterface: Attribute {
/// Get this attribute's type.
fn get_type(&self) -> Ptr<TypeObj>;

fn verify(_attr: &dyn Attribute, _ctx: &Context) -> Result<(), CompilerError>
fn verify(_attr: &dyn Attribute, _ctx: &Context) -> Result<()>
where
Self: Sized,
{
Expand Down
16 changes: 8 additions & 8 deletions src/dialects/builtin/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
common_traits::Verify,
context::{Context, Ptr},
dialect::Dialect,
error::CompilerError,
error::Result,
impl_attr, impl_attr_interface,
printable::{self, Printable},
r#type::TypeObj,
Expand Down Expand Up @@ -46,7 +46,7 @@ impl Printable for StringAttr {
}

impl Verify for StringAttr {
fn verify(&self, _ctx: &Context) -> Result<(), CompilerError> {
fn verify(&self, _ctx: &Context) -> Result<()> {
todo!()
}
}
Expand All @@ -72,7 +72,7 @@ impl Printable for IntegerAttr {
}

impl Verify for IntegerAttr {
fn verify(&self, _ctx: &Context) -> Result<(), CompilerError> {
fn verify(&self, _ctx: &Context) -> Result<()> {
todo!()
}
}
Expand Down Expand Up @@ -119,7 +119,7 @@ impl Printable for FloatAttr {
}

impl Verify for FloatAttr {
fn verify(&self, _ctx: &Context) -> Result<(), CompilerError> {
fn verify(&self, _ctx: &Context) -> Result<()> {
todo!()
}
}
Expand Down Expand Up @@ -164,7 +164,7 @@ impl Printable for SmallDictAttr {
}

impl Verify for SmallDictAttr {
fn verify(&self, _ctx: &Context) -> Result<(), CompilerError> {
fn verify(&self, _ctx: &Context) -> Result<()> {
todo!()
}
}
Expand Down Expand Up @@ -222,7 +222,7 @@ impl Printable for VecAttr {
}

impl Verify for VecAttr {
fn verify(&self, _ctx: &Context) -> Result<(), CompilerError> {
fn verify(&self, _ctx: &Context) -> Result<()> {
todo!()
}
}
Expand Down Expand Up @@ -251,7 +251,7 @@ impl Printable for UnitAttr {
}

impl Verify for UnitAttr {
fn verify(&self, _ctx: &Context) -> Result<(), CompilerError> {
fn verify(&self, _ctx: &Context) -> Result<()> {
Ok(())
}
}
Expand Down Expand Up @@ -280,7 +280,7 @@ impl Printable for TypeAttr {
}

impl Verify for TypeAttr {
fn verify(&self, _ctx: &Context) -> Result<(), CompilerError> {
fn verify(&self, _ctx: &Context) -> Result<()> {
Ok(())
}
}
Expand Down
Loading

0 comments on commit cbe24a2

Please sign in to comment.