Skip to content

Commit

Permalink
[ewn] Add const typechecking to validation
Browse files Browse the repository at this point in the history
  • Loading branch information
croyzor committed Jun 6, 2023
1 parent e012bdf commit 035215f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
5 changes: 1 addition & 4 deletions src/builder/module_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
33 changes: 31 additions & 2 deletions src/hugr/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 035215f

Please sign in to comment.