Skip to content

Commit

Permalink
fix: breaking changes from extension weak references
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Nov 28, 2024
1 parent 2f4c1db commit df6f30f
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 142 deletions.
184 changes: 92 additions & 92 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 11 additions & 7 deletions tket2-hseries/src/extension/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! `Future<t>` is a linear type representing a value that will be available in
//! the future. It can be consumed by `Read`, returning a `t`. It can be
//! duplicated by `Dup`, and discarded with `Free`.
use std::sync::Arc;
use std::sync::{Arc, Weak};

use hugr::{
builder::{BuildError, Dataflow},
Expand Down Expand Up @@ -32,11 +32,11 @@ pub const EXTENSION_VERSION: Version = Version::new(0, 1, 0);
lazy_static! {
/// The "tket2.futures" extension.
pub static ref EXTENSION: Arc<Extension> = {
let mut ext = Extension::new(EXTENSION_ID, EXTENSION_VERSION);
let _ = add_future_type_def(&mut ext).unwrap();
Extension::new_arc(EXTENSION_ID, EXTENSION_VERSION, |ext, ext_ref| {
let _ = add_future_type_def(ext, ext_ref.clone()).unwrap();

FutureOpDef::load_all_ops(&mut ext).unwrap();
Arc::new(ext)
FutureOpDef::load_all_ops( ext, ext_ref).unwrap();
})
};

/// Extension registry including the "tket2.futures" extension.
Expand All @@ -48,12 +48,16 @@ lazy_static! {
pub static ref FUTURE_TYPE_NAME: SmolStr = SmolStr::new_inline("Future");
}

fn add_future_type_def(ext: &mut Extension) -> Result<&TypeDef, ExtensionBuildError> {
fn add_future_type_def(
ext: &mut Extension,
extension_ref: Weak<Extension>,
) -> Result<&TypeDef, ExtensionBuildError> {
ext.add_type(
FUTURE_TYPE_NAME.to_owned(),
vec![TypeBound::Any.into()],
"A value that is computed asynchronously".into(),
TypeBound::Any.into(),
&extension_ref,
)
}

Expand Down Expand Up @@ -121,7 +125,7 @@ impl MakeOpDef for FutureOpDef {
}

fn from_def(op_def: &OpDef) -> Result<Self, hugr::extension::simple_op::OpLoadError> {
try_from_name(op_def.name(), op_def.extension())
try_from_name(op_def.name(), op_def.extension_id())
}

fn description(&self) -> String {
Expand Down
17 changes: 9 additions & 8 deletions tket2-hseries/src/extension/hseries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ pub const EXTENSION_VERSION: Version = Version::new(0, 1, 0);
lazy_static! {
/// The "tket2.hseries" extension.
pub static ref EXTENSION: Arc<Extension> = {
let mut ext = Extension::new(EXTENSION_ID, EXTENSION_VERSION).with_reqs(ExtensionSet::from_iter([
futures::EXTENSION.name(),
PRELUDE.name(),
FLOAT_TYPES.name(),
].into_iter().cloned()));
HSeriesOp::load_all_ops(&mut ext).unwrap();
Arc::new(ext)
Extension::new_arc(EXTENSION_ID, EXTENSION_VERSION, |ext, ext_ref| {
ext.add_requirements(ExtensionSet::from_iter([
futures::EXTENSION.name(),
PRELUDE.name(),
FLOAT_TYPES.name(),
].into_iter().cloned()));
HSeriesOp::load_all_ops( ext, ext_ref).unwrap();
})
};

/// Extension registry including the "tket2.hseries" extension and
Expand Down Expand Up @@ -110,7 +111,7 @@ impl MakeOpDef for HSeriesOp {
}

fn from_def(op_def: &OpDef) -> Result<Self, hugr::extension::simple_op::OpLoadError> {
try_from_name(op_def.name(), op_def.extension())
try_from_name(op_def.name(), op_def.extension_id())
}

fn extension(&self) -> ExtensionId {
Expand Down
2 changes: 1 addition & 1 deletion tket2-hseries/src/extension/hseries/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub fn check_lowered(hugr: &impl HugrView) -> Result<(), Vec<Node>> {
.filter_map(|node| {
let optype = hugr.get_optype(node);
optype.as_extension_op().and_then(|ext| {
(ext.def().extension() == &tket2::extension::TKET2_EXTENSION_ID).then_some(node)
(ext.def().extension_id() == &tket2::extension::TKET2_EXTENSION_ID).then_some(node)
})
})
.collect();
Expand Down
9 changes: 5 additions & 4 deletions tket2-hseries/src/extension/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ pub const EXTENSION_VERSION: Version = Version::new(0, 1, 0);
lazy_static! {
/// The "tket2.result" extension.
pub static ref EXTENSION: Arc<Extension> = {
let mut ext = Extension::new(EXTENSION_ID, EXTENSION_VERSION).with_reqs(ExtensionSet::from_iter([INT_EXTENSION_ID, FLOAT_EXTENSION_ID]));
ResultOpDef::load_all_ops(&mut ext).unwrap();
Arc::new(ext)
Extension::new_arc(EXTENSION_ID, EXTENSION_VERSION, |ext, ext_ref| {
ext.add_requirements(ExtensionSet::from_iter([INT_EXTENSION_ID, FLOAT_EXTENSION_ID]));
ResultOpDef::load_all_ops(ext, ext_ref).unwrap();
})
};

/// Extension registry including the "tket2.result" extension and
Expand Down Expand Up @@ -196,7 +197,7 @@ impl MakeOpDef for ResultOpDef {
}

fn from_def(op_def: &OpDef) -> Result<Self, hugr::extension::simple_op::OpLoadError> {
try_from_name(op_def.name(), op_def.extension())
try_from_name(op_def.name(), op_def.extension_id())
}

fn extension(&self) -> ExtensionId {
Expand Down
32 changes: 15 additions & 17 deletions tket2/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,14 @@ pub static ref TKET1_OP_PAYLOAD : CustomType =

/// The TKET1 extension, containing the opaque TKET1 operations.
pub static ref TKET1_EXTENSION: Arc<Extension> = {
let mut res = Extension::new(TKET1_EXTENSION_ID, TKET1_EXTENSION_VERSION);

let tket1_op_payload = TypeParam::String;
res.add_op(
TKET1_OP_NAME,
"An opaque TKET1 operation.".into(),
Tk1Signature([tket1_op_payload])
).unwrap();

Arc::new(res)
Extension::new_arc(TKET1_EXTENSION_ID, TKET1_EXTENSION_VERSION, |res, ext_ref| {
res.add_op(
TKET1_OP_NAME,
"An opaque TKET1 operation.".into(),
Tk1Signature([TypeParam::String]),
ext_ref
).unwrap();
})
};

/// Extension registry including the prelude, std, TKET1, and Tk2Ops extensions.
Expand Down Expand Up @@ -95,11 +93,11 @@ pub const TKET2_EXTENSION_ID: ExtensionId = ExtensionId::new_unchecked("tket2.qu
pub const TKET2_EXTENSION_VERSION: Version = Version::new(0, 1, 0);

lazy_static! {
/// The extension definition for TKET2 ops and types.
pub static ref TKET2_EXTENSION: Arc<Extension> = {
let mut e = Extension::new(TKET2_EXTENSION_ID, TKET2_EXTENSION_VERSION);
Tk2Op::load_all_ops(&mut e).expect("add fail");
SympyOpDef.add_to_extension(&mut e).unwrap();
Arc::new(e)
};
/// The extension definition for TKET2 ops and types.
pub static ref TKET2_EXTENSION: Arc<Extension> = {
Extension::new_arc(TKET2_EXTENSION_ID, TKET2_EXTENSION_VERSION, |res, ext_ref| {
Tk2Op::load_all_ops(res, ext_ref).expect("add_fail");
SympyOpDef.add_to_extension(res, ext_ref).unwrap();
})
};
}
19 changes: 10 additions & 9 deletions tket2/src/extension/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use hugr::{
};
use smol_str::SmolStr;
use std::f64::consts::PI;
use std::sync::Arc;
use std::sync::{Arc, Weak};
use strum::{EnumIter, EnumString, IntoStaticStr};

use lazy_static::lazy_static;
Expand All @@ -23,11 +23,11 @@ pub const ROTATION_EXTENSION_VERSION: Version = Version::new(0, 1, 0);

lazy_static! {
/// The extension definition for TKET2 rotation type and ops.
pub static ref ROTATION_EXTENSION: Arc<Extension> = {
let mut e = Extension::new(ROTATION_EXTENSION_ID, ROTATION_EXTENSION_VERSION);
add_to_extension(&mut e);
Arc::new(e)
};
pub static ref ROTATION_EXTENSION: Arc<Extension> = {
Extension::new_arc(ROTATION_EXTENSION_ID, ROTATION_EXTENSION_VERSION, |e, extension_ref| {
add_to_extension(e, extension_ref);
}
)};
}

/// Identifier for the rotation type.
Expand Down Expand Up @@ -132,7 +132,7 @@ impl MakeOpDef for RotationOp {
where
Self: Sized,
{
hugr::extension::simple_op::try_from_name(op_def.name(), op_def.extension())
hugr::extension::simple_op::try_from_name(op_def.name(), op_def.extension_id())
}

fn signature(&self) -> hugr::extension::SignatureFunc {
Expand Down Expand Up @@ -189,17 +189,18 @@ impl MakeRegisteredOp for RotationOp {
}
}

pub(super) fn add_to_extension(extension: &mut Extension) {
pub(super) fn add_to_extension(extension: &mut Extension, extension_ref: &Weak<Extension>) {
extension
.add_type(
ROTATION_TYPE_ID,
vec![],
"rotation type expressed as number of half turns".to_owned(),
TypeBound::Copyable.into(),
extension_ref,
)
.unwrap();

RotationOp::load_all_ops(extension).expect("add fail");
RotationOp::load_all_ops(extension, extension_ref).expect("add fail");
}

/// An extension trait for [Dataflow] providing methods to add
Expand Down
2 changes: 1 addition & 1 deletion tket2/src/extension/sympy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl MakeOpDef for SympyOpDef {
where
Self: Sized,
{
try_from_name(op_def.name(), op_def.extension())
try_from_name(op_def.name(), op_def.extension_id())
}

fn signature(&self) -> hugr::extension::SignatureFunc {
Expand Down
6 changes: 3 additions & 3 deletions tket2/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl MakeOpDef for Tk2Op {
}

fn from_def(op_def: &OpDef) -> Result<Self, hugr::extension::simple_op::OpLoadError> {
try_from_name(op_def.name(), op_def.extension())
try_from_name(op_def.name(), op_def.extension_id())
}
}

Expand Down Expand Up @@ -190,7 +190,7 @@ pub(crate) fn match_symb_const_op(op: &OpType) -> Option<String> {
};

if let OpType::ExtensionOp(e) = op {
if e.def().name() == &SYM_OP_ID && e.def().extension() == &EXTENSION_ID {
if e.def().name() == &SYM_OP_ID && e.def().extension_id() == &EXTENSION_ID {
Some(symbol_from_typeargs(e.args()))
} else {
None
Expand Down Expand Up @@ -276,7 +276,7 @@ pub(crate) mod test {

let ext_op = op.into_extension_op();
assert_eq!(ext_op.args(), &[]);
assert_eq!(ext_op.def().extension(), &EXTENSION_ID);
assert_eq!(ext_op.def().extension_id(), &EXTENSION_ID);
let name = ext_op.def().name();
assert_eq!(Tk2Op::from_str(name), Ok(op));
}
Expand Down

0 comments on commit df6f30f

Please sign in to comment.