Skip to content

Commit

Permalink
Make nodes optional in resolution errors
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Dec 12, 2024
1 parent 8722c10 commit e94171d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 32 deletions.
34 changes: 12 additions & 22 deletions hugr-core/src/extension/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,47 +44,35 @@ pub fn resolve_type_extensions<RV: MaybeRV>(
typ: &mut TypeBase<RV>,
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.
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.
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.
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.
Expand All @@ -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<Node>,
/// The operation that requires the extension.
op: OpName,
/// The missing extension
Expand All @@ -111,13 +100,14 @@ pub enum ExtensionResolutionError {
available_extensions: Vec<ExtensionId>,
},
#[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<Node>,
/// The type that requires the extension.
ty: TypeName,
/// The missing extension
Expand All @@ -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<Node>,
op: &OpType,
missing_extension: &ExtensionId,
extensions: &ExtensionRegistry,
Expand All @@ -153,7 +143,7 @@ impl ExtensionResolutionError {

/// Create a new error for missing type extensions.
pub fn missing_type_extension(
node: Node,
node: Option<Node>,
ty: &TypeName,
missing_extension: &ExtensionId,
extensions: &ExtensionRegistry,
Expand Down
5 changes: 4 additions & 1 deletion hugr-core/src/extension/resolution/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)),
}
}
3 changes: 2 additions & 1 deletion hugr-core/src/extension/resolution/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ fn resolve_type_extensions(#[case] op: impl Into<OpType>, #[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();

Expand Down
14 changes: 7 additions & 7 deletions hugr-core/src/extension/resolution/types_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Node>,
op: &mut OpType,
extensions: &ExtensionRegistry,
used_extensions: &mut ExtensionRegistry,
Expand Down Expand Up @@ -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<Node>,
signature: &mut Signature,
extensions: &ExtensionRegistry,
used_extensions: &mut ExtensionRegistry,
Expand All @@ -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<RV: MaybeRV>(
node: Node,
node: Option<Node>,
row: &mut TypeRowBase<RV>,
extensions: &ExtensionRegistry,
used_extensions: &mut ExtensionRegistry,
Expand All @@ -144,7 +144,7 @@ fn resolve_type_row_exts<RV: MaybeRV>(
///
/// Adds the extensions used in the type to the `used_extensions` registry.
pub(super) fn resolve_type_exts<RV: MaybeRV>(
node: Node,
node: Option<Node>,
typ: &mut TypeBase<RV>,
extensions: &ExtensionRegistry,
used_extensions: &mut ExtensionRegistry,
Expand Down Expand Up @@ -175,7 +175,7 @@ pub(super) fn resolve_type_exts<RV: MaybeRV>(
///
/// Adds the extensions used in the type to the `used_extensions` registry.
pub(super) fn resolve_custom_type_exts(
node: Node,
node: Option<Node>,
custom: &mut CustomType,
extensions: &ExtensionRegistry,
used_extensions: &mut ExtensionRegistry,
Expand All @@ -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<Node>,
arg: &mut TypeArg,
extensions: &ExtensionRegistry,
used_extensions: &mut ExtensionRegistry,
Expand All @@ -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<Node>,
value: &mut Value,
extensions: &ExtensionRegistry,
used_extensions: &mut ExtensionRegistry,
Expand Down
2 changes: 1 addition & 1 deletion hugr-core/src/hugr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e94171d

Please sign in to comment.