From 62223e38ceb3524eade784aa0407beacc13518b2 Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Tue, 29 Apr 2025 10:00:12 +0100 Subject: [PATCH 1/5] feat!: Make `NamedOp` private. Add `MakeExtensionOp::name` and `MakeOpDef::opdef_name` --- hugr-core/src/extension/prelude.rs | 57 +++++++++++-------- hugr-core/src/extension/prelude/generic.rs | 21 +++---- hugr-core/src/extension/simple_op.rs | 40 ++++++------- hugr-core/src/ops.rs | 2 +- hugr-core/src/ops/controlflow.rs | 17 ++---- hugr-core/src/ops/custom.rs | 17 +++++- .../std_extensions/arithmetic/conversions.rs | 13 +++-- .../std_extensions/arithmetic/float_ops.rs | 5 ++ .../src/std_extensions/arithmetic/int_ops.rs | 12 ++-- .../collections/array/array_clone.rs | 14 +++-- .../collections/array/array_conversion.rs | 23 ++++---- .../collections/array/array_discard.rs | 16 ++---- .../collections/array/array_op.rs | 13 +++-- .../collections/array/array_repeat.rs | 18 +++--- .../collections/array/array_scan.rs | 14 +++-- .../src/std_extensions/collections/list.rs | 17 +++--- .../collections/static_array.rs | 15 +++-- hugr-core/src/std_extensions/logic.rs | 10 +++- hugr-core/src/std_extensions/ptr.rs | 18 +++--- hugr-llvm/src/custom/extension_op.rs | 17 ++++-- hugr-llvm/src/extension/collections/list.rs | 7 ++- hugr-llvm/src/extension/int.rs | 13 +++-- hugr-llvm/src/extension/logic.rs | 2 +- hugr-llvm/src/extension/prelude.rs | 2 +- hugr-llvm/src/utils/fat.rs | 8 +-- hugr-passes/src/const_fold/test.rs | 2 +- hugr-passes/src/force_order.rs | 57 +++++++++---------- hugr-passes/src/replace_types.rs | 25 ++++---- hugr-passes/src/replace_types/linearize.rs | 10 +++- hugr-passes/src/untuple.rs | 8 +-- 30 files changed, 263 insertions(+), 230 deletions(-) diff --git a/hugr-core/src/extension/prelude.rs b/hugr-core/src/extension/prelude.rs index b1e78baf8..cbbca3759 100644 --- a/hugr-core/src/extension/prelude.rs +++ b/hugr-core/src/extension/prelude.rs @@ -595,7 +595,12 @@ impl ConstFold for TupleOpDef { } } } + impl MakeOpDef for TupleOpDef { + fn opdef_name(&self) -> OpName { + <&Self as Into<&'static str>>::into(self).into() + } + fn init_signature(&self, _extension_ref: &Weak) -> SignatureFunc { let rv = TypeRV::new_row_var_use(0, TypeBound::Any); let tuple_type = TypeRV::new_tuple(vec![rv.clone()]); @@ -649,20 +654,20 @@ impl MakeTuple { } } -impl NamedOp for MakeTuple { +impl MakeExtensionOp for MakeTuple { fn name(&self) -> OpName { - TupleOpDef::MakeTuple.name() + TupleOpDef::MakeTuple.opdef_name() } -} -impl MakeExtensionOp for MakeTuple { fn from_extension_op(ext_op: &crate::ops::ExtensionOp) -> Result where Self: Sized, { let def = TupleOpDef::from_def(ext_op.def())?; if def != TupleOpDef::MakeTuple { - return Err(OpLoadError::NotMember(ext_op.def().name().to_string()))?; + return Err(OpLoadError::NotMember( + ext_op.unqualified_name().to_string(), + ))?; } let [TypeArg::Sequence { elems }] = ext_op.args() else { return Err(SignatureError::InvalidTypeArgs)?; @@ -711,20 +716,20 @@ impl UnpackTuple { } } -impl NamedOp for UnpackTuple { +impl MakeExtensionOp for UnpackTuple { fn name(&self) -> OpName { - TupleOpDef::UnpackTuple.name() + TupleOpDef::UnpackTuple.opdef_name() } -} -impl MakeExtensionOp for UnpackTuple { fn from_extension_op(ext_op: &crate::ops::ExtensionOp) -> Result where Self: Sized, { let def = TupleOpDef::from_def(ext_op.def())?; if def != TupleOpDef::UnpackTuple { - return Err(OpLoadError::NotMember(ext_op.def().name().to_string()))?; + return Err(OpLoadError::NotMember( + ext_op.unqualified_name().to_string(), + ))?; } let [TypeArg::Sequence { elems }] = ext_op.args() else { return Err(SignatureError::InvalidTypeArgs)?; @@ -760,16 +765,13 @@ impl MakeRegisteredOp for UnpackTuple { } } +/// Name of the no-op operation. +pub const NOOP_OP_ID: OpName = OpName::new_inline("Noop"); + #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] /// A no-op operation definition. pub struct NoopDef; -impl NamedOp for NoopDef { - fn name(&self) -> OpName { - "Noop".into() - } -} - impl std::str::FromStr for NoopDef { type Err = (); @@ -781,7 +783,12 @@ impl std::str::FromStr for NoopDef { } } } + impl MakeOpDef for NoopDef { + fn opdef_name(&self) -> OpName { + NOOP_OP_ID + } + fn init_signature(&self, _extension_ref: &Weak) -> SignatureFunc { let tv = Type::new_var_use(0, TypeBound::Any); PolyFuncType::new([TypeBound::Any.into()], Signature::new_endo(tv)).into() @@ -836,13 +843,12 @@ impl Default for Noop { Self(Type::UNIT) } } -impl NamedOp for Noop { + +impl MakeExtensionOp for Noop { fn name(&self) -> OpName { NoopDef.name() } -} -impl MakeExtensionOp for Noop { fn from_extension_op(ext_op: &crate::ops::ExtensionOp) -> Result where Self: Sized, @@ -875,11 +881,6 @@ pub struct BarrierDef; /// Name of the barrier operation. pub const BARRIER_OP_ID: OpName = OpName::new_inline("Barrier"); -impl NamedOp for BarrierDef { - fn name(&self) -> OpName { - BARRIER_OP_ID - } -} impl std::str::FromStr for BarrierDef { type Err = (); @@ -894,6 +895,10 @@ impl std::str::FromStr for BarrierDef { } impl MakeOpDef for BarrierDef { + fn opdef_name(&self) -> OpName { + BARRIER_OP_ID + } + fn init_signature(&self, _extension_ref: &Weak) -> SignatureFunc { PolyFuncTypeRV::new( vec![TypeParam::new_list(TypeBound::Any)], @@ -945,6 +950,10 @@ impl NamedOp for Barrier { } impl MakeExtensionOp for Barrier { + fn name(&self) -> OpName { + BarrierDef.name() + } + fn from_extension_op(ext_op: &crate::ops::ExtensionOp) -> Result where Self: Sized, diff --git a/hugr-core/src/extension/prelude/generic.rs b/hugr-core/src/extension/prelude/generic.rs index 7db49ed79..2c93908f7 100644 --- a/hugr-core/src/extension/prelude/generic.rs +++ b/hugr-core/src/extension/prelude/generic.rs @@ -9,7 +9,6 @@ use crate::extension::OpDef; use crate::extension::SignatureFunc; use crate::extension::{ConstFold, ExtensionId}; use crate::ops::ExtensionOp; -use crate::ops::NamedOp; use crate::ops::OpName; use crate::type_row; use crate::types::FuncValueType; @@ -28,23 +27,17 @@ use super::{ConstUsize, PRELUDE_ID}; use crate::types::type_param::TypeParam; /// Name of the operation for loading generic BoundedNat parameters. -pub const LOAD_NAT_OP_ID: OpName = OpName::new_inline("load_nat"); +pub static LOAD_NAT_OP_ID: OpName = OpName::new_inline("load_nat"); /// Definition of the load nat operation. #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] pub struct LoadNatDef; -impl NamedOp for LoadNatDef { - fn name(&self) -> OpName { - LOAD_NAT_OP_ID - } -} - impl FromStr for LoadNatDef { type Err = (); fn from_str(s: &str) -> Result { - if s == LoadNatDef.name() { + if s == Self.name() { Ok(Self) } else { Err(()) @@ -72,6 +65,10 @@ impl ConstFold for LoadNatDef { } impl MakeOpDef for LoadNatDef { + fn opdef_name(&self) -> OpName { + LOAD_NAT_OP_ID.clone() + } + fn from_def(op_def: &OpDef) -> Result where Self: Sized, @@ -120,13 +117,11 @@ impl LoadNat { } } -impl NamedOp for LoadNat { +impl MakeExtensionOp for LoadNat { fn name(&self) -> OpName { - LOAD_NAT_OP_ID + LoadNatDef.opdef_name() } -} -impl MakeExtensionOp for LoadNat { fn from_extension_op(ext_op: &ExtensionOp) -> Result where Self: Sized, diff --git a/hugr-core/src/extension/simple_op.rs b/hugr-core/src/extension/simple_op.rs index dcae554ec..03f622f3b 100644 --- a/hugr-core/src/extension/simple_op.rs +++ b/hugr-core/src/extension/simple_op.rs @@ -5,11 +5,7 @@ use std::sync::Weak; use strum::IntoEnumIterator; use crate::ops::{ExtensionOp, OpName, OpNameRef}; -use crate::{ - ops::{NamedOp, OpType}, - types::TypeArg, - Extension, -}; +use crate::{ops::OpType, types::TypeArg, Extension}; use super::{op_def::SignatureFunc, ExtensionBuildError, ExtensionId, OpDef, SignatureError}; use delegate::delegate; @@ -29,22 +25,15 @@ pub enum OpLoadError { WrongExtension(ExtensionId, ExtensionId), } -impl NamedOp for T -where - for<'a> &'a T: Into<&'static str>, -{ - fn name(&self) -> OpName { - let s = self.into(); - s.into() - } -} - /// Traits implemented by types which can add themselves to [`Extension`]s as /// [`OpDef`]s or load themselves from an [`OpDef`]. /// /// Particularly useful with C-style enums that implement [strum::IntoEnumIterator], /// as then all definitions can be added to an extension at once. -pub trait MakeOpDef: NamedOp { +pub trait MakeOpDef { + /// TODO docs + fn opdef_name(&self) -> OpName; + /// Try to load one of the operations of this set from an [OpDef]. fn from_def(op_def: &OpDef) -> Result where @@ -70,7 +59,7 @@ pub trait MakeOpDef: NamedOp { /// Description of the operation. By default, the same as `self.name()`. fn description(&self) -> String { - self.name().to_string() + self.opdef_name().to_string() } /// Edit the opdef before finalising. By default does nothing. @@ -87,7 +76,7 @@ pub trait MakeOpDef: NamedOp { extension_ref: &Weak, ) -> Result<(), ExtensionBuildError> { let def = extension.add_op( - self.name(), + self.opdef_name(), self.description(), self.init_signature(extension_ref), extension_ref, @@ -150,7 +139,10 @@ pub trait HasDef: MakeExtensionOp { /// Traits implemented by types which can be loaded from [`ExtensionOp`]s, /// i.e. concrete instances of [`OpDef`]s, with defined type arguments. -pub trait MakeExtensionOp: NamedOp { +pub trait MakeExtensionOp { + /// The name of the operation + fn name(&self) -> OpName; + /// Try to load one of the operations of this set from an [OpDef]. fn from_extension_op(ext_op: &ExtensionOp) -> Result where @@ -188,6 +180,10 @@ pub trait MakeExtensionOp: NamedOp { /// Blanket implementation for non-polymorphic operations - [OpDef]s with no type parameters. impl MakeExtensionOp for T { + fn name(&self) -> OpName { + ::opdef_name(self) + } + #[inline] fn from_extension_op(ext_op: &ExtensionOp) -> Result where @@ -310,6 +306,10 @@ mod test { } impl MakeOpDef for DummyEnum { + fn opdef_name(&self) -> OpName { + <&Self as Into<&'static str>>::into(self).into() + } + fn init_signature(&self, _extension_ref: &Weak) -> SignatureFunc { Signature::new_endo(type_row![]).into() } @@ -366,7 +366,7 @@ mod test { let o = DummyEnum::Dumb; assert_eq!( - DummyEnum::from_def(EXT.get_op(&o.name()).unwrap()).unwrap(), + DummyEnum::from_def(EXT.get_op(&o.opdef_name()).unwrap()).unwrap(), o ); diff --git a/hugr-core/src/ops.rs b/hugr-core/src/ops.rs index 5b5dbc420..39409a180 100644 --- a/hugr-core/src/ops.rs +++ b/hugr-core/src/ops.rs @@ -355,7 +355,7 @@ pub type OpNameRef = str; #[enum_dispatch] /// Trait for setting name of OpType variants. // Separate to OpTrait to allow simple definition via impl_op_name -pub trait NamedOp { +pub(crate) trait NamedOp { /// The name of the operation. fn name(&self) -> OpName; } diff --git a/hugr-core/src/ops/controlflow.rs b/hugr-core/src/ops/controlflow.rs index 07c04f5c4..7f53783f9 100644 --- a/hugr-core/src/ops/controlflow.rs +++ b/hugr-core/src/ops/controlflow.rs @@ -6,8 +6,8 @@ use crate::types::{EdgeKind, Signature, Type, TypeRow}; use crate::Direction; use super::dataflow::{DataflowOpTrait, DataflowParent}; -use super::{impl_op_name, NamedOp, OpTrait, StaticTag}; -use super::{OpName, OpTag}; +use super::OpTag; +use super::{impl_op_name, OpTrait, StaticTag}; /// Tail-controlled loop. #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] @@ -173,17 +173,8 @@ pub struct ExitBlock { pub cfg_outputs: TypeRow, } -impl NamedOp for DataflowBlock { - fn name(&self) -> OpName { - "DataflowBlock".into() - } -} - -impl NamedOp for ExitBlock { - fn name(&self) -> OpName { - "ExitBlock".into() - } -} +impl_op_name!(DataflowBlock); +impl_op_name!(ExitBlock); impl StaticTag for DataflowBlock { const TAG: OpTag = OpTag::DataflowBlock; diff --git a/hugr-core/src/ops/custom.rs b/hugr-core/src/ops/custom.rs index 5f5a13427..246d19e82 100644 --- a/hugr-core/src/ops/custom.rs +++ b/hugr-core/src/ops/custom.rs @@ -136,6 +136,16 @@ impl ExtensionOp { pub fn extension_id(&self) -> &ExtensionId { self.def.extension_id() } + + /// Returns the unqualified name of the operation. + pub fn unqualified_name(&self) -> &OpNameRef { + self.def.name() + } + + /// Returns the unqualified name of the operation. + pub fn qualified_name(&self) -> OpName { + self.name() + } } impl From for OpaqueOp { @@ -220,7 +230,7 @@ pub struct OpaqueOp { signature: Signature, } -fn qualify_name(res_id: &ExtensionId, name: &OpNameRef) -> OpName { +pub(crate) fn qualify_name(res_id: &ExtensionId, name: &OpNameRef) -> OpName { format!("{}.{}", res_id, name).into() } @@ -251,9 +261,10 @@ impl OpaqueOp { impl NamedOp for OpaqueOp { /// The name of the operation. fn name(&self) -> OpName { - qualify_name(&self.extension, &self.name) + format!("OpaqueOp:{}", qualify_name(&self.extension, &self.name)).into() } } + impl OpaqueOp { /// Unique name of the operation. pub fn op_name(&self) -> &OpName { @@ -378,7 +389,7 @@ mod test { vec![TypeArg::Type { ty: usize_t() }], sig.clone(), ); - assert_eq!(op.name(), "res.op"); + assert_eq!(op.name(), "OpaqueOp:res.op"); assert_eq!(DataflowOpTrait::description(&op), "desc"); assert_eq!(op.args(), &[TypeArg::Type { ty: usize_t() }]); assert_eq!(op.signature().as_ref(), &sig); diff --git a/hugr-core/src/std_extensions/arithmetic/conversions.rs b/hugr-core/src/std_extensions/arithmetic/conversions.rs index ea1004d92..a9db6e792 100644 --- a/hugr-core/src/std_extensions/arithmetic/conversions.rs +++ b/hugr-core/src/std_extensions/arithmetic/conversions.rs @@ -9,8 +9,7 @@ use crate::extension::prelude::{bool_t, string_type, usize_t}; use crate::extension::simple_op::{HasConcrete, HasDef}; use crate::extension::simple_op::{MakeExtensionOp, MakeOpDef, MakeRegisteredOp, OpLoadError}; use crate::extension::{ExtensionId, OpDef, SignatureError, SignatureFunc}; -use crate::ops::OpName; -use crate::ops::{custom::ExtensionOp, NamedOp}; +use crate::ops::{ExtensionOp, OpName}; use crate::std_extensions::arithmetic::int_ops::int_polytype; use crate::std_extensions::arithmetic::int_types::int_type; use crate::types::{TypeArg, TypeRV}; @@ -45,6 +44,10 @@ pub enum ConvertOpDef { } impl MakeOpDef for ConvertOpDef { + fn opdef_name(&self) -> OpName { + <&Self as Into<&'static str>>::into(self).into() + } + fn from_def(op_def: &OpDef) -> Result { crate::extension::simple_op::try_from_name(op_def.name(), op_def.extension_id()) } @@ -146,13 +149,11 @@ impl ConvertOpType { } } -impl NamedOp for ConvertOpType { +impl MakeExtensionOp for ConvertOpType { fn name(&self) -> OpName { - self.def.name() + self.def.opdef_name() } -} -impl MakeExtensionOp for ConvertOpType { fn from_extension_op(ext_op: &ExtensionOp) -> Result { let def = ConvertOpDef::from_def(ext_op.def())?; def.instantiate(ext_op.args()) diff --git a/hugr-core/src/std_extensions/arithmetic/float_ops.rs b/hugr-core/src/std_extensions/arithmetic/float_ops.rs index f61353528..6f563bae2 100644 --- a/hugr-core/src/std_extensions/arithmetic/float_ops.rs +++ b/hugr-core/src/std_extensions/arithmetic/float_ops.rs @@ -11,6 +11,7 @@ use crate::{ simple_op::{MakeOpDef, MakeRegisteredOp, OpLoadError}, ExtensionId, OpDef, SignatureFunc, }, + ops::OpName, types::Signature, Extension, }; @@ -48,6 +49,10 @@ pub enum FloatOps { } impl MakeOpDef for FloatOps { + fn opdef_name(&self) -> OpName { + <&Self as Into<&'static str>>::into(self).into() + } + fn from_def(op_def: &OpDef) -> Result { crate::extension::simple_op::try_from_name(op_def.name(), op_def.extension_id()) } diff --git a/hugr-core/src/std_extensions/arithmetic/int_ops.rs b/hugr-core/src/std_extensions/arithmetic/int_ops.rs index 69939d4e1..6b7cebafd 100644 --- a/hugr-core/src/std_extensions/arithmetic/int_ops.rs +++ b/hugr-core/src/std_extensions/arithmetic/int_ops.rs @@ -9,7 +9,7 @@ use crate::extension::simple_op::{ }; use crate::extension::{CustomValidator, OpDef, SignatureFunc, ValidateJustArgs}; use crate::ops::custom::ExtensionOp; -use crate::ops::{NamedOp, OpName}; +use crate::ops::OpName; use crate::types::{FuncValueType, PolyFuncTypeRV, TypeRowRV}; use crate::utils::collect_array; @@ -100,6 +100,9 @@ pub enum IntOpDef { } impl MakeOpDef for IntOpDef { + fn opdef_name(&self) -> OpName { + <&Self as Into<&'static str>>::into(self).into() + } fn from_def(op_def: &OpDef) -> Result { crate::extension::simple_op::try_from_name(op_def.name(), op_def.extension_id()) } @@ -290,12 +293,11 @@ pub struct ConcreteIntOp { pub log_widths: Vec, } -impl NamedOp for ConcreteIntOp { +impl MakeExtensionOp for ConcreteIntOp { fn name(&self) -> OpName { - self.def.name() + self.def.opdef_name() } -} -impl MakeExtensionOp for ConcreteIntOp { + fn from_extension_op(ext_op: &ExtensionOp) -> Result { let def = IntOpDef::from_def(ext_op.def())?; def.instantiate(ext_op.args()) diff --git a/hugr-core/src/std_extensions/collections/array/array_clone.rs b/hugr-core/src/std_extensions/collections/array/array_clone.rs index c532da199..782185ac0 100644 --- a/hugr-core/src/std_extensions/collections/array/array_clone.rs +++ b/hugr-core/src/std_extensions/collections/array/array_clone.rs @@ -35,12 +35,6 @@ impl Default for GenericArrayCloneDef { } } -impl NamedOp for GenericArrayCloneDef { - fn name(&self) -> OpName { - ARRAY_CLONE_OP_ID - } -} - impl FromStr for GenericArrayCloneDef { type Err = (); @@ -70,6 +64,10 @@ impl GenericArrayCloneDef { } impl MakeOpDef for GenericArrayCloneDef { + fn opdef_name(&self) -> OpName { + ARRAY_CLONE_OP_ID + } + fn from_def(op_def: &OpDef) -> Result where Self: Sized, @@ -145,6 +143,10 @@ impl NamedOp for GenericArrayClone { } impl MakeExtensionOp for GenericArrayClone { + fn name(&self) -> OpName { + ARRAY_CLONE_OP_ID + } + fn from_extension_op(ext_op: &ExtensionOp) -> Result where Self: Sized, diff --git a/hugr-core/src/std_extensions/collections/array/array_conversion.rs b/hugr-core/src/std_extensions/collections/array/array_conversion.rs index 3cec4c3fe..75d5b4a9e 100644 --- a/hugr-core/src/std_extensions/collections/array/array_conversion.rs +++ b/hugr-core/src/std_extensions/collections/array/array_conversion.rs @@ -55,17 +55,6 @@ impl Default } } -impl NamedOp - for GenericArrayConvertDef -{ - fn name(&self) -> OpName { - match DIR { - INTO => format!("to_{}", OtherAK::TYPE_NAME).into(), - FROM => format!("from_{}", OtherAK::TYPE_NAME).into(), - } - } -} - impl FromStr for GenericArrayConvertDef { @@ -106,6 +95,14 @@ impl impl MakeOpDef for GenericArrayConvertDef { + fn opdef_name(&self) -> OpName { + match DIR { + INTO => format!("to_{}", OtherAK::TYPE_NAME).into(), + FROM => format!("from_{}", OtherAK::TYPE_NAME).into(), + } + + + } fn from_def(op_def: &OpDef) -> Result where Self: Sized, @@ -192,6 +189,10 @@ impl NamedOp impl MakeExtensionOp for GenericArrayConvert { + fn name(&self) -> OpName { + GenericArrayConvertDef::::new().opdef_name() + } + fn from_extension_op(ext_op: &ExtensionOp) -> Result where Self: Sized, diff --git a/hugr-core/src/std_extensions/collections/array/array_discard.rs b/hugr-core/src/std_extensions/collections/array/array_discard.rs index 7cdfc9b4f..de535c3a5 100644 --- a/hugr-core/src/std_extensions/collections/array/array_discard.rs +++ b/hugr-core/src/std_extensions/collections/array/array_discard.rs @@ -8,7 +8,7 @@ use crate::extension::simple_op::{ HasConcrete, HasDef, MakeExtensionOp, MakeOpDef, MakeRegisteredOp, OpLoadError, }; use crate::extension::{ExtensionId, OpDef, SignatureError, SignatureFunc, TypeDef}; -use crate::ops::{ExtensionOp, NamedOp, OpName}; +use crate::ops::{ExtensionOp, OpName}; use crate::types::type_param::{TypeArg, TypeParam}; use crate::types::{FuncValueType, PolyFuncTypeRV, Type, TypeBound}; use crate::{type_row, Extension}; @@ -35,12 +35,6 @@ impl Default for GenericArrayDiscardDef { } } -impl NamedOp for GenericArrayDiscardDef { - fn name(&self) -> OpName { - ARRAY_DISCARD_OP_ID - } -} - impl FromStr for GenericArrayDiscardDef { type Err = (); @@ -66,6 +60,10 @@ impl GenericArrayDiscardDef { } impl MakeOpDef for GenericArrayDiscardDef { + fn opdef_name(&self) -> OpName { + ARRAY_DISCARD_OP_ID + } + fn from_def(op_def: &OpDef) -> Result where Self: Sized, @@ -127,13 +125,11 @@ impl GenericArrayDiscard { } } -impl NamedOp for GenericArrayDiscard { +impl MakeExtensionOp for GenericArrayDiscard { fn name(&self) -> OpName { ARRAY_DISCARD_OP_ID } -} -impl MakeExtensionOp for GenericArrayDiscard { fn from_extension_op(ext_op: &ExtensionOp) -> Result where Self: Sized, diff --git a/hugr-core/src/std_extensions/collections/array/array_op.rs b/hugr-core/src/std_extensions/collections/array/array_op.rs index e5b63f855..18c421e7c 100644 --- a/hugr-core/src/std_extensions/collections/array/array_op.rs +++ b/hugr-core/src/std_extensions/collections/array/array_op.rs @@ -12,7 +12,7 @@ use crate::extension::simple_op::{ use crate::extension::{ ExtensionId, OpDef, SignatureError, SignatureFromArgs, SignatureFunc, TypeDef, }; -use crate::ops::{ExtensionOp, NamedOp, OpName}; +use crate::ops::{ExtensionOp, OpName}; use crate::type_row; use crate::types::type_param::{TypeArg, TypeParam}; use crate::types::{FuncValueType, PolyFuncTypeRV, Type, TypeBound}; @@ -89,7 +89,7 @@ impl SignatureFromArgs for GenericArrayOpDef { GenericArrayOpDef::_phantom(_, never) => match *never {}, _ => unreachable!( "Operation {} should not need custom computation.", - self.name() + self.opdef_name() ), }; Ok(poly_func_ty) @@ -189,6 +189,9 @@ impl GenericArrayOpDef { } impl MakeOpDef for GenericArrayOpDef { + fn opdef_name(&self) -> OpName { + <&Self as Into<&'static str>>::into(self).into() + } fn from_def(op_def: &OpDef) -> Result where Self: Sized, @@ -253,13 +256,11 @@ pub struct GenericArrayOp { pub size: u64, } -impl NamedOp for GenericArrayOp { +impl MakeExtensionOp for GenericArrayOp { fn name(&self) -> OpName { - self.def.name() + self.def.opdef_name() } -} -impl MakeExtensionOp for GenericArrayOp { fn from_extension_op(ext_op: &ExtensionOp) -> Result where Self: Sized, diff --git a/hugr-core/src/std_extensions/collections/array/array_repeat.rs b/hugr-core/src/std_extensions/collections/array/array_repeat.rs index 10a52308a..fc228532a 100644 --- a/hugr-core/src/std_extensions/collections/array/array_repeat.rs +++ b/hugr-core/src/std_extensions/collections/array/array_repeat.rs @@ -8,7 +8,7 @@ use crate::extension::simple_op::{ HasConcrete, HasDef, MakeExtensionOp, MakeOpDef, MakeRegisteredOp, OpLoadError, }; use crate::extension::{ExtensionId, OpDef, SignatureError, SignatureFunc, TypeDef}; -use crate::ops::{ExtensionOp, NamedOp, OpName}; +use crate::ops::{ExtensionOp, OpName}; use crate::types::type_param::{TypeArg, TypeParam}; use crate::types::{FuncValueType, PolyFuncTypeRV, Signature, Type, TypeBound}; use crate::Extension; @@ -35,12 +35,6 @@ impl Default for GenericArrayRepeatDef { } } -impl NamedOp for GenericArrayRepeatDef { - fn name(&self) -> OpName { - ARRAY_REPEAT_OP_ID - } -} - impl FromStr for GenericArrayRepeatDef { type Err = (); @@ -67,6 +61,10 @@ impl GenericArrayRepeatDef { } impl MakeOpDef for GenericArrayRepeatDef { + fn opdef_name(&self) -> OpName { + ARRAY_REPEAT_OP_ID + } + fn from_def(op_def: &OpDef) -> Result where Self: Sized, @@ -132,13 +130,11 @@ impl GenericArrayRepeat { } } -impl NamedOp for GenericArrayRepeat { +impl MakeExtensionOp for GenericArrayRepeat { fn name(&self) -> OpName { - ARRAY_REPEAT_OP_ID + GenericArrayRepeatDef::::default().opdef_name() } -} -impl MakeExtensionOp for GenericArrayRepeat { fn from_extension_op(ext_op: &ExtensionOp) -> Result where Self: Sized, diff --git a/hugr-core/src/std_extensions/collections/array/array_scan.rs b/hugr-core/src/std_extensions/collections/array/array_scan.rs index fcd06b628..598ce2b32 100644 --- a/hugr-core/src/std_extensions/collections/array/array_scan.rs +++ b/hugr-core/src/std_extensions/collections/array/array_scan.rs @@ -37,12 +37,6 @@ impl Default for GenericArrayScanDef { } } -impl NamedOp for GenericArrayScanDef { - fn name(&self) -> OpName { - ARRAY_SCAN_OP_ID - } -} - impl FromStr for GenericArrayScanDef { type Err = (); @@ -97,6 +91,10 @@ impl GenericArrayScanDef { } impl MakeOpDef for GenericArrayScanDef { + fn opdef_name(&self) -> OpName { + ARRAY_SCAN_OP_ID + } + fn from_def(op_def: &OpDef) -> Result where Self: Sized, @@ -177,6 +175,10 @@ impl NamedOp for GenericArrayScan { } impl MakeExtensionOp for GenericArrayScan { + fn name(&self) -> OpName { + GenericArrayScanDef::::default().opdef_name() + } + fn from_extension_op(ext_op: &ExtensionOp) -> Result where Self: Sized, diff --git a/hugr-core/src/std_extensions/collections/list.rs b/hugr-core/src/std_extensions/collections/list.rs index 3ffb4d9a0..b3cb65286 100644 --- a/hugr-core/src/std_extensions/collections/list.rs +++ b/hugr-core/src/std_extensions/collections/list.rs @@ -28,7 +28,7 @@ use crate::{ ExtensionId, SignatureError, TypeDef, TypeDefBound, }, ops::constant::CustomConst, - ops::{custom::ExtensionOp, NamedOp}, + ops::custom::ExtensionOp, types::{ type_param::{TypeArg, TypeParam}, CustomCheckFailure, CustomType, FuncValueType, PolyFuncTypeRV, Type, TypeBound, @@ -224,6 +224,10 @@ impl ListOp { } impl MakeOpDef for ListOp { + fn opdef_name(&self) -> OpName { + <&Self as Into<&'static str>>::into(self).into() + } + fn from_def(op_def: &OpDef) -> Result { crate::extension::simple_op::try_from_name(op_def.name(), op_def.extension_id()) } @@ -247,7 +251,7 @@ impl MakeOpDef for ListOp { extension_ref: &Weak, ) -> Result<(), ExtensionBuildError> { let sig = self.compute_signature(extension.get_type(&LIST_TYPENAME).unwrap()); - let def = extension.add_op(self.name(), self.description(), sig, extension_ref)?; + let def = extension.add_op(self.opdef_name(), self.description(), sig, extension_ref)?; self.post_opdef(def); @@ -333,21 +337,18 @@ pub struct ListOpInst { elem_type: Type, } -impl NamedOp for ListOpInst { +impl MakeExtensionOp for ListOpInst { fn name(&self) -> OpName { - let name: &str = self.op.into(); - name.into() + self.op.opdef_name() } -} -impl MakeExtensionOp for ListOpInst { fn from_extension_op( ext_op: &ExtensionOp, ) -> Result { let [TypeArg::Type { ty }] = ext_op.args() else { return Err(SignatureError::InvalidTypeArgs.into()); }; - let name = ext_op.def().name(); + let name = ext_op.unqualified_name(); let Ok(op) = ListOp::from_str(name) else { return Err(OpLoadError::NotMember(name.to_string())); }; diff --git a/hugr-core/src/std_extensions/collections/static_array.rs b/hugr-core/src/std_extensions/collections/static_array.rs index 05e5651a1..edacad81a 100644 --- a/hugr-core/src/std_extensions/collections/static_array.rs +++ b/hugr-core/src/std_extensions/collections/static_array.rs @@ -32,7 +32,7 @@ use crate::{ }, ops::{ constant::{maybe_hash_values, CustomConst, TryHash, ValueName}, - ExtensionOp, NamedOp, OpName, Value, + ExtensionOp, OpName, Value, }, types::{ type_param::{TypeArgError, TypeParam}, @@ -213,6 +213,11 @@ impl StaticArrayOpDef { } impl MakeOpDef for StaticArrayOpDef { + fn opdef_name(&self) -> OpName { + let s: &str = self.into(); + s.into() + } + fn from_def(op_def: &OpDef) -> Result where Self: Sized, @@ -255,7 +260,7 @@ impl MakeOpDef for StaticArrayOpDef { extension.get_type(&STATIC_ARRAY_TYPENAME).unwrap(), extension_ref, ); - let def = extension.add_op(self.name(), self.description(), sig, extension_ref)?; + let def = extension.add_op(self.opdef_name(), self.description(), sig, extension_ref)?; self.post_opdef(def); @@ -272,13 +277,11 @@ pub struct StaticArrayOp { pub elem_ty: Type, } -impl NamedOp for StaticArrayOp { +impl MakeExtensionOp for StaticArrayOp { fn name(&self) -> OpName { - self.def.name() + self.def.opdef_name() } -} -impl MakeExtensionOp for StaticArrayOp { fn from_extension_op(ext_op: &ExtensionOp) -> Result where Self: Sized, diff --git a/hugr-core/src/std_extensions/logic.rs b/hugr-core/src/std_extensions/logic.rs index 20977cb51..572649ac6 100644 --- a/hugr-core/src/std_extensions/logic.rs +++ b/hugr-core/src/std_extensions/logic.rs @@ -6,7 +6,7 @@ use strum::{EnumIter, EnumString, IntoStaticStr}; use crate::extension::{ConstFold, ConstFoldResult}; use crate::ops::constant::ValueName; -use crate::ops::Value; +use crate::ops::{OpName, Value}; use crate::types::Signature; use crate::{ extension::{ @@ -77,6 +77,10 @@ pub enum LogicOp { } impl MakeOpDef for LogicOp { + fn opdef_name(&self) -> OpName { + <&Self as Into<&'static str>>::into(self).into() + } + fn init_signature(&self, _extension_ref: &Weak) -> SignatureFunc { match self { LogicOp::And | LogicOp::Or | LogicOp::Eq | LogicOp::Xor => { @@ -168,7 +172,7 @@ pub(crate) mod test { use super::{extension, LogicOp}; use crate::{ extension::simple_op::{MakeOpDef, MakeRegisteredOp}, - ops::{NamedOp, Value}, + ops::Value, Extension, }; @@ -183,7 +187,7 @@ pub(crate) mod test { for op in LogicOp::iter() { assert_eq!( - LogicOp::from_def(r.get_op(&op.name()).unwrap(),).unwrap(), + LogicOp::from_def(r.get_op(op.into()).unwrap(),).unwrap(), op ); } diff --git a/hugr-core/src/std_extensions/ptr.rs b/hugr-core/src/std_extensions/ptr.rs index 6d77ae52d..16ea905bd 100644 --- a/hugr-core/src/std_extensions/ptr.rs +++ b/hugr-core/src/std_extensions/ptr.rs @@ -16,7 +16,7 @@ use crate::{ }, ExtensionId, OpDef, SignatureError, SignatureFunc, }, - ops::{custom::ExtensionOp, NamedOp}, + ops::custom::ExtensionOp, type_row, types::type_param::{TypeArg, TypeParam}, Extension, @@ -43,6 +43,11 @@ impl PtrOpDef { } impl MakeOpDef for PtrOpDef { + fn opdef_name(&self) -> OpName { + let s: &str = self.into(); + s.into() + } + fn from_def(op_def: &OpDef) -> Result where Self: Sized, @@ -145,13 +150,11 @@ impl PtrOp { } } -impl NamedOp for PtrOp { +impl MakeExtensionOp for PtrOp { fn name(&self) -> OpName { - self.def.name() + self.def.opdef_name() } -} -impl MakeExtensionOp for PtrOp { fn from_extension_op(ext_op: &ExtensionOp) -> Result { let def = PtrOpDef::from_def(ext_op.def())?; def.instantiate(ext_op.args()) @@ -225,7 +228,6 @@ pub(crate) mod test { use crate::ops::ExtensionOp; use crate::{ builder::{Dataflow, DataflowHugr}, - ops::NamedOp, std_extensions::arithmetic::int_types::INT_TYPES, }; use cool_asserts::assert_matches; @@ -234,8 +236,8 @@ pub(crate) mod test { use super::*; use crate::std_extensions::arithmetic::float_types::float64_type; - fn get_opdef(op: impl NamedOp) -> Option<&'static Arc> { - EXTENSION.get_op(&op.name()) + fn get_opdef(op: impl Into<&'static str>) -> Option<&'static Arc> { + EXTENSION.get_op(op.into()) } #[test] diff --git a/hugr-llvm/src/custom/extension_op.rs b/hugr-llvm/src/custom/extension_op.rs index f1391b1ea..7cf912ec7 100644 --- a/hugr-llvm/src/custom/extension_op.rs +++ b/hugr-llvm/src/custom/extension_op.rs @@ -84,10 +84,14 @@ impl<'a, H: HugrView> ExtensionOpMap<'a, H> { let handler = Rc::new(handler); for op in Op::iter() { let handler = handler.clone(); - self.extension_op(op.extension(), op.name().clone(), move |context, args| { - let op = Op::from_extension_op(&args.node())?; - handler(context, args, op) - }); + self.extension_op( + op.extension(), + op.opdef_name().clone(), + move |context, args| { + let op = Op::from_extension_op(&args.node())?; + handler(context, args, op) + }, + ); } } @@ -100,7 +104,10 @@ impl<'a, H: HugrView> ExtensionOpMap<'a, H> { args: EmitOpArgs<'c, '_, ExtensionOp, H>, ) -> Result<()> { let node = args.node(); - let key = (node.def().extension_id().clone(), node.def().name().clone()); + let key = ( + node.def().extension_id().clone(), + node.unqualified_name().into(), + ); let Some(handler) = self.0.get(&key) else { bail!("No extension could emit extension op: {key:?}") }; diff --git a/hugr-llvm/src/extension/collections/list.rs b/hugr-llvm/src/extension/collections/list.rs index 341a33466..78cb5b419 100644 --- a/hugr-llvm/src/extension/collections/list.rs +++ b/hugr-llvm/src/extension/collections/list.rs @@ -1,6 +1,7 @@ use anyhow::{bail, Ok, Result}; use hugr_core::{ - ops::{ExtensionOp, NamedOp}, + extension::simple_op::MakeExtensionOp as _, + ops::ExtensionOp, std_extensions::collections::list::{self, ListOp, ListValue}, types::{SumType, Type, TypeArg}, HugrView, Node, @@ -369,7 +370,7 @@ mod test { prelude::{self, qb_t, usize_t, ConstUsize}, ExtensionRegistry, }, - ops::{DataflowOpTrait, NamedOp, Value}, + ops::{DataflowOpTrait, Value}, std_extensions::collections::list::{self, list_type, ListOp, ListValue}, }; use rstest::rstest; @@ -389,6 +390,8 @@ mod test { #[case::insert(ListOp::insert)] #[case::length(ListOp::length)] fn test_list_emission(mut llvm_ctx: TestContext, #[case] op: ListOp) { + use hugr_core::extension::simple_op::MakeExtensionOp as _; + let ext_op = list::EXTENSION .instantiate_extension_op(op.name().as_ref(), [qb_t().into()]) .unwrap(); diff --git a/hugr-llvm/src/extension/int.rs b/hugr-llvm/src/extension/int.rs index 8a13372df..5f840bc91 100644 --- a/hugr-llvm/src/extension/int.rs +++ b/hugr-llvm/src/extension/int.rs @@ -1,6 +1,9 @@ use hugr_core::{ - extension::prelude::{sum_with_error, ConstError}, - ops::{constant::CustomConst, ExtensionOp, NamedOp, Value}, + extension::{ + prelude::{sum_with_error, ConstError}, + simple_op::MakeExtensionOp, + }, + ops::{constant::CustomConst, ExtensionOp, Value}, std_extensions::arithmetic::{ int_ops::IntOpDef, int_types::{self, ConstInt}, @@ -738,7 +741,7 @@ fn emit_int_op<'c, H: HugrView>( // panic if there's not exactly one nat arg pub(crate) fn get_width_arg>( args: &EmitOpArgs<'_, '_, ExtensionOp, H>, - op: &impl NamedOp, + op: &impl MakeExtensionOp, ) -> Result { let [TypeArg::BoundedNat { n: log_width }] = args.node.args() else { bail!("Expected exactly one BoundedNat parameter to {}", op.name()) @@ -1150,7 +1153,7 @@ mod test { use hugr_core::{ builder::{handle::Outputs, Dataflow, DataflowSubContainer, SubContainer}, extension::prelude::bool_t, - ops::{DataflowOpTrait, ExtensionOp, NamedOp}, + ops::{DataflowOpTrait, ExtensionOp}, std_extensions::arithmetic::{ int_ops::{self, IntOpDef}, int_types::{ConstInt, INT_TYPES}, @@ -1263,6 +1266,8 @@ mod test { #[case::idivmod_checked_u("idivmod_checked_u", &[6])] #[case::idivmod_checked_s("idivmod_checked_s", &[6])] fn test_emission(int_llvm_ctx: TestContext, #[case] op: IntOpDef, #[case] args: &[u8]) { + use hugr_core::extension::simple_op::MakeExtensionOp as _; + let mut insta = insta::Settings::clone_current(); insta.set_snapshot_suffix(format!( "{}_{}_{:?}", diff --git a/hugr-llvm/src/extension/logic.rs b/hugr-llvm/src/extension/logic.rs index 7f26e3bb5..db2261659 100644 --- a/hugr-llvm/src/extension/logic.rs +++ b/hugr-llvm/src/extension/logic.rs @@ -1,6 +1,6 @@ use hugr_core::{ extension::simple_op::MakeExtensionOp, - ops::{ExtensionOp, NamedOp, Value}, + ops::{ExtensionOp, Value}, std_extensions::logic::{self, LogicOp}, types::SumType, HugrView, Node, diff --git a/hugr-llvm/src/extension/prelude.rs b/hugr-llvm/src/extension/prelude.rs index e7863fe5b..04b4c37db 100644 --- a/hugr-llvm/src/extension/prelude.rs +++ b/hugr-llvm/src/extension/prelude.rs @@ -377,7 +377,7 @@ pub fn add_prelude_extensions<'a, H: HugrView + 'a>( args.outputs.finish(context.builder(), returns) } }) - .extension_op(prelude::PRELUDE_ID, generic::LOAD_NAT_OP_ID, { + .extension_op(prelude::PRELUDE_ID, generic::LOAD_NAT_OP_ID.clone(), { let pcg = pcg.clone(); move |context, args| { let load_nat = LoadNat::from_extension_op(args.node().as_ref())?; diff --git a/hugr-llvm/src/utils/fat.rs b/hugr-llvm/src/utils/fat.rs index 5deeb4bf0..398fcff62 100644 --- a/hugr-llvm/src/utils/fat.rs +++ b/hugr-llvm/src/utils/fat.rs @@ -2,12 +2,12 @@ //! //! We define a trait [FatExt], an extension trait for [HugrView]. It provides //! methods that return [FatNode]s rather than [Node]s. -use std::{cmp::Ordering, hash::Hash, marker::PhantomData, ops::Deref}; +use std::{cmp::Ordering, fmt, hash::Hash, marker::PhantomData, ops::Deref}; use hugr_core::{ core::HugrNode, hugr::{views::HierarchyView, HugrError}, - ops::{DataflowBlock, ExitBlock, Input, NamedOp, OpType, Output, CFG}, + ops::{DataflowBlock, ExitBlock, Input, OpType, Output, CFG}, types::Type, Hugr, HugrView, IncomingPort, Node, NodeIndex, OutgoingPort, }; @@ -307,12 +307,12 @@ impl Clone for FatNode<'_, OT, H> { } } -impl std::fmt::Display for FatNode<'_, OT, H, H::Node> +impl fmt::Display for FatNode<'_, OT, H, H::Node> where for<'a> &'a OpType: TryInto<&'a OT>, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_fmt(format_args!("N<{}:{}>", self.as_ref().name(), self.node)) + f.write_fmt(format_args!("N<{}:{}>", self.as_ref(), self.node)) } } diff --git a/hugr-passes/src/const_fold/test.rs b/hugr-passes/src/const_fold/test.rs index dcdc4df0a..ea85c5999 100644 --- a/hugr-passes/src/const_fold/test.rs +++ b/hugr-passes/src/const_fold/test.rs @@ -1555,7 +1555,7 @@ fn test_cfg( &ConstInt::new_u(4, unfolded_cst).unwrap().into() ); } else if let Some(op) = hugr.get_optype(ch).as_extension_op() { - assert_eq!(op.def().name(), "iadd"); + assert_eq!(op.unqualified_name(), "iadd"); } } } diff --git a/hugr-passes/src/force_order.rs b/hugr-passes/src/force_order.rs index cbb637b2a..95aa5bcb1 100644 --- a/hugr-passes/src/force_order.rs +++ b/hugr-passes/src/force_order.rs @@ -2,8 +2,12 @@ use std::{cmp::Reverse, collections::BinaryHeap, iter}; use hugr_core::{ - hugr::{hugrmut::HugrMut, HugrError}, - ops::{NamedOp, OpTag, OpTrait}, + hugr::{ + hugrmut::HugrMut, + views::{HierarchyView, SiblingGraph}, + HugrError, + }, + ops::{OpTag, OpTrait}, types::EdgeKind, HugrView as _, Node, }; @@ -54,35 +58,26 @@ pub fn force_order_by_key, K: Ord>( for dp in dataflow_parents { // we filter out the input and output nodes from the topological sort let [i, o] = hugr.get_io(dp).unwrap(); - let ordered_nodes = { - let rank = |n| rank(hugr, hugr.from_portgraph_node(n)); - let sg = hugr.region_portgraph(dp); - let petgraph = NodeFiltered::from_fn(&sg, |x| { - let x = hugr.from_portgraph_node(x); - x != dp && x != i && x != o - }); - ForceOrder::new(&petgraph, &rank) - .iter(&petgraph) - .map(|x| hugr.from_portgraph_node(x)) - .filter(|&x| { - let expected_edge = Some(EdgeKind::StateOrder); - let optype = hugr.get_optype(x); - if optype.other_input() == expected_edge - || optype.other_output() == expected_edge - { - assert_eq!( - optype.other_input(), - optype.other_output(), - "Optype does not have both input and output order edge: {}", - optype.name() - ); - true - } else { - false - } - }) - .collect_vec() - }; + let rank = |n| rank(hugr, n); + let sg = SiblingGraph::::try_new(hugr, dp)?; + let petgraph = NodeFiltered::from_fn(sg.as_petgraph(), |x| x != dp && x != i && x != o); + let ordered_nodes = ForceOrder::new(&petgraph, &rank) + .iter(&petgraph) + .filter(|&x| { + let expected_edge = Some(EdgeKind::StateOrder); + let optype = hugr.get_optype(x); + if optype.other_input() == expected_edge || optype.other_output() == expected_edge { + assert_eq!( + optype.other_input(), + optype.other_output(), + "Optype does not have both input and output order edge: {optype}" + ); + true + } else { + false + } + }) + .collect_vec(); // we iterate over the topologically sorted nodes, prepending the input // node and suffixing the output node. diff --git a/hugr-passes/src/replace_types.rs b/hugr-passes/src/replace_types.rs index b5b98e887..641142832 100644 --- a/hugr-passes/src/replace_types.rs +++ b/hugr-passes/src/replace_types.rs @@ -548,7 +548,6 @@ pub mod handlers; #[derive(Clone, Hash, PartialEq, Eq)] struct OpHashWrapper { - ext_name: ExtensionId, op_name: String, // Only because SmolStr not in hugr-passes yet args: Vec, } @@ -556,8 +555,7 @@ struct OpHashWrapper { impl From<&ExtensionOp> for OpHashWrapper { fn from(op: &ExtensionOp) -> Self { Self { - ext_name: op.def().extension_id().clone(), - op_name: op.def().name().to_string(), + op_name: op.qualified_name().to_string(), args: op.args().to_vec(), } } @@ -605,13 +603,12 @@ mod test { use hugr_core::hugr::{IdentList, ValidationError}; use hugr_core::ops::constant::CustomConst; use hugr_core::ops::constant::OpaqueValue; - use hugr_core::ops::{ExtensionOp, NamedOp, OpTrait, OpType, Tag, Value}; - use hugr_core::std_extensions::arithmetic::int_types::ConstInt; - use hugr_core::std_extensions::arithmetic::{conversions::ConvertOpDef, int_types::INT_TYPES}; - use hugr_core::std_extensions::collections::array::Array; - use hugr_core::std_extensions::collections::array::{ArrayKind, GenericArrayValue}; - use hugr_core::std_extensions::collections::list::{ - list_type, list_type_def, ListOp, ListValue, + use hugr_core::ops::{ExtensionOp, OpTrait, OpType, Tag, Value}; + use hugr_core::std_extensions::arithmetic::conversions::ConvertOpDef; + use hugr_core::std_extensions::arithmetic::int_types::{ConstInt, INT_TYPES}; + use hugr_core::std_extensions::collections::{ + array::{array_type, array_type_def, ArrayOp, ArrayOpDef, ArrayValue}, + list::{list_type, list_type_def, ListOp, ListValue}, }; use hugr_core::std_extensions::collections::value_array::{ value_array_type, VArrayOp, VArrayOpDef, VArrayValue, ValueArray, @@ -792,7 +789,7 @@ mod test { let ext_ops = h.nodes().filter_map(|n| h.get_optype(n).as_extension_op()); assert_eq!( - ext_ops.map(|e| e.def().name()).sorted().collect_vec(), + ext_ops.map(|e| e.unqualified_name()).sorted().collect_vec(), ["get", "itousize", "lowered_read_bool", "panic",] ); } @@ -839,7 +836,7 @@ mod test { assert_eq!( ext_ops .iter() - .map(|e| e.def().name()) + .map(|x| x.unqualified_name()) .sorted() .collect_vec(), ["get", "itousize", "panic"] @@ -1030,7 +1027,7 @@ mod test { assert_eq!( h.nodes() .filter_map(|n| h.get_optype(n).as_extension_op()) - .map(ExtensionOp::name) + .map(|x| x.qualified_name()) .sorted() .collect_vec(), ["NoBoundsCheck.read", "collections.list.get"] @@ -1118,7 +1115,7 @@ mod test { let ext_op_names = h .nodes() .filter_map(|n| h.get_optype(n).as_extension_op()) - .map(|e| e.def().name()) + .map(|e| e.unqualified_name()) .sorted() .collect_vec(); assert_eq!(ext_op_names, ["get", "itousize", "panic",]); diff --git a/hugr-passes/src/replace_types/linearize.rs b/hugr-passes/src/replace_types/linearize.rs index 859fefbf7..cd331f3d0 100644 --- a/hugr-passes/src/replace_types/linearize.rs +++ b/hugr-passes/src/replace_types/linearize.rs @@ -365,7 +365,7 @@ mod test { }; use hugr_core::hugr::views::{DescendantsGraph, HierarchyView}; use hugr_core::ops::handle::NodeHandle; - use hugr_core::ops::{DataflowOpTrait, ExtensionOp, NamedOp, OpName, OpType}; + use hugr_core::ops::{DataflowOpTrait, ExtensionOp, OpName, OpType}; use hugr_core::std_extensions::arithmetic::int_types::INT_TYPES; use hugr_core::std_extensions::collections::value_array::{ value_array_type, value_array_type_def, VArrayOpDef, VArrayRepeat, VArrayScan, @@ -474,7 +474,7 @@ mod test { let ext_ops = h.nodes().filter_map(|n| h.get_optype(n).as_extension_op()); let mut counts = HashMap::::new(); for e in ext_ops { - *counts.entry(e.name()).or_default() += 1; + *counts.entry(e.qualified_name()).or_default() += 1; } assert_eq!( counts, @@ -534,7 +534,11 @@ mod test { let ext_ops = DescendantsGraph::::try_new(&h, case1) .unwrap() .nodes() - .filter_map(|n| h.get_optype(n).as_extension_op().map(ExtensionOp::name)) + .filter_map(|n| { + h.get_optype(n) + .as_extension_op() + .map(ExtensionOp::qualified_name) + }) .collect_vec(); assert_eq!(ext_ops, expected_ext_ops); } diff --git a/hugr-passes/src/untuple.rs b/hugr-passes/src/untuple.rs index 1c9be1c75..ac13bfb41 100644 --- a/hugr-passes/src/untuple.rs +++ b/hugr-passes/src/untuple.rs @@ -3,12 +3,12 @@ use std::collections::VecDeque; use hugr_core::builder::{DFGBuilder, Dataflow, DataflowHugr}; -use hugr_core::extension::prelude::{MakeTuple, TupleOpDef}; +use hugr_core::extension::prelude::{MakeTuple, UnpackTuple}; use hugr_core::hugr::hugrmut::HugrMut; use hugr_core::hugr::views::sibling_subgraph::TopoConvexChecker; use hugr_core::hugr::views::SiblingSubgraph; use hugr_core::hugr::SimpleReplacementError; -use hugr_core::ops::{NamedOp, OpTrait, OpType}; +use hugr_core::ops::{OpTrait, OpType}; use hugr_core::types::Type; use hugr_core::{HugrView, Node, SimpleReplacement}; use itertools::Itertools; @@ -141,14 +141,14 @@ impl ComposablePass for UntuplePass { /// /// Boilerplate required due to https://github.com/CQCL/hugr/issues/1496 fn is_make_tuple(optype: &OpType) -> bool { - optype.name() == format!("prelude.{}", TupleOpDef::MakeTuple.name()) + optype.cast::().is_some() } /// Returns true if the given optype is an UnpackTuple operation. /// /// Boilerplate required due to https://github.com/CQCL/hugr/issues/1496 fn is_unpack_tuple(optype: &OpType) -> bool { - optype.name() == format!("prelude.{}", TupleOpDef::UnpackTuple.name()) + optype.cast::().is_some() } /// If this is a MakeTuple operation followed by some number of UnpackTuple operations From b16c0cf28e6310317000d746927c409fee0a1882 Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Tue, 29 Apr 2025 12:26:33 +0100 Subject: [PATCH 2/5] tweak --- hugr-core/src/ops/custom.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/hugr-core/src/ops/custom.rs b/hugr-core/src/ops/custom.rs index 246d19e82..c1610aa57 100644 --- a/hugr-core/src/ops/custom.rs +++ b/hugr-core/src/ops/custom.rs @@ -137,12 +137,13 @@ impl ExtensionOp { self.def.extension_id() } - /// Returns the unqualified name of the operation. + /// Returns the unqualified name of the operation. e.g. 'iadd' + /// pub fn unqualified_name(&self) -> &OpNameRef { self.def.name() } - /// Returns the unqualified name of the operation. + /// Returns the unqualified name of the operation. e.g. 'arithmetic.iadd' pub fn qualified_name(&self) -> OpName { self.name() } From 14ac03d298bd94abf724bc36added5329f667791 Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Tue, 29 Apr 2025 12:27:53 +0100 Subject: [PATCH 3/5] fix --- hugr-core/src/ops/custom.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hugr-core/src/ops/custom.rs b/hugr-core/src/ops/custom.rs index c1610aa57..7fea373ee 100644 --- a/hugr-core/src/ops/custom.rs +++ b/hugr-core/src/ops/custom.rs @@ -231,7 +231,7 @@ pub struct OpaqueOp { signature: Signature, } -pub(crate) fn qualify_name(res_id: &ExtensionId, name: &OpNameRef) -> OpName { +fn qualify_name(res_id: &ExtensionId, name: &OpNameRef) -> OpName { format!("{}.{}", res_id, name).into() } From fa2611127d8e667ea10921113b065bd4706451c1 Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Tue, 6 May 2025 13:12:30 +0100 Subject: [PATCH 4/5] address feedback, rename -> op{,def}_id --- hugr-core/src/export.rs | 2 +- hugr-core/src/extension.rs | 4 +- hugr-core/src/extension/prelude.rs | 38 ++++++++--------- hugr-core/src/extension/prelude/generic.rs | 8 ++-- hugr-core/src/extension/resolution/ops.rs | 2 +- hugr-core/src/extension/simple_op.rs | 41 ++++++++++++------- hugr-core/src/hugr/validate.rs | 4 +- hugr-core/src/ops/custom.rs | 22 ++++++---- .../std_extensions/arithmetic/conversions.rs | 8 ++-- .../std_extensions/arithmetic/float_ops.rs | 2 +- .../src/std_extensions/arithmetic/int_ops.rs | 6 +-- .../collections/array/array_clone.rs | 8 ++-- .../collections/array/array_conversion.rs | 12 +++--- .../collections/array/array_discard.rs | 8 ++-- .../collections/array/array_op.rs | 10 ++--- .../collections/array/array_repeat.rs | 13 +++--- .../collections/array/array_scan.rs | 16 +++----- .../src/std_extensions/collections/list.rs | 12 +++--- .../collections/static_array.rs | 11 +++-- hugr-core/src/std_extensions/logic.rs | 2 +- hugr-core/src/std_extensions/ptr.rs | 6 +-- hugr-llvm/src/custom/extension_op.rs | 4 +- hugr-llvm/src/extension/collections/list.rs | 6 +-- hugr-llvm/src/extension/int.rs | 9 ++-- hugr-llvm/src/extension/logic.rs | 10 ++--- hugr-passes/src/const_fold/test.rs | 2 +- hugr-passes/src/linearize_array.rs | 9 ++-- hugr-passes/src/replace_types.rs | 16 ++++---- hugr-passes/src/replace_types/linearize.rs | 4 +- 29 files changed, 151 insertions(+), 144 deletions(-) diff --git a/hugr-core/src/export.rs b/hugr-core/src/export.rs index 42e04629b..042350cf7 100644 --- a/hugr-core/src/export.rs +++ b/hugr-core/src/export.rs @@ -447,7 +447,7 @@ impl<'a> Context<'a> { } OpType::OpaqueOp(op) => { - let node = self.make_named_global_ref(op.extension(), op.op_name()); + let node = self.make_named_global_ref(op.extension(), op.unqualified_id()); let params = self .bump .alloc_slice_fill_iter(op.args().iter().map(|arg| self.export_type_arg(arg))); diff --git a/hugr-core/src/extension.rs b/hugr-core/src/extension.rs index 4300c74ad..71629a73a 100644 --- a/hugr-core/src/extension.rs +++ b/hugr-core/src/extension.rs @@ -466,8 +466,8 @@ trait CustomConcrete { impl CustomConcrete for OpaqueOp { type Identifier = OpName; - fn def_name(&self) -> &OpName { - self.op_name() + fn def_name(&self) -> &Self::Identifier { + self.unqualified_id() } fn type_args(&self) -> &[TypeArg] { diff --git a/hugr-core/src/extension/prelude.rs b/hugr-core/src/extension/prelude.rs index cbbca3759..d2720150d 100644 --- a/hugr-core/src/extension/prelude.rs +++ b/hugr-core/src/extension/prelude.rs @@ -597,8 +597,8 @@ impl ConstFold for TupleOpDef { } impl MakeOpDef for TupleOpDef { - fn opdef_name(&self) -> OpName { - <&Self as Into<&'static str>>::into(self).into() + fn opdef_id(&self) -> OpName { + <&'static str>::from(self).into() } fn init_signature(&self, _extension_ref: &Weak) -> SignatureFunc { @@ -655,8 +655,8 @@ impl MakeTuple { } impl MakeExtensionOp for MakeTuple { - fn name(&self) -> OpName { - TupleOpDef::MakeTuple.opdef_name() + fn op_id(&self) -> OpName { + TupleOpDef::MakeTuple.opdef_id() } fn from_extension_op(ext_op: &crate::ops::ExtensionOp) -> Result @@ -665,9 +665,7 @@ impl MakeExtensionOp for MakeTuple { { let def = TupleOpDef::from_def(ext_op.def())?; if def != TupleOpDef::MakeTuple { - return Err(OpLoadError::NotMember( - ext_op.unqualified_name().to_string(), - ))?; + return Err(OpLoadError::NotMember(ext_op.unqualified_id().to_string()))?; } let [TypeArg::Sequence { elems }] = ext_op.args() else { return Err(SignatureError::InvalidTypeArgs)?; @@ -717,8 +715,8 @@ impl UnpackTuple { } impl MakeExtensionOp for UnpackTuple { - fn name(&self) -> OpName { - TupleOpDef::UnpackTuple.opdef_name() + fn op_id(&self) -> OpName { + TupleOpDef::UnpackTuple.opdef_id() } fn from_extension_op(ext_op: &crate::ops::ExtensionOp) -> Result @@ -727,9 +725,7 @@ impl MakeExtensionOp for UnpackTuple { { let def = TupleOpDef::from_def(ext_op.def())?; if def != TupleOpDef::UnpackTuple { - return Err(OpLoadError::NotMember( - ext_op.unqualified_name().to_string(), - ))?; + return Err(OpLoadError::NotMember(ext_op.unqualified_id().to_string()))?; } let [TypeArg::Sequence { elems }] = ext_op.args() else { return Err(SignatureError::InvalidTypeArgs)?; @@ -776,7 +772,7 @@ impl std::str::FromStr for NoopDef { type Err = (); fn from_str(s: &str) -> Result { - if s == NoopDef.name() { + if s == NoopDef.op_id() { Ok(Self) } else { Err(()) @@ -785,7 +781,7 @@ impl std::str::FromStr for NoopDef { } impl MakeOpDef for NoopDef { - fn opdef_name(&self) -> OpName { + fn opdef_id(&self) -> OpName { NOOP_OP_ID } @@ -845,8 +841,8 @@ impl Default for Noop { } impl MakeExtensionOp for Noop { - fn name(&self) -> OpName { - NoopDef.name() + fn op_id(&self) -> OpName { + NoopDef.op_id() } fn from_extension_op(ext_op: &crate::ops::ExtensionOp) -> Result @@ -886,7 +882,7 @@ impl std::str::FromStr for BarrierDef { type Err = (); fn from_str(s: &str) -> Result { - if s == BarrierDef.name() { + if s == BarrierDef.op_id() { Ok(Self) } else { Err(()) @@ -895,7 +891,7 @@ impl std::str::FromStr for BarrierDef { } impl MakeOpDef for BarrierDef { - fn opdef_name(&self) -> OpName { + fn opdef_id(&self) -> OpName { BARRIER_OP_ID } @@ -945,13 +941,13 @@ impl Barrier { impl NamedOp for Barrier { fn name(&self) -> OpName { - BarrierDef.name() + BarrierDef.op_id() } } impl MakeExtensionOp for Barrier { - fn name(&self) -> OpName { - BarrierDef.name() + fn op_id(&self) -> OpName { + BarrierDef.op_id() } fn from_extension_op(ext_op: &crate::ops::ExtensionOp) -> Result diff --git a/hugr-core/src/extension/prelude/generic.rs b/hugr-core/src/extension/prelude/generic.rs index 2c93908f7..f1caac3bc 100644 --- a/hugr-core/src/extension/prelude/generic.rs +++ b/hugr-core/src/extension/prelude/generic.rs @@ -37,7 +37,7 @@ impl FromStr for LoadNatDef { type Err = (); fn from_str(s: &str) -> Result { - if s == Self.name() { + if s == Self.op_id() { Ok(Self) } else { Err(()) @@ -65,7 +65,7 @@ impl ConstFold for LoadNatDef { } impl MakeOpDef for LoadNatDef { - fn opdef_name(&self) -> OpName { + fn opdef_id(&self) -> OpName { LOAD_NAT_OP_ID.clone() } @@ -118,8 +118,8 @@ impl LoadNat { } impl MakeExtensionOp for LoadNat { - fn name(&self) -> OpName { - LoadNatDef.opdef_name() + fn op_id(&self) -> OpName { + LoadNatDef.opdef_id() } fn from_extension_op(ext_op: &ExtensionOp) -> Result diff --git a/hugr-core/src/extension/resolution/ops.rs b/hugr-core/src/extension/resolution/ops.rs index 3ab1491b4..a5a41c1c7 100644 --- a/hugr-core/src/extension/resolution/ops.rs +++ b/hugr-core/src/extension/resolution/ops.rs @@ -73,7 +73,7 @@ pub(crate) fn resolve_op_extensions<'e>( // Fail if the Extension is not in the registry, or if the Extension was // found but did not have the expected operation. let extension = extension.expect("OpaqueOp should have an extension"); - let Some(def) = extension.get_op(opaque.op_name()) else { + let Some(def) = extension.get_op(opaque.unqualified_id()) else { return Err(OpaqueOpError::OpNotFoundInExtension { node, op: opaque.name().clone(), diff --git a/hugr-core/src/extension/simple_op.rs b/hugr-core/src/extension/simple_op.rs index 03f622f3b..301655786 100644 --- a/hugr-core/src/extension/simple_op.rs +++ b/hugr-core/src/extension/simple_op.rs @@ -30,9 +30,18 @@ pub enum OpLoadError { /// /// Particularly useful with C-style enums that implement [strum::IntoEnumIterator], /// as then all definitions can be added to an extension at once. +/// +/// [MakeExtensionOp] has a blanket impl for types that impl [MakeOpDef]. pub trait MakeOpDef { - /// TODO docs - fn opdef_name(&self) -> OpName; + /// The [OpDef::name] which will be used when `Self` is added to an [Extension] + /// or when `Self` is loaded from an [OpDef]. + /// + /// This identifer must be unique within the extension with which the + /// [OpDef] is registered. An [ExtensionOp] instantiating this [OpDef] will + /// report `self.opdef_id()` as its [ExtensionOp::unqualified_id]. + /// + /// [MakeExtensionOp::op_id] must match this function. + fn opdef_id(&self) -> OpName; /// Try to load one of the operations of this set from an [OpDef]. fn from_def(op_def: &OpDef) -> Result @@ -57,9 +66,9 @@ pub trait MakeOpDef { self.init_signature(&self.extension_ref()) } - /// Description of the operation. By default, the same as `self.name()`. + /// Description of the operation. By default, the same as `self.opdef_id()`. fn description(&self) -> String { - self.opdef_name().to_string() + self.opdef_id().to_string() } /// Edit the opdef before finalising. By default does nothing. @@ -76,7 +85,7 @@ pub trait MakeOpDef { extension_ref: &Weak, ) -> Result<(), ExtensionBuildError> { let def = extension.add_op( - self.opdef_name(), + self.opdef_id(), self.description(), self.init_signature(extension_ref), extension_ref, @@ -140,8 +149,12 @@ pub trait HasDef: MakeExtensionOp { /// Traits implemented by types which can be loaded from [`ExtensionOp`]s, /// i.e. concrete instances of [`OpDef`]s, with defined type arguments. pub trait MakeExtensionOp { - /// The name of the operation - fn name(&self) -> OpName; + /// The [OpDef::name] of [ExtensionOp]s from which `Self` can be loaded. + /// + /// This identifer must be unique within the extension with which the + /// [OpDef] is registered. An [ExtensionOp] instantiating this [OpDef] will + /// report `self.opdef_id()` as its [ExtensionOp::unqualified_id]. + fn op_id(&self) -> OpName; /// Try to load one of the operations of this set from an [OpDef]. fn from_extension_op(ext_op: &ExtensionOp) -> Result @@ -180,8 +193,8 @@ pub trait MakeExtensionOp { /// Blanket implementation for non-polymorphic operations - [OpDef]s with no type parameters. impl MakeExtensionOp for T { - fn name(&self) -> OpName { - ::opdef_name(self) + fn op_id(&self) -> OpName { + self.opdef_id() } #[inline] @@ -239,7 +252,7 @@ impl RegisteredOp { /// Generate an [OpType]. pub fn to_extension_op(&self) -> Option { ExtensionOp::new( - self.extension.upgrade()?.get_op(&self.name())?.clone(), + self.extension.upgrade()?.get_op(&self.op_id())?.clone(), self.type_args(), ) .ok() @@ -248,7 +261,7 @@ impl RegisteredOp { delegate! { to self.op { /// Name of the operation - derived from strum serialization. - pub fn name(&self) -> OpName; + pub fn op_id(&self) -> OpName; /// Any type args which define this operation. Default is no type arguments. pub fn type_args(&self) -> Vec; } @@ -306,8 +319,8 @@ mod test { } impl MakeOpDef for DummyEnum { - fn opdef_name(&self) -> OpName { - <&Self as Into<&'static str>>::into(self).into() + fn opdef_id(&self) -> OpName { + <&'static str>::from(self).into() } fn init_signature(&self, _extension_ref: &Weak) -> SignatureFunc { @@ -366,7 +379,7 @@ mod test { let o = DummyEnum::Dumb; assert_eq!( - DummyEnum::from_def(EXT.get_op(&o.opdef_name()).unwrap()).unwrap(), + DummyEnum::from_def(EXT.get_op(&o.opdef_id()).unwrap()).unwrap(), o ); diff --git a/hugr-core/src/hugr/validate.rs b/hugr-core/src/hugr/validate.rs index 31b34b044..2c75a53aa 100644 --- a/hugr-core/src/hugr/validate.rs +++ b/hugr-core/src/hugr/validate.rs @@ -102,7 +102,7 @@ impl<'a> ValidationContext<'a> { if let OpType::OpaqueOp(opaque) = op_type { Err(OpaqueOpError::UnresolvedOp( node, - opaque.op_name().clone(), + opaque.unqualified_id().clone(), opaque.extension().clone(), ))?; } @@ -536,7 +536,7 @@ impl<'a> ValidationContext<'a> { OpType::OpaqueOp(opaque) => { Err(OpaqueOpError::UnresolvedOp( node, - opaque.op_name().clone(), + opaque.qualified_id(), opaque.extension().clone(), ))?; } diff --git a/hugr-core/src/ops/custom.rs b/hugr-core/src/ops/custom.rs index 7fea373ee..0e81d07a8 100644 --- a/hugr-core/src/ops/custom.rs +++ b/hugr-core/src/ops/custom.rs @@ -137,15 +137,15 @@ impl ExtensionOp { self.def.extension_id() } - /// Returns the unqualified name of the operation. e.g. 'iadd' + /// Returns the unqualified id of the operation. e.g. 'iadd' /// - pub fn unqualified_name(&self) -> &OpNameRef { + pub fn unqualified_id(&self) -> &OpNameRef { self.def.name() } - /// Returns the unqualified name of the operation. e.g. 'arithmetic.iadd' - pub fn qualified_name(&self) -> OpName { - self.name() + /// Returns the qualified id of the operation. e.g. 'arithmetic.iadd' + pub fn qualified_id(&self) -> OpName { + qualify_name(self.extension_id(), self.unqualified_id()) } } @@ -177,7 +177,7 @@ impl Eq for ExtensionOp {} impl NamedOp for ExtensionOp { /// The name of the operation. fn name(&self) -> OpName { - qualify_name(self.def.extension_id(), self.def.name()) + self.qualified_id() } } @@ -260,18 +260,22 @@ impl OpaqueOp { } impl NamedOp for OpaqueOp { - /// The name of the operation. fn name(&self) -> OpName { - format!("OpaqueOp:{}", qualify_name(&self.extension, &self.name)).into() + format!("OpaqueOp:{}", self.qualified_id()).into() } } impl OpaqueOp { /// Unique name of the operation. - pub fn op_name(&self) -> &OpName { + pub fn unqualified_id(&self) -> &OpName { &self.name } + /// Unique name of the operation. + pub fn qualified_id(&self) -> OpName { + qualify_name(self.extension(), self.unqualified_id()) + } + /// Type arguments. pub fn args(&self) -> &[TypeArg] { &self.args diff --git a/hugr-core/src/std_extensions/arithmetic/conversions.rs b/hugr-core/src/std_extensions/arithmetic/conversions.rs index a9db6e792..4e0dc050c 100644 --- a/hugr-core/src/std_extensions/arithmetic/conversions.rs +++ b/hugr-core/src/std_extensions/arithmetic/conversions.rs @@ -44,8 +44,8 @@ pub enum ConvertOpDef { } impl MakeOpDef for ConvertOpDef { - fn opdef_name(&self) -> OpName { - <&Self as Into<&'static str>>::into(self).into() + fn opdef_id(&self) -> OpName { + <&'static str>::from(self).into() } fn from_def(op_def: &OpDef) -> Result { @@ -150,8 +150,8 @@ impl ConvertOpType { } impl MakeExtensionOp for ConvertOpType { - fn name(&self) -> OpName { - self.def.opdef_name() + fn op_id(&self) -> OpName { + self.def.opdef_id() } fn from_extension_op(ext_op: &ExtensionOp) -> Result { diff --git a/hugr-core/src/std_extensions/arithmetic/float_ops.rs b/hugr-core/src/std_extensions/arithmetic/float_ops.rs index 6f563bae2..3e4878081 100644 --- a/hugr-core/src/std_extensions/arithmetic/float_ops.rs +++ b/hugr-core/src/std_extensions/arithmetic/float_ops.rs @@ -49,7 +49,7 @@ pub enum FloatOps { } impl MakeOpDef for FloatOps { - fn opdef_name(&self) -> OpName { + fn opdef_id(&self) -> OpName { <&Self as Into<&'static str>>::into(self).into() } diff --git a/hugr-core/src/std_extensions/arithmetic/int_ops.rs b/hugr-core/src/std_extensions/arithmetic/int_ops.rs index 6b7cebafd..552014625 100644 --- a/hugr-core/src/std_extensions/arithmetic/int_ops.rs +++ b/hugr-core/src/std_extensions/arithmetic/int_ops.rs @@ -100,7 +100,7 @@ pub enum IntOpDef { } impl MakeOpDef for IntOpDef { - fn opdef_name(&self) -> OpName { + fn opdef_id(&self) -> OpName { <&Self as Into<&'static str>>::into(self).into() } fn from_def(op_def: &OpDef) -> Result { @@ -294,8 +294,8 @@ pub struct ConcreteIntOp { } impl MakeExtensionOp for ConcreteIntOp { - fn name(&self) -> OpName { - self.def.opdef_name() + fn op_id(&self) -> OpName { + self.def.opdef_id() } fn from_extension_op(ext_op: &ExtensionOp) -> Result { diff --git a/hugr-core/src/std_extensions/collections/array/array_clone.rs b/hugr-core/src/std_extensions/collections/array/array_clone.rs index 782185ac0..fee399d44 100644 --- a/hugr-core/src/std_extensions/collections/array/array_clone.rs +++ b/hugr-core/src/std_extensions/collections/array/array_clone.rs @@ -64,7 +64,7 @@ impl GenericArrayCloneDef { } impl MakeOpDef for GenericArrayCloneDef { - fn opdef_name(&self) -> OpName { + fn opdef_id(&self) -> OpName { ARRAY_CLONE_OP_ID } @@ -102,7 +102,7 @@ impl MakeOpDef for GenericArrayCloneDef { extension_ref: &Weak, ) -> Result<(), crate::extension::ExtensionBuildError> { let sig = self.signature_from_def(extension.get_type(&AK::TYPE_NAME).unwrap()); - let def = extension.add_op(self.name(), self.description(), sig, extension_ref)?; + let def = extension.add_op(self.opdef_id(), self.description(), sig, extension_ref)?; self.post_opdef(def); Ok(()) } @@ -143,8 +143,8 @@ impl NamedOp for GenericArrayClone { } impl MakeExtensionOp for GenericArrayClone { - fn name(&self) -> OpName { - ARRAY_CLONE_OP_ID + fn op_id(&self) -> OpName { + GenericArrayCloneDef::::default().opdef_id() } fn from_extension_op(ext_op: &ExtensionOp) -> Result diff --git a/hugr-core/src/std_extensions/collections/array/array_conversion.rs b/hugr-core/src/std_extensions/collections/array/array_conversion.rs index 75d5b4a9e..6946ff6a7 100644 --- a/hugr-core/src/std_extensions/collections/array/array_conversion.rs +++ b/hugr-core/src/std_extensions/collections/array/array_conversion.rs @@ -62,7 +62,7 @@ impl FromStr fn from_str(s: &str) -> Result { let def = GenericArrayConvertDef::new(); - if s == def.name() { + if s == def.opdef_id() { Ok(def) } else { Err(()) @@ -95,13 +95,11 @@ impl impl MakeOpDef for GenericArrayConvertDef { - fn opdef_name(&self) -> OpName { + fn opdef_id(&self) -> OpName { match DIR { INTO => format!("to_{}", OtherAK::TYPE_NAME).into(), FROM => format!("from_{}", OtherAK::TYPE_NAME).into(), } - - } fn from_def(op_def: &OpDef) -> Result where @@ -140,7 +138,7 @@ impl MakeOpDef extension_ref: &Weak, ) -> Result<(), crate::extension::ExtensionBuildError> { let sig = self.signature_from_def(extension.get_type(&AK::TYPE_NAME).unwrap()); - let def = extension.add_op(self.name(), self.description(), sig, extension_ref)?; + let def = extension.add_op(self.opdef_id(), self.description(), sig, extension_ref)?; self.post_opdef(def); Ok(()) } @@ -189,8 +187,8 @@ impl NamedOp impl MakeExtensionOp for GenericArrayConvert { - fn name(&self) -> OpName { - GenericArrayConvertDef::::new().opdef_name() + fn op_id(&self) -> OpName { + GenericArrayConvertDef::::new().opdef_id() } fn from_extension_op(ext_op: &ExtensionOp) -> Result diff --git a/hugr-core/src/std_extensions/collections/array/array_discard.rs b/hugr-core/src/std_extensions/collections/array/array_discard.rs index de535c3a5..594a3b82a 100644 --- a/hugr-core/src/std_extensions/collections/array/array_discard.rs +++ b/hugr-core/src/std_extensions/collections/array/array_discard.rs @@ -60,7 +60,7 @@ impl GenericArrayDiscardDef { } impl MakeOpDef for GenericArrayDiscardDef { - fn opdef_name(&self) -> OpName { + fn opdef_id(&self) -> OpName { ARRAY_DISCARD_OP_ID } @@ -98,7 +98,7 @@ impl MakeOpDef for GenericArrayDiscardDef { extension_ref: &Weak, ) -> Result<(), crate::extension::ExtensionBuildError> { let sig = self.signature_from_def(extension.get_type(&AK::TYPE_NAME).unwrap()); - let def = extension.add_op(self.name(), self.description(), sig, extension_ref)?; + let def = extension.add_op(self.opdef_id(), self.description(), sig, extension_ref)?; self.post_opdef(def); Ok(()) } @@ -126,8 +126,8 @@ impl GenericArrayDiscard { } impl MakeExtensionOp for GenericArrayDiscard { - fn name(&self) -> OpName { - ARRAY_DISCARD_OP_ID + fn op_id(&self) -> OpName { + GenericArrayDiscardDef::::default().opdef_id() } fn from_extension_op(ext_op: &ExtensionOp) -> Result diff --git a/hugr-core/src/std_extensions/collections/array/array_op.rs b/hugr-core/src/std_extensions/collections/array/array_op.rs index 18c421e7c..92e4ce641 100644 --- a/hugr-core/src/std_extensions/collections/array/array_op.rs +++ b/hugr-core/src/std_extensions/collections/array/array_op.rs @@ -89,7 +89,7 @@ impl SignatureFromArgs for GenericArrayOpDef { GenericArrayOpDef::_phantom(_, never) => match *never {}, _ => unreachable!( "Operation {} should not need custom computation.", - self.opdef_name() + self.opdef_id() ), }; Ok(poly_func_ty) @@ -189,7 +189,7 @@ impl GenericArrayOpDef { } impl MakeOpDef for GenericArrayOpDef { - fn opdef_name(&self) -> OpName { + fn opdef_id(&self) -> OpName { <&Self as Into<&'static str>>::into(self).into() } fn from_def(op_def: &OpDef) -> Result @@ -237,7 +237,7 @@ impl MakeOpDef for GenericArrayOpDef { ) -> Result<(), crate::extension::ExtensionBuildError> { let sig = self.signature_from_def(extension.get_type(&AK::TYPE_NAME).unwrap(), extension_ref); - let def = extension.add_op(self.name(), self.description(), sig, extension_ref)?; + let def = extension.add_op(self.opdef_id(), self.description(), sig, extension_ref)?; self.post_opdef(def); @@ -257,8 +257,8 @@ pub struct GenericArrayOp { } impl MakeExtensionOp for GenericArrayOp { - fn name(&self) -> OpName { - self.def.opdef_name() + fn op_id(&self) -> OpName { + self.def.opdef_id() } fn from_extension_op(ext_op: &ExtensionOp) -> Result diff --git a/hugr-core/src/std_extensions/collections/array/array_repeat.rs b/hugr-core/src/std_extensions/collections/array/array_repeat.rs index fc228532a..e5e18b374 100644 --- a/hugr-core/src/std_extensions/collections/array/array_repeat.rs +++ b/hugr-core/src/std_extensions/collections/array/array_repeat.rs @@ -39,8 +39,9 @@ impl FromStr for GenericArrayRepeatDef { type Err = (); fn from_str(s: &str) -> Result { - if s == ARRAY_REPEAT_OP_ID { - Ok(GenericArrayRepeatDef::new()) + let candidate = Self::default(); + if s == candidate.opdef_id() { + Ok(candidate) } else { Err(()) } @@ -61,7 +62,7 @@ impl GenericArrayRepeatDef { } impl MakeOpDef for GenericArrayRepeatDef { - fn opdef_name(&self) -> OpName { + fn opdef_id(&self) -> OpName { ARRAY_REPEAT_OP_ID } @@ -101,7 +102,7 @@ impl MakeOpDef for GenericArrayRepeatDef { extension_ref: &Weak, ) -> Result<(), crate::extension::ExtensionBuildError> { let sig = self.signature_from_def(extension.get_type(&AK::TYPE_NAME).unwrap()); - let def = extension.add_op(self.name(), self.description(), sig, extension_ref)?; + let def = extension.add_op(self.opdef_id(), self.description(), sig, extension_ref)?; self.post_opdef(def); @@ -131,8 +132,8 @@ impl GenericArrayRepeat { } impl MakeExtensionOp for GenericArrayRepeat { - fn name(&self) -> OpName { - GenericArrayRepeatDef::::default().opdef_name() + fn op_id(&self) -> OpName { + GenericArrayRepeatDef::::default().opdef_id() } fn from_extension_op(ext_op: &ExtensionOp) -> Result diff --git a/hugr-core/src/std_extensions/collections/array/array_scan.rs b/hugr-core/src/std_extensions/collections/array/array_scan.rs index 598ce2b32..596af5aad 100644 --- a/hugr-core/src/std_extensions/collections/array/array_scan.rs +++ b/hugr-core/src/std_extensions/collections/array/array_scan.rs @@ -10,7 +10,7 @@ use crate::extension::simple_op::{ HasConcrete, HasDef, MakeExtensionOp, MakeOpDef, MakeRegisteredOp, OpLoadError, }; use crate::extension::{ExtensionId, OpDef, SignatureError, SignatureFunc, TypeDef}; -use crate::ops::{ExtensionOp, NamedOp, OpName}; +use crate::ops::{ExtensionOp, OpName}; use crate::types::type_param::{TypeArg, TypeParam}; use crate::types::{FuncTypeBase, PolyFuncTypeRV, RowVariable, Type, TypeBound, TypeRV}; use crate::Extension; @@ -91,7 +91,7 @@ impl GenericArrayScanDef { } impl MakeOpDef for GenericArrayScanDef { - fn opdef_name(&self) -> OpName { + fn opdef_id(&self) -> OpName { ARRAY_SCAN_OP_ID } @@ -133,7 +133,7 @@ impl MakeOpDef for GenericArrayScanDef { extension_ref: &Weak, ) -> Result<(), crate::extension::ExtensionBuildError> { let sig = self.signature_from_def(extension.get_type(&AK::TYPE_NAME).unwrap()); - let def = extension.add_op(self.name(), self.description(), sig, extension_ref)?; + let def = extension.add_op(self.op_id(), self.description(), sig, extension_ref)?; self.post_opdef(def); @@ -168,15 +168,9 @@ impl GenericArrayScan { } } -impl NamedOp for GenericArrayScan { - fn name(&self) -> OpName { - ARRAY_SCAN_OP_ID - } -} - impl MakeExtensionOp for GenericArrayScan { - fn name(&self) -> OpName { - GenericArrayScanDef::::default().opdef_name() + fn op_id(&self) -> OpName { + GenericArrayScanDef::::default().opdef_id() } fn from_extension_op(ext_op: &ExtensionOp) -> Result diff --git a/hugr-core/src/std_extensions/collections/list.rs b/hugr-core/src/std_extensions/collections/list.rs index b3cb65286..6a717ea19 100644 --- a/hugr-core/src/std_extensions/collections/list.rs +++ b/hugr-core/src/std_extensions/collections/list.rs @@ -224,7 +224,7 @@ impl ListOp { } impl MakeOpDef for ListOp { - fn opdef_name(&self) -> OpName { + fn opdef_id(&self) -> OpName { <&Self as Into<&'static str>>::into(self).into() } @@ -251,7 +251,7 @@ impl MakeOpDef for ListOp { extension_ref: &Weak, ) -> Result<(), ExtensionBuildError> { let sig = self.compute_signature(extension.get_type(&LIST_TYPENAME).unwrap()); - let def = extension.add_op(self.opdef_name(), self.description(), sig, extension_ref)?; + let def = extension.add_op(self.opdef_id(), self.description(), sig, extension_ref)?; self.post_opdef(def); @@ -338,8 +338,8 @@ pub struct ListOpInst { } impl MakeExtensionOp for ListOpInst { - fn name(&self) -> OpName { - self.op.opdef_name() + fn op_id(&self) -> OpName { + self.op.opdef_id() } fn from_extension_op( @@ -348,7 +348,7 @@ impl MakeExtensionOp for ListOpInst { let [TypeArg::Type { ty }] = ext_op.args() else { return Err(SignatureError::InvalidTypeArgs.into()); }; - let name = ext_op.unqualified_name(); + let name = ext_op.unqualified_id(); let Ok(op) = ListOp::from_str(name) else { return Err(OpLoadError::NotMember(name.to_string())); }; @@ -370,7 +370,7 @@ impl ListOpInst { /// Convert this list operation to an [`ExtensionOp`] by providing a /// registry to validate the element type against. pub fn to_extension_op(self) -> Option { - ExtensionOp::new(EXTENSION.get_op(&self.name())?.clone(), self.type_args()).ok() + ExtensionOp::new(EXTENSION.get_op(&self.op_id())?.clone(), self.type_args()).ok() } } diff --git a/hugr-core/src/std_extensions/collections/static_array.rs b/hugr-core/src/std_extensions/collections/static_array.rs index edacad81a..fa45707f1 100644 --- a/hugr-core/src/std_extensions/collections/static_array.rs +++ b/hugr-core/src/std_extensions/collections/static_array.rs @@ -213,9 +213,8 @@ impl StaticArrayOpDef { } impl MakeOpDef for StaticArrayOpDef { - fn opdef_name(&self) -> OpName { - let s: &str = self.into(); - s.into() + fn opdef_id(&self) -> OpName { + <&'static str>::from(self).into() } fn from_def(op_def: &OpDef) -> Result @@ -260,7 +259,7 @@ impl MakeOpDef for StaticArrayOpDef { extension.get_type(&STATIC_ARRAY_TYPENAME).unwrap(), extension_ref, ); - let def = extension.add_op(self.opdef_name(), self.description(), sig, extension_ref)?; + let def = extension.add_op(self.opdef_id(), self.description(), sig, extension_ref)?; self.post_opdef(def); @@ -278,8 +277,8 @@ pub struct StaticArrayOp { } impl MakeExtensionOp for StaticArrayOp { - fn name(&self) -> OpName { - self.def.opdef_name() + fn op_id(&self) -> OpName { + self.def.opdef_id() } fn from_extension_op(ext_op: &ExtensionOp) -> Result diff --git a/hugr-core/src/std_extensions/logic.rs b/hugr-core/src/std_extensions/logic.rs index 572649ac6..62323f14c 100644 --- a/hugr-core/src/std_extensions/logic.rs +++ b/hugr-core/src/std_extensions/logic.rs @@ -77,7 +77,7 @@ pub enum LogicOp { } impl MakeOpDef for LogicOp { - fn opdef_name(&self) -> OpName { + fn opdef_id(&self) -> OpName { <&Self as Into<&'static str>>::into(self).into() } diff --git a/hugr-core/src/std_extensions/ptr.rs b/hugr-core/src/std_extensions/ptr.rs index 16ea905bd..50bdee15b 100644 --- a/hugr-core/src/std_extensions/ptr.rs +++ b/hugr-core/src/std_extensions/ptr.rs @@ -43,7 +43,7 @@ impl PtrOpDef { } impl MakeOpDef for PtrOpDef { - fn opdef_name(&self) -> OpName { + fn opdef_id(&self) -> OpName { let s: &str = self.into(); s.into() } @@ -151,8 +151,8 @@ impl PtrOp { } impl MakeExtensionOp for PtrOp { - fn name(&self) -> OpName { - self.def.opdef_name() + fn op_id(&self) -> OpName { + self.def.opdef_id() } fn from_extension_op(ext_op: &ExtensionOp) -> Result { diff --git a/hugr-llvm/src/custom/extension_op.rs b/hugr-llvm/src/custom/extension_op.rs index 7cf912ec7..d297cc186 100644 --- a/hugr-llvm/src/custom/extension_op.rs +++ b/hugr-llvm/src/custom/extension_op.rs @@ -86,7 +86,7 @@ impl<'a, H: HugrView> ExtensionOpMap<'a, H> { let handler = handler.clone(); self.extension_op( op.extension(), - op.opdef_name().clone(), + op.opdef_id().clone(), move |context, args| { let op = Op::from_extension_op(&args.node())?; handler(context, args, op) @@ -106,7 +106,7 @@ impl<'a, H: HugrView> ExtensionOpMap<'a, H> { let node = args.node(); let key = ( node.def().extension_id().clone(), - node.unqualified_name().into(), + node.unqualified_id().into(), ); let Some(handler) = self.0.get(&key) else { bail!("No extension could emit extension op: {key:?}") diff --git a/hugr-llvm/src/extension/collections/list.rs b/hugr-llvm/src/extension/collections/list.rs index 78cb5b419..2f9729bd1 100644 --- a/hugr-llvm/src/extension/collections/list.rs +++ b/hugr-llvm/src/extension/collections/list.rs @@ -283,7 +283,7 @@ fn emit_list_op<'c, H: HugrView>( args.outputs .finish(ctx.builder(), vec![list, length.into()])?; } - _ => bail!("Collections: unimplemented op: {}", op.name()), + _ => bail!("Collections: unimplemented op: {}", op.op_id()), } Ok(()) } @@ -393,7 +393,7 @@ mod test { use hugr_core::extension::simple_op::MakeExtensionOp as _; let ext_op = list::EXTENSION - .instantiate_extension_op(op.name().as_ref(), [qb_t().into()]) + .instantiate_extension_op(op.op_id().as_ref(), [qb_t().into()]) .unwrap(); let es = ExtensionRegistry::new([list::EXTENSION.to_owned(), prelude::PRELUDE.to_owned()]); es.validate().unwrap(); @@ -410,7 +410,7 @@ mod test { }); llvm_ctx.add_extensions(CodegenExtsBuilder::add_default_prelude_extensions); llvm_ctx.add_extensions(CodegenExtsBuilder::add_default_list_extensions); - check_emission!(op.name().as_str(), hugr, llvm_ctx); + check_emission!(op.op_id().as_str(), hugr, llvm_ctx); } #[rstest] diff --git a/hugr-llvm/src/extension/int.rs b/hugr-llvm/src/extension/int.rs index 5f840bc91..bed721e06 100644 --- a/hugr-llvm/src/extension/int.rs +++ b/hugr-llvm/src/extension/int.rs @@ -733,7 +733,7 @@ fn emit_int_op<'c, H: HugrView>( |_| Ok(arg), )?]) }), - _ => Err(anyhow!("IntOpEmitter: unimplemented op: {}", op.name())), + _ => Err(anyhow!("IntOpEmitter: unimplemented op: {}", op.op_id())), } } @@ -744,7 +744,10 @@ pub(crate) fn get_width_arg>( op: &impl MakeExtensionOp, ) -> Result { let [TypeArg::BoundedNat { n: log_width }] = args.node.args() else { - bail!("Expected exactly one BoundedNat parameter to {}", op.name()) + bail!( + "Expected exactly one BoundedNat parameter to {}", + op.op_id() + ) }; Ok(*log_width) } @@ -1272,7 +1275,7 @@ mod test { insta.set_snapshot_suffix(format!( "{}_{}_{:?}", insta.snapshot_suffix().unwrap_or(""), - op.name(), + op.op_id(), args, )); let concrete = match *args { diff --git a/hugr-llvm/src/extension/logic.rs b/hugr-llvm/src/extension/logic.rs index db2261659..9797f9aff 100644 --- a/hugr-llvm/src/extension/logic.rs +++ b/hugr-llvm/src/extension/logic.rs @@ -56,11 +56,11 @@ fn emit_logic_op<'c, H: HugrView>( pub fn add_logic_extensions<'a, H: HugrView + 'a>( cem: CodegenExtsBuilder<'a, H>, ) -> CodegenExtsBuilder<'a, H> { - cem.extension_op(logic::EXTENSION_ID, LogicOp::Eq.name(), emit_logic_op) - .extension_op(logic::EXTENSION_ID, LogicOp::And.name(), emit_logic_op) - .extension_op(logic::EXTENSION_ID, LogicOp::Or.name(), emit_logic_op) - .extension_op(logic::EXTENSION_ID, LogicOp::Not.name(), emit_logic_op) - .extension_op(logic::EXTENSION_ID, LogicOp::Xor.name(), emit_logic_op) // Added Xor + cem.extension_op(logic::EXTENSION_ID, LogicOp::Eq.op_id(), emit_logic_op) + .extension_op(logic::EXTENSION_ID, LogicOp::And.op_id(), emit_logic_op) + .extension_op(logic::EXTENSION_ID, LogicOp::Or.op_id(), emit_logic_op) + .extension_op(logic::EXTENSION_ID, LogicOp::Not.op_id(), emit_logic_op) + .extension_op(logic::EXTENSION_ID, LogicOp::Xor.op_id(), emit_logic_op) // Added Xor } impl<'a, H: HugrView + 'a> CodegenExtsBuilder<'a, H> { diff --git a/hugr-passes/src/const_fold/test.rs b/hugr-passes/src/const_fold/test.rs index ea85c5999..bd78de497 100644 --- a/hugr-passes/src/const_fold/test.rs +++ b/hugr-passes/src/const_fold/test.rs @@ -1555,7 +1555,7 @@ fn test_cfg( &ConstInt::new_u(4, unfolded_cst).unwrap().into() ); } else if let Some(op) = hugr.get_optype(ch).as_extension_op() { - assert_eq!(op.unqualified_name(), "iadd"); + assert_eq!(op.unqualified_id(), "iadd"); } } } diff --git a/hugr-passes/src/linearize_array.rs b/hugr-passes/src/linearize_array.rs index 56ba7a7d0..1c39dbe6b 100644 --- a/hugr-passes/src/linearize_array.rs +++ b/hugr-passes/src/linearize_array.rs @@ -3,10 +3,9 @@ use hugr_core::{ extension::{ prelude::Noop, - simple_op::{HasConcrete, MakeRegisteredOp}, + simple_op::{HasConcrete, MakeOpDef as _, MakeRegisteredOp}, }, hugr::hugrmut::HugrMut, - ops::NamedOp, std_extensions::collections::{ array::{ array_type_def, array_type_parametric, Array, ArrayKind, ArrayOpDef, ArrayRepeatDef, @@ -55,7 +54,7 @@ impl Default for LinearizeArrayPass { }); for op_def in ArrayOpDef::iter() { pass.replace_parametrized_op( - value_array::EXTENSION.get_op(&op_def.name()).unwrap(), + value_array::EXTENSION.get_op(&op_def.opdef_id()).unwrap(), move |args| { // `get` is only allowed for copyable elements. Assuming the Hugr was // valid when we started, the only way for the element to become linear @@ -96,7 +95,7 @@ impl Default for LinearizeArrayPass { ); pass.replace_parametrized_op( value_array::EXTENSION - .get_op(&VArrayFromArrayDef::new().name()) + .get_op(&VArrayFromArrayDef::new().opdef_id()) .unwrap(), |args| { let array_ty = array_type_parametric(args[0].clone(), args[1].clone()).unwrap(); @@ -107,7 +106,7 @@ impl Default for LinearizeArrayPass { ); pass.replace_parametrized_op( value_array::EXTENSION - .get_op(&VArrayToArrayDef::new().name()) + .get_op(&VArrayToArrayDef::new().opdef_id()) .unwrap(), |args| { let array_ty = array_type_parametric(args[0].clone(), args[1].clone()).unwrap(); diff --git a/hugr-passes/src/replace_types.rs b/hugr-passes/src/replace_types.rs index 641142832..7c5f4baf3 100644 --- a/hugr-passes/src/replace_types.rs +++ b/hugr-passes/src/replace_types.rs @@ -555,7 +555,7 @@ struct OpHashWrapper { impl From<&ExtensionOp> for OpHashWrapper { fn from(op: &ExtensionOp) -> Self { Self { - op_name: op.qualified_name().to_string(), + op_name: op.qualified_id().to_string(), args: op.args().to_vec(), } } @@ -606,9 +606,9 @@ mod test { use hugr_core::ops::{ExtensionOp, OpTrait, OpType, Tag, Value}; use hugr_core::std_extensions::arithmetic::conversions::ConvertOpDef; use hugr_core::std_extensions::arithmetic::int_types::{ConstInt, INT_TYPES}; - use hugr_core::std_extensions::collections::{ - array::{array_type, array_type_def, ArrayOp, ArrayOpDef, ArrayValue}, - list::{list_type, list_type_def, ListOp, ListValue}, + use hugr_core::std_extensions::collections::array::{Array, ArrayKind, GenericArrayValue}; + use hugr_core::std_extensions::collections::list::{ + list_type, list_type_def, ListOp, ListValue, }; use hugr_core::std_extensions::collections::value_array::{ value_array_type, VArrayOp, VArrayOpDef, VArrayValue, ValueArray, @@ -789,7 +789,7 @@ mod test { let ext_ops = h.nodes().filter_map(|n| h.get_optype(n).as_extension_op()); assert_eq!( - ext_ops.map(|e| e.unqualified_name()).sorted().collect_vec(), + ext_ops.map(|e| e.unqualified_id()).sorted().collect_vec(), ["get", "itousize", "lowered_read_bool", "panic",] ); } @@ -836,7 +836,7 @@ mod test { assert_eq!( ext_ops .iter() - .map(|x| x.unqualified_name()) + .map(|x| x.unqualified_id()) .sorted() .collect_vec(), ["get", "itousize", "panic"] @@ -1027,7 +1027,7 @@ mod test { assert_eq!( h.nodes() .filter_map(|n| h.get_optype(n).as_extension_op()) - .map(|x| x.qualified_name()) + .map(|x| x.qualified_id()) .sorted() .collect_vec(), ["NoBoundsCheck.read", "collections.list.get"] @@ -1115,7 +1115,7 @@ mod test { let ext_op_names = h .nodes() .filter_map(|n| h.get_optype(n).as_extension_op()) - .map(|e| e.unqualified_name()) + .map(|e| e.unqualified_id()) .sorted() .collect_vec(); assert_eq!(ext_op_names, ["get", "itousize", "panic",]); diff --git a/hugr-passes/src/replace_types/linearize.rs b/hugr-passes/src/replace_types/linearize.rs index cd331f3d0..b00521691 100644 --- a/hugr-passes/src/replace_types/linearize.rs +++ b/hugr-passes/src/replace_types/linearize.rs @@ -474,7 +474,7 @@ mod test { let ext_ops = h.nodes().filter_map(|n| h.get_optype(n).as_extension_op()); let mut counts = HashMap::::new(); for e in ext_ops { - *counts.entry(e.qualified_name()).or_default() += 1; + *counts.entry(e.qualified_id()).or_default() += 1; } assert_eq!( counts, @@ -537,7 +537,7 @@ mod test { .filter_map(|n| { h.get_optype(n) .as_extension_op() - .map(ExtensionOp::qualified_name) + .map(ExtensionOp::qualified_id) }) .collect_vec(); assert_eq!(ext_ops, expected_ext_ops); From dca9eeb55add6401f380dd3eaa1b9a458410c86c Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Tue, 6 May 2025 14:55:57 +0100 Subject: [PATCH 5/5] tweak --- hugr-core/src/std_extensions/collections/array/array_op.rs | 3 ++- hugr-core/src/std_extensions/logic.rs | 2 +- hugr-core/src/std_extensions/ptr.rs | 3 +-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/hugr-core/src/std_extensions/collections/array/array_op.rs b/hugr-core/src/std_extensions/collections/array/array_op.rs index 92e4ce641..74bc8ae36 100644 --- a/hugr-core/src/std_extensions/collections/array/array_op.rs +++ b/hugr-core/src/std_extensions/collections/array/array_op.rs @@ -190,8 +190,9 @@ impl GenericArrayOpDef { impl MakeOpDef for GenericArrayOpDef { fn opdef_id(&self) -> OpName { - <&Self as Into<&'static str>>::into(self).into() + <&'static str>::from(self).into() } + fn from_def(op_def: &OpDef) -> Result where Self: Sized, diff --git a/hugr-core/src/std_extensions/logic.rs b/hugr-core/src/std_extensions/logic.rs index 62323f14c..f7a5990ee 100644 --- a/hugr-core/src/std_extensions/logic.rs +++ b/hugr-core/src/std_extensions/logic.rs @@ -78,7 +78,7 @@ pub enum LogicOp { impl MakeOpDef for LogicOp { fn opdef_id(&self) -> OpName { - <&Self as Into<&'static str>>::into(self).into() + <&'static str>::from(self).into() } fn init_signature(&self, _extension_ref: &Weak) -> SignatureFunc { diff --git a/hugr-core/src/std_extensions/ptr.rs b/hugr-core/src/std_extensions/ptr.rs index 50bdee15b..9115bef74 100644 --- a/hugr-core/src/std_extensions/ptr.rs +++ b/hugr-core/src/std_extensions/ptr.rs @@ -44,8 +44,7 @@ impl PtrOpDef { impl MakeOpDef for PtrOpDef { fn opdef_id(&self) -> OpName { - let s: &str = self.into(); - s.into() + <&'static str>::from(self).into() } fn from_def(op_def: &OpDef) -> Result