Skip to content

Commit a1924e2

Browse files
committed
address feedback, rename -> op{,def}_id
1 parent 3aa8287 commit a1924e2

File tree

25 files changed

+130
-121
lines changed

25 files changed

+130
-121
lines changed

hugr-core/src/export.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ impl<'a> Context<'a> {
447447
}
448448

449449
OpType::OpaqueOp(op) => {
450-
let node = self.make_named_global_ref(op.extension(), op.op_name());
450+
let node = self.make_named_global_ref(op.extension(), op.unqualified_id());
451451
let params = self
452452
.bump
453453
.alloc_slice_fill_iter(op.args().iter().map(|arg| self.export_type_arg(arg)));

hugr-core/src/extension.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,8 @@ trait CustomConcrete {
466466
impl CustomConcrete for OpaqueOp {
467467
type Identifier = OpName;
468468

469-
fn def_name(&self) -> &OpName {
470-
self.op_name()
469+
fn def_name(&self) -> &Self::Identifier {
470+
self.unqualified_id()
471471
}
472472

473473
fn type_args(&self) -> &[TypeArg] {

hugr-core/src/extension/prelude.rs

+17-21
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,8 @@ impl ConstFold for TupleOpDef {
597597
}
598598

599599
impl MakeOpDef for TupleOpDef {
600-
fn opdef_name(&self) -> OpName {
601-
<&Self as Into<&'static str>>::into(self).into()
600+
fn opdef_id(&self) -> OpName {
601+
<&'static str>::from(self).into()
602602
}
603603

604604
fn init_signature(&self, _extension_ref: &Weak<Extension>) -> SignatureFunc {
@@ -655,8 +655,8 @@ impl MakeTuple {
655655
}
656656

657657
impl MakeExtensionOp for MakeTuple {
658-
fn name(&self) -> OpName {
659-
TupleOpDef::MakeTuple.opdef_name()
658+
fn op_id(&self) -> OpName {
659+
TupleOpDef::MakeTuple.opdef_id()
660660
}
661661

662662
fn from_extension_op(ext_op: &crate::ops::ExtensionOp) -> Result<Self, OpLoadError>
@@ -665,9 +665,7 @@ impl MakeExtensionOp for MakeTuple {
665665
{
666666
let def = TupleOpDef::from_def(ext_op.def())?;
667667
if def != TupleOpDef::MakeTuple {
668-
return Err(OpLoadError::NotMember(
669-
ext_op.unqualified_name().to_string(),
670-
))?;
668+
return Err(OpLoadError::NotMember(ext_op.unqualified_id().to_string()))?;
671669
}
672670
let [TypeArg::Sequence { elems }] = ext_op.args() else {
673671
return Err(SignatureError::InvalidTypeArgs)?;
@@ -717,8 +715,8 @@ impl UnpackTuple {
717715
}
718716

719717
impl MakeExtensionOp for UnpackTuple {
720-
fn name(&self) -> OpName {
721-
TupleOpDef::UnpackTuple.opdef_name()
718+
fn op_id(&self) -> OpName {
719+
TupleOpDef::UnpackTuple.opdef_id()
722720
}
723721

724722
fn from_extension_op(ext_op: &crate::ops::ExtensionOp) -> Result<Self, OpLoadError>
@@ -727,9 +725,7 @@ impl MakeExtensionOp for UnpackTuple {
727725
{
728726
let def = TupleOpDef::from_def(ext_op.def())?;
729727
if def != TupleOpDef::UnpackTuple {
730-
return Err(OpLoadError::NotMember(
731-
ext_op.unqualified_name().to_string(),
732-
))?;
728+
return Err(OpLoadError::NotMember(ext_op.unqualified_id().to_string()))?;
733729
}
734730
let [TypeArg::Sequence { elems }] = ext_op.args() else {
735731
return Err(SignatureError::InvalidTypeArgs)?;
@@ -776,7 +772,7 @@ impl std::str::FromStr for NoopDef {
776772
type Err = ();
777773

778774
fn from_str(s: &str) -> Result<Self, Self::Err> {
779-
if s == NoopDef.name() {
775+
if s == NoopDef.op_id() {
780776
Ok(Self)
781777
} else {
782778
Err(())
@@ -785,7 +781,7 @@ impl std::str::FromStr for NoopDef {
785781
}
786782

787783
impl MakeOpDef for NoopDef {
788-
fn opdef_name(&self) -> OpName {
784+
fn opdef_id(&self) -> OpName {
789785
NOOP_OP_ID
790786
}
791787

@@ -845,8 +841,8 @@ impl Default for Noop {
845841
}
846842

847843
impl MakeExtensionOp for Noop {
848-
fn name(&self) -> OpName {
849-
NoopDef.name()
844+
fn op_id(&self) -> OpName {
845+
NoopDef.op_id()
850846
}
851847

852848
fn from_extension_op(ext_op: &crate::ops::ExtensionOp) -> Result<Self, OpLoadError>
@@ -886,7 +882,7 @@ impl std::str::FromStr for BarrierDef {
886882
type Err = ();
887883

888884
fn from_str(s: &str) -> Result<Self, Self::Err> {
889-
if s == BarrierDef.name() {
885+
if s == BarrierDef.op_id() {
890886
Ok(Self)
891887
} else {
892888
Err(())
@@ -895,7 +891,7 @@ impl std::str::FromStr for BarrierDef {
895891
}
896892

897893
impl MakeOpDef for BarrierDef {
898-
fn opdef_name(&self) -> OpName {
894+
fn opdef_id(&self) -> OpName {
899895
BARRIER_OP_ID
900896
}
901897

@@ -945,13 +941,13 @@ impl Barrier {
945941

946942
impl NamedOp for Barrier {
947943
fn name(&self) -> OpName {
948-
BarrierDef.name()
944+
BarrierDef.op_id()
949945
}
950946
}
951947

952948
impl MakeExtensionOp for Barrier {
953-
fn name(&self) -> OpName {
954-
BarrierDef.name()
949+
fn op_id(&self) -> OpName {
950+
BarrierDef.op_id()
955951
}
956952

957953
fn from_extension_op(ext_op: &crate::ops::ExtensionOp) -> Result<Self, OpLoadError>

hugr-core/src/extension/prelude/generic.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl FromStr for LoadNatDef {
3737
type Err = ();
3838

3939
fn from_str(s: &str) -> Result<Self, Self::Err> {
40-
if s == Self.name() {
40+
if s == Self.op_id() {
4141
Ok(Self)
4242
} else {
4343
Err(())
@@ -65,7 +65,7 @@ impl ConstFold for LoadNatDef {
6565
}
6666

6767
impl MakeOpDef for LoadNatDef {
68-
fn opdef_name(&self) -> OpName {
68+
fn opdef_id(&self) -> OpName {
6969
LOAD_NAT_OP_ID.clone()
7070
}
7171

@@ -118,8 +118,8 @@ impl LoadNat {
118118
}
119119

120120
impl MakeExtensionOp for LoadNat {
121-
fn name(&self) -> OpName {
122-
LoadNatDef.opdef_name()
121+
fn op_id(&self) -> OpName {
122+
LoadNatDef.opdef_id()
123123
}
124124

125125
fn from_extension_op(ext_op: &ExtensionOp) -> Result<Self, OpLoadError>

hugr-core/src/extension/resolution/ops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub(crate) fn resolve_op_extensions<'e>(
7373
// Fail if the Extension is not in the registry, or if the Extension was
7474
// found but did not have the expected operation.
7575
let extension = extension.expect("OpaqueOp should have an extension");
76-
let Some(def) = extension.get_op(opaque.op_name()) else {
76+
let Some(def) = extension.get_op(opaque.unqualified_id()) else {
7777
return Err(OpaqueOpError::OpNotFoundInExtension {
7878
node,
7979
op: opaque.name().clone(),

hugr-core/src/extension/simple_op.rs

+27-14
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,18 @@ pub enum OpLoadError {
3030
///
3131
/// Particularly useful with C-style enums that implement [strum::IntoEnumIterator],
3232
/// as then all definitions can be added to an extension at once.
33+
///
34+
/// [MakeExtensionOp] has a blanket impl for types that impl [MakeOpDef].
3335
pub trait MakeOpDef {
34-
/// TODO docs
35-
fn opdef_name(&self) -> OpName;
36+
/// The [OpDef::name] which will be used when `Self` is added to an [Extension]
37+
/// or when `Self` is loaded from an [OpDef].
38+
///
39+
/// This identifer must be unique within the extension with which the
40+
/// [OpDef] is registered. An [ExtensionOp] instantiating this [OpDef] will
41+
/// report `self.opdef_id()` as its [ExtensionOp::unqualified_id].
42+
///
43+
/// [MakeExtensionOp::op_id] must match this function.
44+
fn opdef_id(&self) -> OpName;
3645

3746
/// Try to load one of the operations of this set from an [OpDef].
3847
fn from_def(op_def: &OpDef) -> Result<Self, OpLoadError>
@@ -57,9 +66,9 @@ pub trait MakeOpDef {
5766
self.init_signature(&self.extension_ref())
5867
}
5968

60-
/// Description of the operation. By default, the same as `self.name()`.
69+
/// Description of the operation. By default, the same as `self.opdef_id()`.
6170
fn description(&self) -> String {
62-
self.opdef_name().to_string()
71+
self.opdef_id().to_string()
6372
}
6473

6574
/// Edit the opdef before finalising. By default does nothing.
@@ -76,7 +85,7 @@ pub trait MakeOpDef {
7685
extension_ref: &Weak<Extension>,
7786
) -> Result<(), ExtensionBuildError> {
7887
let def = extension.add_op(
79-
self.opdef_name(),
88+
self.opdef_id(),
8089
self.description(),
8190
self.init_signature(extension_ref),
8291
extension_ref,
@@ -140,8 +149,12 @@ pub trait HasDef: MakeExtensionOp {
140149
/// Traits implemented by types which can be loaded from [`ExtensionOp`]s,
141150
/// i.e. concrete instances of [`OpDef`]s, with defined type arguments.
142151
pub trait MakeExtensionOp {
143-
/// The name of the operation
144-
fn name(&self) -> OpName;
152+
/// The [OpDef::name] of [ExtensionOp]s from which `Self` can be loaded.
153+
///
154+
/// This identifer must be unique within the extension with which the
155+
/// [OpDef] is registered. An [ExtensionOp] instantiating this [OpDef] will
156+
/// report `self.opdef_id()` as its [ExtensionOp::unqualified_id].
157+
fn op_id(&self) -> OpName;
145158

146159
/// Try to load one of the operations of this set from an [OpDef].
147160
fn from_extension_op(ext_op: &ExtensionOp) -> Result<Self, OpLoadError>
@@ -180,8 +193,8 @@ pub trait MakeExtensionOp {
180193

181194
/// Blanket implementation for non-polymorphic operations - [OpDef]s with no type parameters.
182195
impl<T: MakeOpDef> MakeExtensionOp for T {
183-
fn name(&self) -> OpName {
184-
<Self as MakeOpDef>::opdef_name(self)
196+
fn op_id(&self) -> OpName {
197+
self.opdef_id()
185198
}
186199

187200
#[inline]
@@ -239,7 +252,7 @@ impl<T: MakeExtensionOp> RegisteredOp<T> {
239252
/// Generate an [OpType].
240253
pub fn to_extension_op(&self) -> Option<ExtensionOp> {
241254
ExtensionOp::new(
242-
self.extension.upgrade()?.get_op(&self.name())?.clone(),
255+
self.extension.upgrade()?.get_op(&self.op_id())?.clone(),
243256
self.type_args(),
244257
)
245258
.ok()
@@ -248,7 +261,7 @@ impl<T: MakeExtensionOp> RegisteredOp<T> {
248261
delegate! {
249262
to self.op {
250263
/// Name of the operation - derived from strum serialization.
251-
pub fn name(&self) -> OpName;
264+
pub fn op_id(&self) -> OpName;
252265
/// Any type args which define this operation. Default is no type arguments.
253266
pub fn type_args(&self) -> Vec<TypeArg>;
254267
}
@@ -306,8 +319,8 @@ mod test {
306319
}
307320

308321
impl MakeOpDef for DummyEnum {
309-
fn opdef_name(&self) -> OpName {
310-
<&Self as Into<&'static str>>::into(self).into()
322+
fn opdef_id(&self) -> OpName {
323+
<&'static str>::from(self).into()
311324
}
312325

313326
fn init_signature(&self, _extension_ref: &Weak<Extension>) -> SignatureFunc {
@@ -366,7 +379,7 @@ mod test {
366379
let o = DummyEnum::Dumb;
367380

368381
assert_eq!(
369-
DummyEnum::from_def(EXT.get_op(&o.opdef_name()).unwrap()).unwrap(),
382+
DummyEnum::from_def(EXT.get_op(&o.opdef_id()).unwrap()).unwrap(),
370383
o
371384
);
372385

hugr-core/src/hugr/validate.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl<'a> ValidationContext<'a> {
102102
if let OpType::OpaqueOp(opaque) = op_type {
103103
Err(OpaqueOpError::UnresolvedOp(
104104
node,
105-
opaque.op_name().clone(),
105+
opaque.unqualified_id().clone(),
106106
opaque.extension().clone(),
107107
))?;
108108
}
@@ -536,7 +536,7 @@ impl<'a> ValidationContext<'a> {
536536
OpType::OpaqueOp(opaque) => {
537537
Err(OpaqueOpError::UnresolvedOp(
538538
node,
539-
opaque.op_name().clone(),
539+
opaque.qualified_id(),
540540
opaque.extension().clone(),
541541
))?;
542542
}

hugr-core/src/ops/custom.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ impl ExtensionOp {
137137
self.def.extension_id()
138138
}
139139

140-
/// Returns the unqualified name of the operation. e.g. 'iadd'
140+
/// Returns the unqualified id of the operation. e.g. 'iadd'
141141
///
142-
pub fn unqualified_name(&self) -> &OpNameRef {
142+
pub fn unqualified_id(&self) -> &OpNameRef {
143143
self.def.name()
144144
}
145145

146-
/// Returns the unqualified name of the operation. e.g. 'arithmetic.iadd'
147-
pub fn qualified_name(&self) -> OpName {
148-
self.name()
146+
/// Returns the qualified id of the operation. e.g. 'arithmetic.iadd'
147+
pub fn qualified_id(&self) -> OpName {
148+
qualify_name(self.extension_id(), self.unqualified_id())
149149
}
150150
}
151151

@@ -177,7 +177,7 @@ impl Eq for ExtensionOp {}
177177
impl NamedOp for ExtensionOp {
178178
/// The name of the operation.
179179
fn name(&self) -> OpName {
180-
qualify_name(self.def.extension_id(), self.def.name())
180+
self.qualified_id()
181181
}
182182
}
183183

@@ -260,18 +260,22 @@ impl OpaqueOp {
260260
}
261261

262262
impl NamedOp for OpaqueOp {
263-
/// The name of the operation.
264263
fn name(&self) -> OpName {
265-
format!("OpaqueOp:{}", qualify_name(&self.extension, &self.name)).into()
264+
format!("OpaqueOp:{}", self.qualified_id()).into()
266265
}
267266
}
268267

269268
impl OpaqueOp {
270269
/// Unique name of the operation.
271-
pub fn op_name(&self) -> &OpName {
270+
pub fn unqualified_id(&self) -> &OpName {
272271
&self.name
273272
}
274273

274+
/// Unique name of the operation.
275+
pub fn qualified_id(&self) -> OpName {
276+
qualify_name(self.extension(), self.unqualified_id())
277+
}
278+
275279
/// Type arguments.
276280
pub fn args(&self) -> &[TypeArg] {
277281
&self.args

hugr-core/src/std_extensions/arithmetic/conversions.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ pub enum ConvertOpDef {
4444
}
4545

4646
impl MakeOpDef for ConvertOpDef {
47-
fn opdef_name(&self) -> OpName {
48-
<&Self as Into<&'static str>>::into(self).into()
47+
fn opdef_id(&self) -> OpName {
48+
<&'static str>::from(self).into()
4949
}
5050

5151
fn from_def(op_def: &OpDef) -> Result<Self, OpLoadError> {
@@ -150,8 +150,8 @@ impl ConvertOpType {
150150
}
151151

152152
impl MakeExtensionOp for ConvertOpType {
153-
fn name(&self) -> OpName {
154-
self.def.opdef_name()
153+
fn op_id(&self) -> OpName {
154+
self.def.opdef_id()
155155
}
156156

157157
fn from_extension_op(ext_op: &ExtensionOp) -> Result<Self, OpLoadError> {

hugr-core/src/std_extensions/arithmetic/float_ops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub enum FloatOps {
4949
}
5050

5151
impl MakeOpDef for FloatOps {
52-
fn opdef_name(&self) -> OpName {
52+
fn opdef_id(&self) -> OpName {
5353
<&Self as Into<&'static str>>::into(self).into()
5454
}
5555

0 commit comments

Comments
 (0)