Skip to content
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
2 changes: 1 addition & 1 deletion hugr-cli/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use clap::Parser;
use clap_verbosity_flag::log::Level;
use hugr::package::PackageValidationError;
use hugr::Hugr;
use hugr::{Hugr, HugrView};

use crate::hugr_io::HugrInputArgs;
use crate::{CliError, OtherArgs};
Expand Down
4 changes: 2 additions & 2 deletions hugr-core/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
//! `CircuitBuilder`.
//!
//! ```rust
//! # use hugr::Hugr;
//! # use hugr::{Hugr, HugrView};
//! # use hugr::builder::{BuildError, BuildHandle, Container, DFGBuilder, Dataflow, DataflowHugr, ModuleBuilder, DataflowSubContainer, HugrBuilder};
//! use hugr::extension::prelude::bool_t;
//! use hugr::std_extensions::logic::{self, LogicOp};
Expand Down Expand Up @@ -138,7 +138,7 @@ pub fn inout_sig(inputs: impl Into<TypeRow>, outputs: impl Into<TypeRow>) -> Sig
pub enum BuildError {
/// The constructed HUGR is invalid.
#[error("The constructed HUGR is invalid: {0}.")]
InvalidHUGR(#[from] ValidationError),
InvalidHUGR(#[from] ValidationError<Node>),
/// SignatureError in trying to construct a node (differs from
/// [ValidationError::SignatureError] in that we could not construct a node to report about)
#[error(transparent)]
Expand Down
2 changes: 1 addition & 1 deletion hugr-core/src/builder/build_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub trait Container {
/// (with varying root node types)
pub trait HugrBuilder: Container {
/// Finish building the HUGR, perform any validation checks and return it.
fn finish_hugr(self) -> Result<Hugr, ValidationError>;
fn finish_hugr(self) -> Result<Hugr, ValidationError<Node>>;
}

/// Types implementing this trait build a container graph region by borrowing a HUGR
Expand Down
2 changes: 1 addition & 1 deletion hugr-core/src/builder/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl CFGBuilder<Hugr> {
}

impl HugrBuilder for CFGBuilder<Hugr> {
fn finish_hugr(self) -> Result<Hugr, crate::hugr::ValidationError> {
fn finish_hugr(self) -> Result<Hugr, crate::hugr::ValidationError<Node>> {
self.base.validate()?;
Ok(self.base)
}
Expand Down
2 changes: 1 addition & 1 deletion hugr-core/src/builder/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> ConditionalBuilder<B> {
}

impl HugrBuilder for ConditionalBuilder<Hugr> {
fn finish_hugr(self) -> Result<Hugr, crate::hugr::ValidationError> {
fn finish_hugr(self) -> Result<Hugr, crate::hugr::ValidationError<Node>> {
self.base.validate()?;
Ok(self.base)
}
Expand Down
4 changes: 2 additions & 2 deletions hugr-core/src/builder/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl DFGBuilder<Hugr> {
}

impl HugrBuilder for DFGBuilder<Hugr> {
fn finish_hugr(self) -> Result<Hugr, ValidationError> {
fn finish_hugr(self) -> Result<Hugr, ValidationError<Node>> {
self.base.validate()?;
Ok(self.base)
}
Expand Down Expand Up @@ -306,7 +306,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>, T: From<BuildHandle<DfgID>>> SubContainer for
}

impl<T> HugrBuilder for DFGWrapper<Hugr, T> {
fn finish_hugr(self) -> Result<Hugr, ValidationError> {
fn finish_hugr(self) -> Result<Hugr, ValidationError<Node>> {
self.0.finish_hugr()
}
}
Expand Down
2 changes: 1 addition & 1 deletion hugr-core/src/builder/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Default for ModuleBuilder<Hugr> {
}

impl HugrBuilder for ModuleBuilder<Hugr> {
fn finish_hugr(self) -> Result<Hugr, ValidationError> {
fn finish_hugr(self) -> Result<Hugr, ValidationError<Node>> {
self.0.validate()?;
Ok(self.0)
}
Expand Down
2 changes: 1 addition & 1 deletion hugr-core/src/builder/tail_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ mod test {
use super::*;
#[test]
fn basic_loop() -> Result<(), BuildError> {
let build_result: Result<Hugr, ValidationError> = {
let build_result: Result<Hugr, ValidationError<_>> = {
let mut loop_b = TailLoopBuilder::new(vec![], vec![bool_t()], vec![usize_t()])?;
let [i1] = loop_b.input_wires_arr();
let const_wire = loop_b.add_load_value(ConstUsize::new(1));
Expand Down
23 changes: 12 additions & 11 deletions hugr-core/src/extension/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use types_mut::{
use derive_more::{Display, Error, From};

use super::{Extension, ExtensionId, ExtensionRegistry, ExtensionSet};
use crate::core::HugrNode;
use crate::ops::constant::ValueName;
use crate::ops::custom::OpaqueOpError;
use crate::ops::{NamedOp, OpName, OpType, Value};
Expand Down Expand Up @@ -78,11 +79,11 @@ pub fn resolve_value_extensions(
/// Errors that can occur during extension resolution.
#[derive(Debug, Display, Clone, Error, From, PartialEq)]
#[non_exhaustive]
pub enum ExtensionResolutionError {
pub enum ExtensionResolutionError<N: HugrNode = Node> {
/// Could not resolve an opaque operation to an extension operation.
#[display("Error resolving opaque operation: {_0}")]
#[from]
OpaqueOpError(OpaqueOpError),
OpaqueOpError(OpaqueOpError<N>),
/// An operation requires an extension that is not in the given registry.
#[display(
"{op}{} requires extension {missing_extension}, but it could not be found in the extension list used during resolution. The available extensions are: {}",
Expand All @@ -91,7 +92,7 @@ pub enum ExtensionResolutionError {
)]
MissingOpExtension {
/// The node that requires the extension.
node: Option<Node>,
node: Option<N>,
/// The operation that requires the extension.
op: OpName,
/// The missing extension
Expand All @@ -107,7 +108,7 @@ pub enum ExtensionResolutionError {
)]
MissingTypeExtension {
/// The node that requires the extension.
node: Option<Node>,
node: Option<N>,
/// The type that requires the extension.
ty: TypeName,
/// The missing extension
Expand Down Expand Up @@ -149,10 +150,10 @@ pub enum ExtensionResolutionError {
},
}

impl ExtensionResolutionError {
impl<N: HugrNode> ExtensionResolutionError<N> {
/// Create a new error for missing operation extensions.
pub fn missing_op_extension(
node: Option<Node>,
node: Option<N>,
op: &OpType,
missing_extension: &ExtensionId,
extensions: &ExtensionRegistry,
Expand All @@ -167,7 +168,7 @@ impl ExtensionResolutionError {

/// Create a new error for missing type extensions.
pub fn missing_type_extension(
node: Option<Node>,
node: Option<N>,
ty: &TypeName,
missing_extension: &ExtensionId,
extensions: &WeakExtensionRegistry,
Expand All @@ -184,7 +185,7 @@ impl ExtensionResolutionError {
/// Errors that can occur when collecting extension requirements.
#[derive(Debug, Display, Clone, Error, From, PartialEq)]
#[non_exhaustive]
pub enum ExtensionCollectionError {
pub enum ExtensionCollectionError<N: HugrNode = Node> {
/// An operation requires an extension that is not in the given registry.
#[display(
"{op}{} contains custom types for which have lost the reference to their defining extensions. Dropped extensions: {}",
Expand All @@ -193,7 +194,7 @@ pub enum ExtensionCollectionError {
)]
DroppedOpExtensions {
/// The node that is missing extensions.
node: Option<Node>,
node: Option<N>,
/// The operation that is missing extensions.
op: OpName,
/// The missing extensions.
Expand All @@ -212,10 +213,10 @@ pub enum ExtensionCollectionError {
},
}

impl ExtensionCollectionError {
impl<N: HugrNode> ExtensionCollectionError<N> {
/// Create a new error when operation extensions have been dropped.
pub fn dropped_op_extension(
node: Option<Node>,
node: Option<N>,
op: &OpType,
missing_extension: impl IntoIterator<Item = ExtensionId>,
) -> Self {
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 @@ -337,7 +337,7 @@ pub enum LoadHugrError {
Load(#[from] serde_json::Error),
/// Validation of the loaded Hugr failed.
#[error(transparent)]
Validation(#[from] ValidationError),
Validation(#[from] ValidationError<Node>),
/// Error when resolving extension operations and types.
#[error(transparent)]
Extension(#[from] ExtensionResolutionError),
Expand Down
11 changes: 8 additions & 3 deletions hugr-core/src/hugr/serialize/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::extension::test::SimpleOpDef;
use crate::extension::ExtensionRegistry;
use crate::hugr::internal::HugrMutInternals;
use crate::hugr::validate::ValidationError;
use crate::hugr::views::ExtractionResult;
use crate::ops::custom::{ExtensionOp, OpaqueOp, OpaqueOpError};
use crate::ops::{self, dataflow::IOTrait, Input, Module, Output, Value, DFG};
use crate::std_extensions::arithmetic::float_types::float64_type;
Expand Down Expand Up @@ -174,10 +175,14 @@ fn ser_roundtrip_check_schema<T: Serialize + serde::de::DeserializeOwned>(
/// equality checking.
///
/// Returns the deserialized HUGR.
pub fn check_hugr_roundtrip(hugr: &Hugr, check_schema: bool) -> Hugr {
let new_hugr = ser_roundtrip_check_schema(hugr, get_schemas(check_schema));
pub fn check_hugr_roundtrip(hugr: &impl HugrView, check_schema: bool) -> Hugr {
// Transform the whole view into a HUGR.
let (mut base, extract_map) = hugr.extract_hugr(hugr.module_root());
base.set_entrypoint(extract_map.extracted_node(hugr.entrypoint()));

check_hugr(hugr, &new_hugr);
let new_hugr = ser_roundtrip_check_schema(&base, get_schemas(check_schema));

check_hugr(&base, &new_hugr);
new_hugr
}

Expand Down
Loading
Loading