Skip to content

fix: StaticArrayValue serialisation #2009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ target/
# Failing proptest artifacts
proptest-regressions/

# snaphsot test failures
*.snap.new

# Devenv
.devenv*
devenv.local.nix
Expand Down
57 changes: 33 additions & 24 deletions hugr-core/src/std_extensions/collections/static_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,27 @@
//! * `get<T: Copyable>: [static_array<T>, prelude.usize] -> [[] + [T]]`
//! * `len<T: Copyable>: [static_array<T>] -> [prelude.usize]`
use std::{
hash, iter,
hash::{self, Hash as _},
iter,
sync::{self, Arc},
};

use crate::{
builder::{BuildError, Dataflow},
extension::{
prelude::{option_type, usize_t},
resolution::{ExtensionResolutionError, WeakExtensionRegistry},
resolution::{
resolve_type_extensions, resolve_value_extensions, ExtensionResolutionError,
WeakExtensionRegistry,
},
simple_op::{
try_from_name, HasConcrete, HasDef, MakeExtensionOp, MakeOpDef, MakeRegisteredOp,
OpLoadError,
},
ExtensionId, ExtensionSet, OpDef, SignatureError, SignatureFunc, TypeDef,
},
ops::{
constant::{CustomConst, TryHash, ValueName},
constant::{maybe_hash_values, CustomConst, TryHash, ValueName},
ExtensionOp, NamedOp, OpName, Value,
},
types::{
Expand All @@ -41,9 +45,6 @@ use crate::{
Extension, Wire,
};

use super::array::ArrayValue;

use delegate::delegate;
use lazy_static::lazy_static;

/// Reported unique name of the extension
Expand All @@ -57,18 +58,21 @@ pub const VERSION: semver::Version = semver::Version::new(0, 1, 0);
/// Statically sized array of values, all of the same [TypeBound::Copyable]
/// type.
pub struct StaticArrayValue {
/// The contents of the `StaticArrayValue`.
pub value: ArrayValue,
values: Vec<Value>,
typ: Type,
/// The name of the `StaticArrayValue`.
pub name: String,
}

impl StaticArrayValue {
delegate! {
to self.value {
/// Returns the type of values inside the `[StaticArrayValue]`.
pub fn get_element_type(&self) -> &Type;
}
/// Returns the type of values inside the `[StaticArrayValue]`.
pub fn get_element_type(&self) -> &Type {
&self.typ
}

/// Returns the values contained inside the `[StaticArrayValue]`.
pub fn get_contents(&self) -> &[Value] {
&self.values
}

/// Create a new [CustomConst] for an array of values of type `typ`.
Expand All @@ -85,7 +89,8 @@ impl StaticArrayValue {
.into());
}
Ok(Self {
value: ArrayValue::new(typ, contents),
typ,
values: contents.into_iter().collect(),
name: name.to_string(),
})
}
Expand All @@ -102,9 +107,12 @@ impl StaticArrayValue {
}

impl TryHash for StaticArrayValue {
fn try_hash(&self, st: &mut dyn hash::Hasher) -> bool {
st.write(self.name.as_bytes());
self.value.try_hash(st)
fn try_hash(&self, mut st: &mut dyn hash::Hasher) -> bool {
maybe_hash_values(&self.values, &mut st) && {
self.name.hash(&mut st);
self.typ.hash(&mut st);
true
}
}
}

Expand All @@ -123,17 +131,18 @@ impl CustomConst for StaticArrayValue {
}

fn extension_reqs(&self) -> ExtensionSet {
ExtensionSet::union_over(self.value.get_contents().iter().map(Value::extension_reqs))
ExtensionSet::union_over(self.values.iter().map(Value::extension_reqs))
.union(EXTENSION_ID.into())
}

delegate! {
to self.value {
fn update_extensions(
&mut self,
extensions: &WeakExtensionRegistry,
) -> Result<(), ExtensionResolutionError>;
fn update_extensions(
&mut self,
extensions: &WeakExtensionRegistry,
) -> Result<(), ExtensionResolutionError> {
for val in &mut self.values {
resolve_value_extensions(val, extensions)?;
}
resolve_type_extensions(&mut self.typ, extensions)
}
}

Expand Down
5 changes: 3 additions & 2 deletions hugr-py/src/hugr/std/collections/static_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"""Constant value for a statically sized immutable array of elements."""

v: list[val.Value]
ty: tys.Type
ty: StaticArray
name: str

def __init__(self, v: list[val.Value], elem_ty: tys.Type, name: str) -> None:
Expand All @@ -53,7 +53,8 @@
# The value list must be serialized at this point, otherwise the
# `Extension` value would not be serializable.
vs = [v._to_serial_root() for v in self.v]
serial_val = {"values": vs, "name": self.name}
element_ty = self.ty.ty._to_serial_root()
serial_val = {"values": vs, "name": self.name, "typ": element_ty}

Check warning on line 57 in hugr-py/src/hugr/std/collections/static_array.py

View check run for this annotation

Codecov / codecov/patch

hugr-py/src/hugr/std/collections/static_array.py#L56-L57

Added lines #L56 - L57 were not covered by tests
return val.Extension(
"StaticArrayValue", typ=self.ty, val=serial_val, extensions=[EXTENSION.name]
)
Expand Down
Loading