Skip to content

Commit

Permalink
feat: add panic op to prelude (#802)
Browse files Browse the repository at this point in the history
As described in #801
  • Loading branch information
ss2165 committed Jan 9, 2024
1 parent 41b55e0 commit 60844fe
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/extension/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use smol_str::SmolStr;
use crate::{
extension::{ExtensionId, TypeDefBound},
ops::LeafOp,
type_row,
types::{
type_param::{TypeArg, TypeParam},
CustomCheckFailure, CustomType, FunctionType, PolyFuncType, Type, TypeBound,
Expand Down Expand Up @@ -87,6 +88,13 @@ lazy_static! {
)
.unwrap();
prelude
.add_op(
SmolStr::new_inline(PANIC_OP_ID),
"Panic with input error".to_string(),
FunctionType::new(type_row![Type::new_extension(ERROR_CUSTOM_TYPE)], type_row![]),
)
.unwrap();
prelude
};
/// An extension registry containing only the prelude
pub static ref PRELUDE_REGISTRY: ExtensionRegistry =
Expand Down Expand Up @@ -121,6 +129,8 @@ pub fn array_type(size: TypeArg, element_ty: Type) -> Type {

/// Name of the operation in the prelude for creating new arrays.
pub const NEW_ARRAY_OP_ID: &str = "new_array";
/// Name of the prelude panic operation.
pub const PANIC_OP_ID: &str = "panic";

/// Initialize a new array op of element type `element_ty` of length `size`
pub fn new_array_op(element_ty: Type, size: u64) -> LeafOp {
Expand Down Expand Up @@ -260,7 +270,7 @@ mod test {
}

#[test]
/// test the prelude error type.
/// test the prelude error type and panic op.
fn test_error_type() {
let ext_def = PRELUDE
.get_type(&ERROR_TYPE_NAME)
Expand All @@ -283,5 +293,17 @@ mod test {
);
assert!(error_val.equal_consts(&ConstError::new(2, "my message")));
assert!(!error_val.equal_consts(&ConstError::new(3, "my message")));

let mut b = DFGBuilder::new(FunctionType::new_endo(type_row![])).unwrap();

let err = b.add_load_const(error_val).unwrap();

let op = PRELUDE
.instantiate_extension_op(PANIC_OP_ID, [], &PRELUDE_REGISTRY)
.unwrap();

b.add_dataflow_op(op, [err]).unwrap();

b.finish_prelude_hugr_with_outputs([]).unwrap();
}
}

0 comments on commit 60844fe

Please sign in to comment.