Skip to content

Commit 5b50d1d

Browse files
authored
fix!: Replace LoadFunction::signature with LoadFunction::instantiation (#1756)
Closes #1755 This change breaks serialisation. We will already break serialisation with the next release, so we judge this to be acceptable. BREAKING CHANGE: The `LoadFunction::signature` field is removed. Replace uses with `DataflowOpTrait::signature()`.
1 parent 34ce691 commit 5b50d1d

File tree

9 files changed

+24
-28
lines changed

9 files changed

+24
-28
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub(crate) fn collect_op_types_extensions(
5858
OpType::LoadConstant(lc) => collect_type_exts(&lc.datatype, &mut used, &mut missing),
5959
OpType::LoadFunction(lf) => {
6060
collect_signature_exts(lf.func_sig.body(), &mut used, &mut missing);
61-
collect_signature_exts(&lf.signature, &mut used, &mut missing);
61+
collect_signature_exts(&lf.instantiation, &mut used, &mut missing);
6262
}
6363
OpType::DFG(dfg) => collect_signature_exts(&dfg.signature, &mut used, &mut missing),
6464
OpType::OpaqueOp(op) => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn resolve_op_types_extensions(
6969
}
7070
OpType::LoadFunction(lf) => {
7171
resolve_signature_exts(node, lf.func_sig.body_mut(), extensions, used_extensions)?;
72-
resolve_signature_exts(node, &mut lf.signature, extensions, used_extensions)?;
72+
resolve_signature_exts(node, &mut lf.instantiation, extensions, used_extensions)?;
7373
}
7474
OpType::DFG(dfg) => {
7575
resolve_signature_exts(node, &mut dfg.signature, extensions, used_extensions)?

hugr-core/src/ops/dataflow.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use super::{impl_op_name, OpTag, OpTrait};
77
use crate::extension::{ExtensionRegistry, ExtensionSet, SignatureError};
88
use crate::ops::StaticTag;
99
use crate::types::{EdgeKind, PolyFuncType, Signature, Type, TypeArg, TypeRow};
10-
use crate::IncomingPort;
10+
use crate::{type_row, IncomingPort};
1111

1212
#[cfg(test)]
13-
use ::proptest_derive::Arbitrary;
13+
use proptest_derive::Arbitrary;
1414

1515
/// Trait implemented by all dataflow operations.
1616
pub trait DataflowOpTrait {
@@ -347,7 +347,7 @@ pub struct LoadFunction {
347347
/// The type arguments that instantiate `func_sig`.
348348
pub type_args: Vec<TypeArg>,
349349
/// The instantiation of `func_sig`.
350-
pub signature: Signature, // Cache, so we can fail in try_new() not in signature()
350+
pub instantiation: Signature, // Cache, so we can fail in try_new() not in signature()
351351
}
352352
impl_op_name!(LoadFunction);
353353
impl DataflowOpTrait for LoadFunction {
@@ -358,7 +358,10 @@ impl DataflowOpTrait for LoadFunction {
358358
}
359359

360360
fn signature(&self) -> Cow<'_, Signature> {
361-
Cow::Borrowed(&self.signature)
361+
Cow::Owned(Signature::new(
362+
type_row![],
363+
Type::new_function(self.instantiation.clone()),
364+
))
362365
}
363366

364367
fn static_input(&self) -> Option<EdgeKind> {
@@ -377,11 +380,10 @@ impl LoadFunction {
377380
) -> Result<Self, SignatureError> {
378381
let type_args: Vec<_> = type_args.into();
379382
let instantiation = func_sig.instantiate(&type_args, exts)?;
380-
let signature = Signature::new(TypeRow::new(), vec![Type::new_function(instantiation)]);
381383
Ok(Self {
382384
func_sig,
383385
type_args,
384-
signature,
386+
instantiation,
385387
})
386388
}
387389

@@ -410,12 +412,12 @@ impl LoadFunction {
410412
self.type_args.clone(),
411413
extension_registry,
412414
)?;
413-
if other.signature == self.signature {
415+
if other.instantiation == self.instantiation {
414416
Ok(())
415417
} else {
416418
Err(SignatureError::LoadFunctionIncorrectlyAppliesType {
417-
cached: self.signature.clone(),
418-
expected: other.signature.clone(),
419+
cached: self.instantiation.clone(),
420+
expected: other.instantiation.clone(),
419421
})
420422
}
421423
}

hugr-py/src/hugr/_serialization/ops.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -367,18 +367,12 @@ class LoadFunction(DataflowOp):
367367
op: Literal["LoadFunction"] = "LoadFunction"
368368
func_sig: PolyFuncType
369369
type_args: list[stys.TypeArg]
370-
signature: FunctionType
370+
instantiation: FunctionType
371371

372372
def deserialize(self) -> ops.LoadFunc:
373-
signature = self.signature.deserialize()
374-
assert len(signature.input) == 0
375-
(f_ty,) = signature.output
376-
assert isinstance(
377-
f_ty, tys.FunctionType
378-
), "Expected single function type output"
379373
return ops.LoadFunc(
380374
self.func_sig.deserialize(),
381-
f_ty,
375+
self.instantiation.deserialize(),
382376
deser_it(self.type_args),
383377
)
384378

hugr-py/src/hugr/ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ def _to_serial(self, parent: Node) -> sops.LoadFunction:
12371237
parent=parent.idx,
12381238
func_sig=self.signature._to_serial(),
12391239
type_args=ser_it(self.type_args),
1240-
signature=self.outer_signature()._to_serial(),
1240+
instantiation=self.instantiation._to_serial(),
12411241
)
12421242

12431243
def outer_signature(self) -> tys.FunctionType:

specification/schema/hugr_schema_live.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,15 +1054,15 @@
10541054
"title": "Type Args",
10551055
"type": "array"
10561056
},
1057-
"signature": {
1057+
"instantiation": {
10581058
"$ref": "#/$defs/FunctionType"
10591059
}
10601060
},
10611061
"required": [
10621062
"parent",
10631063
"func_sig",
10641064
"type_args",
1065-
"signature"
1065+
"instantiation"
10661066
],
10671067
"title": "LoadFunction",
10681068
"type": "object"

specification/schema/hugr_schema_strict_live.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,15 +1054,15 @@
10541054
"title": "Type Args",
10551055
"type": "array"
10561056
},
1057-
"signature": {
1057+
"instantiation": {
10581058
"$ref": "#/$defs/FunctionType"
10591059
}
10601060
},
10611061
"required": [
10621062
"parent",
10631063
"func_sig",
10641064
"type_args",
1065-
"signature"
1065+
"instantiation"
10661066
],
10671067
"title": "LoadFunction",
10681068
"type": "object"

specification/schema/testing_hugr_schema_live.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,15 +1054,15 @@
10541054
"title": "Type Args",
10551055
"type": "array"
10561056
},
1057-
"signature": {
1057+
"instantiation": {
10581058
"$ref": "#/$defs/FunctionType"
10591059
}
10601060
},
10611061
"required": [
10621062
"parent",
10631063
"func_sig",
10641064
"type_args",
1065-
"signature"
1065+
"instantiation"
10661066
],
10671067
"title": "LoadFunction",
10681068
"type": "object"

specification/schema/testing_hugr_schema_strict_live.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,15 +1054,15 @@
10541054
"title": "Type Args",
10551055
"type": "array"
10561056
},
1057-
"signature": {
1057+
"instantiation": {
10581058
"$ref": "#/$defs/FunctionType"
10591059
}
10601060
},
10611061
"required": [
10621062
"parent",
10631063
"func_sig",
10641064
"type_args",
1065-
"signature"
1065+
"instantiation"
10661066
],
10671067
"title": "LoadFunction",
10681068
"type": "object"

0 commit comments

Comments
 (0)