diff --git a/src/builder/module_builder.rs b/src/builder/module_builder.rs index c701438788..d594f120de 100644 --- a/src/builder/module_builder.rs +++ b/src/builder/module_builder.rs @@ -3,10 +3,7 @@ use super::{ BuildError, Container, }; -use crate::{ - hugr::view::HugrView, - types::SimpleType, -}; +use crate::{hugr::view::HugrView, types::SimpleType}; use crate::ops::handle::{AliasID, ConstID, FuncID, NodeHandle}; use crate::ops::{ConstValue, ModuleOp, OpType}; diff --git a/src/hugr/validate.rs b/src/hugr/validate.rs index 31f65a460a..e89967a55c 100644 --- a/src/hugr/validate.rs +++ b/src/hugr/validate.rs @@ -11,7 +11,7 @@ use crate::hugr::typecheck::{typecheck_const, TypeError}; use crate::ops::tag::OpTag; use crate::ops::validate::{ChildrenEdgeData, ChildrenValidationError, EdgeValidationError}; use crate::ops::{ControlFlowOp, DataflowOp, LeafOp, ModuleOp, OpType}; -use crate::types::{EdgeKind, SimpleType}; +use crate::types::{ClassicType, EdgeKind, SimpleType}; use crate::{Direction, Hugr, Node, Port}; use super::view::HugrView; @@ -375,7 +375,25 @@ impl<'a> ValidationContext<'a> { match from_optype.port_kind(from_offset).unwrap() { // Inter-graph constant wires do not have restrictions - EdgeKind::Const(_) => return Ok(()), + EdgeKind::Const(typ) => match from_optype { + // We don't need to check the second argument of ModuleOp::Const + // because it's what's providing the information for `port_kind` + OpType::Module(ModuleOp::Const(val, _)) => { + println!("Const typechecking: {:?} in {}", val, typ); + return typecheck_const(&typ, val).map_err(ValidationError::from); + } + // If const edges aren't coming from const nodes, they're graph + // edges coming from Declare or Def + OpType::Module(ModuleOp::Def { .. } | ModuleOp::Declare { .. }) => return Ok(()), + _ => { + return Err(InterGraphEdgeError::InvalidConstSrc { + from, + from_offset, + typ, + } + .into()) + } + }, EdgeKind::Value(SimpleType::Classic(_)) => {} ty => { return Err(InterGraphEdgeError::NonClassicalData { @@ -606,6 +624,9 @@ pub enum ValidationError { /// There are invalid inter-graph edges. #[error(transparent)] InterGraphEdgeError(#[from] InterGraphEdgeError), + /// Type error for constant values + #[error("Type error for constant value: {0}.")] + ConstTypeError(#[from] TypeError), } /// Errors related to the inter-graph edge validations. @@ -666,6 +687,14 @@ pub enum InterGraphEdgeError { from_parent: Node, ancestor: Node, }, + #[error( + "Const edge comes from an invalid node type: {from:?} ({from_offset:?}). Edge type: {typ}" + )] + InvalidConstSrc { + from: Node, + from_offset: Port, + typ: ClassicType, + }, } #[cfg(test)]