From e94171d124c1fa716264f6bc7077e96e80e70cbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20Borgna?= Date: Thu, 12 Dec 2024 17:45:28 +0000 Subject: [PATCH] Make nodes optional in resolution errors --- hugr-core/src/extension/resolution.rs | 34 +++++++------------ hugr-core/src/extension/resolution/ops.rs | 5 ++- hugr-core/src/extension/resolution/test.rs | 3 +- .../src/extension/resolution/types_mut.rs | 14 ++++---- hugr-core/src/hugr.rs | 2 +- 5 files changed, 26 insertions(+), 32 deletions(-) diff --git a/hugr-core/src/extension/resolution.rs b/hugr-core/src/extension/resolution.rs index 4901502b9..269924a45 100644 --- a/hugr-core/src/extension/resolution.rs +++ b/hugr-core/src/extension/resolution.rs @@ -44,11 +44,8 @@ pub fn resolve_type_extensions( typ: &mut TypeBase, extensions: &ExtensionRegistry, ) -> Result<(), ExtensionResolutionError> { - // This public export is used for implementing `CustomConst::update_extensions`, so we don't need the full internal API here. - // TODO: Make `node` optional in `ExtensionResolutionError` - let node: Node = portgraph::NodeIndex::new(0).into(); let mut used_extensions = ExtensionRegistry::default(); - resolve_type_exts(node, typ, extensions, &mut used_extensions) + resolve_type_exts(None, typ, extensions, &mut used_extensions) } /// Update all weak Extension pointers in a custom type. @@ -56,11 +53,8 @@ pub fn resolve_custom_type_extensions( typ: &mut CustomType, extensions: &ExtensionRegistry, ) -> Result<(), ExtensionResolutionError> { - // This public export is used for implementing `CustomConst::update_extensions`, so we don't need the full internal API here. - // TODO: Make `node` optional in `ExtensionResolutionError` - let node: Node = portgraph::NodeIndex::new(0).into(); let mut used_extensions = ExtensionRegistry::default(); - resolve_custom_type_exts(node, typ, extensions, &mut used_extensions) + resolve_custom_type_exts(None, typ, extensions, &mut used_extensions) } /// Update all weak Extension pointers inside a type argument. @@ -68,11 +62,8 @@ pub fn resolve_typearg_extensions( arg: &mut TypeArg, extensions: &ExtensionRegistry, ) -> Result<(), ExtensionResolutionError> { - // This public export is used for implementing `CustomConst::update_extensions`, so we don't need the full internal API here. - // TODO: Make `node` optional in `ExtensionResolutionError` - let node: Node = portgraph::NodeIndex::new(0).into(); let mut used_extensions = ExtensionRegistry::default(); - resolve_typearg_exts(node, arg, extensions, &mut used_extensions) + resolve_typearg_exts(None, arg, extensions, &mut used_extensions) } /// Update all weak Extension pointers inside a constant value. @@ -80,11 +71,8 @@ pub fn resolve_value_extensions( value: &mut Value, extensions: &ExtensionRegistry, ) -> Result<(), ExtensionResolutionError> { - // This public export is used for implementing `CustomConst::update_extensions`, so we don't need the full internal API here. - // TODO: Make `node` optional in `ExtensionResolutionError` - let node: Node = portgraph::NodeIndex::new(0).into(); let mut used_extensions = ExtensionRegistry::default(); - resolve_value_exts(node, value, extensions, &mut used_extensions) + resolve_value_exts(None, value, extensions, &mut used_extensions) } /// Errors that can occur during extension resolution. @@ -97,12 +85,13 @@ pub enum ExtensionResolutionError { OpaqueOpError(OpaqueOpError), /// An operation requires an extension that is not in the given registry. #[display( - "{op} ({node}) requires extension {missing_extension}, but it could not be found in the extension list used during resolution. The available extensions are: {}", + "{op}{} requires extension {missing_extension}, but it could not be found in the extension list used during resolution. The available extensions are: {}", + node.map(|n| format!(" in {}", n)).unwrap_or_default(), available_extensions.join(", ") )] MissingOpExtension { /// The node that requires the extension. - node: Node, + node: Option, /// The operation that requires the extension. op: OpName, /// The missing extension @@ -111,13 +100,14 @@ pub enum ExtensionResolutionError { available_extensions: Vec, }, #[display( - "Type {ty} in {node} requires extension {missing_extension}, but it could not be found in the extension list used during resolution. The available extensions are: {}", + "Type {ty}{} requires extension {missing_extension}, but it could not be found in the extension list used during resolution. The available extensions are: {}", + node.map(|n| format!(" in {}", n)).unwrap_or_default(), available_extensions.join(", ") )] /// A type references an extension that is not in the given registry. MissingTypeExtension { /// The node that requires the extension. - node: Node, + node: Option, /// The type that requires the extension. ty: TypeName, /// The missing extension @@ -138,7 +128,7 @@ pub enum ExtensionResolutionError { impl ExtensionResolutionError { /// Create a new error for missing operation extensions. pub fn missing_op_extension( - node: Node, + node: Option, op: &OpType, missing_extension: &ExtensionId, extensions: &ExtensionRegistry, @@ -153,7 +143,7 @@ impl ExtensionResolutionError { /// Create a new error for missing type extensions. pub fn missing_type_extension( - node: Node, + node: Option, ty: &TypeName, missing_extension: &ExtensionId, extensions: &ExtensionRegistry, diff --git a/hugr-core/src/extension/resolution/ops.rs b/hugr-core/src/extension/resolution/ops.rs index 8e9591688..4ffc9c35e 100644 --- a/hugr-core/src/extension/resolution/ops.rs +++ b/hugr-core/src/extension/resolution/ops.rs @@ -124,7 +124,10 @@ fn operation_extension<'e>( match extensions.get(ext) { Some(e) => Ok(Some(e)), None => Err(ExtensionResolutionError::missing_op_extension( - node, op, ext, extensions, + Some(node), + op, + ext, + extensions, )), } } diff --git a/hugr-core/src/extension/resolution/test.rs b/hugr-core/src/extension/resolution/test.rs index 0cf9fb0dc..73e7cedb9 100644 --- a/hugr-core/src/extension/resolution/test.rs +++ b/hugr-core/src/extension/resolution/test.rs @@ -54,7 +54,8 @@ fn resolve_type_extensions(#[case] op: impl Into, #[case] extensions: Ex let mut used_exts = ExtensionRegistry::default(); resolve_op_extensions(dummy_node, &mut deser_op, &extensions).unwrap(); - resolve_op_types_extensions(dummy_node, &mut deser_op, &extensions, &mut used_exts).unwrap(); + resolve_op_types_extensions(Some(dummy_node), &mut deser_op, &extensions, &mut used_exts) + .unwrap(); let deser_extensions = deser_op.used_extensions().unwrap(); diff --git a/hugr-core/src/extension/resolution/types_mut.rs b/hugr-core/src/extension/resolution/types_mut.rs index 520bf2919..31439603a 100644 --- a/hugr-core/src/extension/resolution/types_mut.rs +++ b/hugr-core/src/extension/resolution/types_mut.rs @@ -22,7 +22,7 @@ use crate::Node; /// /// This is a helper function used right after deserializing a Hugr. pub fn resolve_op_types_extensions( - node: Node, + node: Option, op: &mut OpType, extensions: &ExtensionRegistry, used_extensions: &mut ExtensionRegistry, @@ -113,7 +113,7 @@ pub fn resolve_op_types_extensions( /// /// Adds the extensions used in the signature to the `used_extensions` registry. fn resolve_signature_exts( - node: Node, + node: Option, signature: &mut Signature, extensions: &ExtensionRegistry, used_extensions: &mut ExtensionRegistry, @@ -129,7 +129,7 @@ fn resolve_signature_exts( /// /// Adds the extensions used in the row to the `used_extensions` registry. fn resolve_type_row_exts( - node: Node, + node: Option, row: &mut TypeRowBase, extensions: &ExtensionRegistry, used_extensions: &mut ExtensionRegistry, @@ -144,7 +144,7 @@ fn resolve_type_row_exts( /// /// Adds the extensions used in the type to the `used_extensions` registry. pub(super) fn resolve_type_exts( - node: Node, + node: Option, typ: &mut TypeBase, extensions: &ExtensionRegistry, used_extensions: &mut ExtensionRegistry, @@ -175,7 +175,7 @@ pub(super) fn resolve_type_exts( /// /// Adds the extensions used in the type to the `used_extensions` registry. pub(super) fn resolve_custom_type_exts( - node: Node, + node: Option, custom: &mut CustomType, extensions: &ExtensionRegistry, used_extensions: &mut ExtensionRegistry, @@ -201,7 +201,7 @@ pub(super) fn resolve_custom_type_exts( /// /// Adds the extensions used in the type to the `used_extensions` registry. pub(super) fn resolve_typearg_exts( - node: Node, + node: Option, arg: &mut TypeArg, extensions: &ExtensionRegistry, used_extensions: &mut ExtensionRegistry, @@ -222,7 +222,7 @@ pub(super) fn resolve_typearg_exts( /// /// Adds the extensions used in the row to the `used_extensions` registry. pub(super) fn resolve_value_exts( - node: Node, + node: Option, value: &mut Value, extensions: &ExtensionRegistry, used_extensions: &mut ExtensionRegistry, diff --git a/hugr-core/src/hugr.rs b/hugr-core/src/hugr.rs index e3392b601..17e2bffd0 100644 --- a/hugr-core/src/hugr.rs +++ b/hugr-core/src/hugr.rs @@ -243,7 +243,7 @@ impl Hugr { if let Some(extension) = resolve_op_extensions(node, op, extensions)? { used_extensions.register_updated_ref(extension); } - resolve_op_types_extensions(node, op, extensions, &mut used_extensions)?; + resolve_op_types_extensions(Some(node), op, extensions, &mut used_extensions)?; } self.extensions = used_extensions;